Skip to content

Commit a9a072d

Browse files
kasem-smkasem-sm
andauthored
Change update mechanism of DailyReadWidget.kt (#148)
* Change update mechanism of DailyReadWidget.kt (untested) * Refactor on method of retrieving `glanceID` * improvements * Update Glance widget first. * Added material you support for glance widget. * Use custom state for Glance widget. * Add credits. * Fix spacing Co-authored-by: kasem-sm <[email protected]>
1 parent 56abcaa commit a9a072d

File tree

8 files changed

+80
-18
lines changed

8 files changed

+80
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ questions related to SlimeKT or Android development, ping me anytime!
127127
Vivo**](https://twitter.com/manuelvicnt) - They always help review my code snippets and add their
128128
value to them.
129129
- [**Hadi**](https://twitter.com/hadilq) - Assisted me in improving the modularized structure of this project.
130+
- [**Marcel**](https://twitter.com/marxallski) - His [suggestions](https://github.com/kasem-sm/SlimeKT/pull/148) helped me to improve the Glance widget.
130131

131132
## License
132133

docs/end_notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ There are no special rules for contributing your expertise and making the open-s
1212
- [**Gabor Varadi**](https://twitter.com/Zhuinden) - He is always willing to answer my questions. A great man and a blessing to the Android community (AKA, the `SavedStateHandle` preacher).
1313
- [**Doris Liu**](https://twitter.com/doris4lt) and [**Manuel Vivo**](https://twitter.com/manuelvicnt) - They always help review my code snippets and add their value to them.
1414
- [**Hadi**](https://twitter.com/hadilq) - Assisted me in improving the modularized structure of this project.
15+
- [**Marcel**](https://twitter.com/marxallski) - His [suggestions](https://github.com/kasem-sm/SlimeKT/pull/148) helped me to improve the Glance widget.
1516

1617
## License
1718

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,4 @@ Direct Messages on [My Twitter](https://twitter.com/KasemSM_) are always open. I
121121
- [**Gabor Varadi**](https://twitter.com/Zhuinden) - He is always willing to answer my questions. A great man and a blessing to the Android community (AKA, the `SavedStateHandle` preacher).
122122
- [**Doris Liu**](https://twitter.com/doris4lt) and [**Manuel Vivo**](https://twitter.com/manuelvicnt) - They always help review my code snippets and add their value to them.
123123
- [**Hadi**](https://twitter.com/hadilq) - Assisted me in improving the modularized structure of this project.
124+
- [**Marcel**](https://twitter.com/marxallski) - His [suggestions](https://github.com/kasem-sm/SlimeKT/pull/148) helped me to improve the Glance widget.

features/article/daily-read-worker/src/main/java/kasem/sm/article/daily_read_worker/DailyReadTask.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ internal class DailyReadTask @AssistedInject constructor(
8585
articleTitle: String,
8686
featuredImage: String,
8787
) {
88+
DailyReadWidgetReceiver.updateWidget(articleTitle, context)
89+
8890
context.getBitmap(
8991
imageLoader = imageLoader,
9092
imageUrl = featuredImage,
@@ -95,8 +97,6 @@ internal class DailyReadTask @AssistedInject constructor(
9597
title = "Your daily read is ready",
9698
featuredImage = image
9799
)
98-
99-
DailyReadWidgetReceiver.updateWidget(articleTitle, context)
100100
}
101101
)
102102
}

features/article/widget/src/main/java/kasem/sm/article/widget/DailyReadWidget.kt

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,104 @@
55
package kasem.sm.article.widget
66

77
import android.content.Context
8-
import android.os.Build
9-
import androidx.annotation.RequiresApi
10-
import androidx.compose.material3.MaterialTheme
118
import androidx.compose.runtime.Composable
129
import androidx.compose.ui.unit.dp
1310
import androidx.compose.ui.unit.sp
11+
import androidx.datastore.core.DataStore
12+
import androidx.datastore.preferences.core.Preferences
13+
import androidx.datastore.preferences.core.stringPreferencesKey
14+
import androidx.datastore.preferences.preferencesDataStore
1415
import androidx.glance.GlanceId
1516
import androidx.glance.GlanceModifier
1617
import androidx.glance.action.ActionParameters
1718
import androidx.glance.action.clickable
1819
import androidx.glance.appwidget.GlanceAppWidget
20+
import androidx.glance.appwidget.SizeMode
1921
import androidx.glance.appwidget.action.ActionCallback
2022
import androidx.glance.appwidget.action.actionRunCallback
23+
import androidx.glance.appwidget.appWidgetBackground
24+
import androidx.glance.appwidget.state.updateAppWidgetState
25+
import androidx.glance.appwidget.updateAll
2126
import androidx.glance.background
27+
import androidx.glance.currentState
2228
import androidx.glance.layout.Column
2329
import androidx.glance.layout.fillMaxSize
2430
import androidx.glance.layout.padding
31+
import androidx.glance.state.GlanceStateDefinition
32+
import androidx.glance.text.FontWeight
2533
import androidx.glance.text.Text
2634
import androidx.glance.text.TextStyle
2735
import androidx.glance.unit.ColorProvider
36+
import java.io.File
37+
import kotlin.random.Random
2838

29-
class DailyReadWidget(
30-
private val articleTitle: String = DEFAULT,
31-
) : GlanceAppWidget() {
39+
class DailyReadWidget : GlanceAppWidget() {
40+
41+
override val stateDefinition = CustomGlanceStateDefinition
42+
43+
override val sizeMode: SizeMode
44+
get() = SizeMode.Exact
3245

33-
@RequiresApi(Build.VERSION_CODES.S)
3446
@Composable
3547
override fun Content() {
48+
val state = currentState<Preferences>()
49+
val articleTitle = state[articleTitlePreference] ?: ""
50+
3651
Column(
3752
modifier = GlanceModifier
3853
.fillMaxSize()
39-
.background(MaterialTheme.colorScheme.primaryContainer)
40-
.clickable(actionRunCallback<ActionUpdate>())
54+
.appWidgetBackground()
55+
.background(R.color.widget_background_color)
56+
.clickable(actionRunCallback<ActionUpdate>()),
4157
) {
4258
Text(
43-
text = articleTitle,
59+
text = "Daily Read",
4460
modifier = GlanceModifier.padding(10.dp),
4561
style = TextStyle(
46-
color = ColorProvider(MaterialTheme.colorScheme.onPrimaryContainer),
47-
fontSize = 18.sp
62+
color = ColorProvider(R.color.widget_text_color),
63+
fontSize = 22.sp,
64+
fontWeight = FontWeight.Bold
65+
)
66+
)
67+
Text(
68+
text = articleTitle,
69+
modifier = GlanceModifier.padding(horizontal = 10.dp),
70+
style = TextStyle(
71+
color = ColorProvider(R.color.widget_text_color),
72+
fontSize = 16.sp,
73+
fontWeight = FontWeight.Normal
4874
)
4975
)
5076
}
5177
}
5278

5379
companion object {
54-
const val DEFAULT = ""
80+
private const val ARTICLE_TITLE_KEY = "kasem.sm.article.widget.article_title_key"
81+
val articleTitlePreference =
82+
stringPreferencesKey(ARTICLE_TITLE_KEY)
83+
}
84+
85+
object CustomGlanceStateDefinition : GlanceStateDefinition<Preferences> {
86+
override suspend fun getDataStore(context: Context, fileKey: String): DataStore<Preferences> {
87+
return context.dataStore
88+
}
89+
90+
override fun getLocation(context: Context, fileKey: String): File {
91+
return File(context.applicationContext.filesDir, "datastore/$fileName")
92+
}
93+
94+
private const val fileName = "widget_store"
95+
private val Context.dataStore: DataStore<Preferences>
96+
by preferencesDataStore(name = fileName)
5597
}
5698
}
5799

58100
class ActionUpdate : ActionCallback {
59-
60101
override suspend fun onRun(context: Context, glanceId: GlanceId, parameters: ActionParameters) {
61-
DailyReadWidget("Updated from click").update(context, glanceId)
102+
updateAppWidgetState(context, glanceId) { prefs ->
103+
prefs[DailyReadWidget.articleTitlePreference] =
104+
"Updated from Click ${Random.nextBits(100)}"
105+
}
106+
DailyReadWidget().updateAll(context)
62107
}
63108
}

features/article/widget/src/main/java/kasem/sm/article/widget/DailyReadWidgetReceiver.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ package kasem.sm.article.widget
66

77
import android.content.Context
88
import androidx.glance.appwidget.GlanceAppWidget
9+
import androidx.glance.appwidget.GlanceAppWidgetManager
910
import androidx.glance.appwidget.GlanceAppWidgetReceiver
11+
import androidx.glance.appwidget.state.updateAppWidgetState
1012
import androidx.glance.appwidget.updateAll
1113

1214
class DailyReadWidgetReceiver : GlanceAppWidgetReceiver() {
1315
override val glanceAppWidget: GlanceAppWidget = DailyReadWidget()
1416

1517
companion object {
1618
suspend fun updateWidget(articleTitle: String, context: Context) {
17-
DailyReadWidget(articleTitle).updateAll(context)
19+
val glanceId = GlanceAppWidgetManager(context).getGlanceIds(DailyReadWidget::class.java).last()
20+
updateAppWidgetState(context, glanceId) { prefs ->
21+
prefs[DailyReadWidget.articleTitlePreference] = articleTitle
22+
}
23+
DailyReadWidget().updateAll(context)
1824
}
1925
}
2026
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:color="?android:attr/colorAccent"/>
4+
</selector>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item android:color="?android:attr/colorPrimary"/>
4+
</selector>

0 commit comments

Comments
 (0)