package org.terst.doot.widget.ui import android.content.Context import androidx.datastore.preferences.core.edit import androidx.glance.GlanceId import androidx.glance.action.ActionParameters import androidx.glance.appwidget.action.ActionCallback import androidx.glance.appwidget.updateAll import org.terst.doot.widget.data.Keys import org.terst.doot.widget.data.dataStore import org.terst.doot.widget.work.CompleteWorker import org.terst.doot.widget.work.RefreshWorker class CompleteTaskAction : ActionCallback { override suspend fun onAction( context: Context, glanceId: GlanceId, parameters: ActionParameters ) { val id = parameters[idKey] ?: return val source = parameters[sourceKey] ?: return CompleteWorker.enqueue(context, id, source) } companion object { val idKey = ActionParameters.Key("item_id") val sourceKey = ActionParameters.Key("item_source") } } // Sets IS_REFRESHING synchronously (before the network round trip) so the // widget's icon flips to the loading state on the spot -- RefreshWorker // clears the flag when it finishes, regardless of outcome, so the icon // never gets stuck. class RefreshTaskAction : ActionCallback { override suspend fun onAction( context: Context, glanceId: GlanceId, parameters: ActionParameters ) { context.dataStore.edit { it[Keys.IS_REFRESHING] = true } DootWidget().updateAll(context) RefreshWorker.runOnce(context) } }