summaryrefslogtreecommitdiff
path: root/android/app/src/main/java/org/terst/doot/widget/ui/Actions.kt
blob: 3a813fd881c618944a82f882b119f2545629124c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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<String>("item_id")
        val sourceKey = ActionParameters.Key<String>("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)
    }
}