1
+ /*
2
+ * Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
3
+ */
4
+
5
+ package at.bitfire.davdroid.ui.widget
6
+
7
+ import android.content.Context
8
+ import android.widget.Toast
9
+ import androidx.compose.runtime.Composable
10
+ import androidx.compose.ui.unit.dp
11
+ import androidx.glance.ColorFilter
12
+ import androidx.glance.GlanceId
13
+ import androidx.glance.GlanceModifier
14
+ import androidx.glance.Image
15
+ import androidx.glance.ImageProvider
16
+ import androidx.glance.LocalContext
17
+ import androidx.glance.action.clickable
18
+ import androidx.glance.appwidget.GlanceAppWidget
19
+ import androidx.glance.appwidget.cornerRadius
20
+ import androidx.glance.appwidget.provideContent
21
+ import androidx.glance.background
22
+ import androidx.glance.layout.Alignment
23
+ import androidx.glance.layout.Box
24
+ import androidx.glance.layout.fillMaxSize
25
+ import androidx.glance.layout.size
26
+ import androidx.glance.unit.ColorProvider
27
+ import at.bitfire.davdroid.R
28
+ import at.bitfire.davdroid.ui.M3ColorScheme
29
+ import dagger.hilt.EntryPoint
30
+ import dagger.hilt.InstallIn
31
+ import dagger.hilt.android.EntryPointAccessors
32
+ import dagger.hilt.components.SingletonComponent
33
+
34
+ /* *
35
+ * A widget with a "Sync all" button displaying just an icon to indicate the action.
36
+ */
37
+ class IconSyncButtonWidget : GlanceAppWidget () {
38
+
39
+ // Hilt over @AndroidEntryPoint is not available for widgets
40
+ @EntryPoint
41
+ @InstallIn(SingletonComponent ::class )
42
+ interface SyncButtonWidgetEntryPoint {
43
+ fun model (): SyncWidgetModel
44
+ }
45
+
46
+
47
+ override suspend fun provideGlance (context : Context , id : GlanceId ) {
48
+ // initial data
49
+ val entryPoint = EntryPointAccessors .fromApplication<SyncButtonWidgetEntryPoint >(context)
50
+ val model = entryPoint.model()
51
+
52
+ // will be called when the widget is updated
53
+ provideContent {
54
+ WidgetContent (model)
55
+ }
56
+ }
57
+
58
+ @Composable
59
+ private fun WidgetContent (model : SyncWidgetModel ) {
60
+ val context = LocalContext .current
61
+
62
+ Box (
63
+ modifier = GlanceModifier
64
+ .size(50 .dp)
65
+ .background(ColorProvider (M3ColorScheme .primaryLight))
66
+ .cornerRadius(25 .dp)
67
+ .clickable {
68
+ model.requestSync()
69
+ Toast .makeText(context, R .string.sync_started, Toast .LENGTH_SHORT ).show()
70
+ },
71
+ contentAlignment = Alignment .Center ,
72
+ ) {
73
+ Image (
74
+ provider = ImageProvider (R .drawable.ic_sync),
75
+ contentDescription = context.getString(R .string.widget_sync_all_accounts),
76
+ modifier = GlanceModifier .fillMaxSize().size(32 .dp),
77
+ colorFilter = ColorFilter .tint(
78
+ ColorProvider (M3ColorScheme .onPrimaryLight)
79
+ )
80
+ )
81
+ }
82
+ }
83
+
84
+ }
0 commit comments