Skip to content

Commit

Permalink
Change update mechanism of DailyReadWidget.kt (#148)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
kasem-sm and kasem-sm authored May 31, 2022
1 parent 56abcaa commit a9a072d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ questions related to SlimeKT or Android development, ping me anytime!
Vivo**](https://twitter.com/manuelvicnt) - They always help review my code snippets and add their
value to them.
- [**Hadi**](https://twitter.com/hadilq) - Assisted me in improving the modularized structure of this project.
- [**Marcel**](https://twitter.com/marxallski) - His [suggestions](https://github.com/kasem-sm/SlimeKT/pull/148) helped me to improve the Glance widget.

## License

Expand Down
1 change: 1 addition & 0 deletions docs/end_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ There are no special rules for contributing your expertise and making the open-s
- [**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).
- [**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.
- [**Hadi**](https://twitter.com/hadilq) - Assisted me in improving the modularized structure of this project.
- [**Marcel**](https://twitter.com/marxallski) - His [suggestions](https://github.com/kasem-sm/SlimeKT/pull/148) helped me to improve the Glance widget.

## License

Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,4 @@ Direct Messages on [My Twitter](https://twitter.com/KasemSM_) are always open. I
- [**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).
- [**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.
- [**Hadi**](https://twitter.com/hadilq) - Assisted me in improving the modularized structure of this project.
- [**Marcel**](https://twitter.com/marxallski) - His [suggestions](https://github.com/kasem-sm/SlimeKT/pull/148) helped me to improve the Glance widget.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ internal class DailyReadTask @AssistedInject constructor(
articleTitle: String,
featuredImage: String,
) {
DailyReadWidgetReceiver.updateWidget(articleTitle, context)

context.getBitmap(
imageLoader = imageLoader,
imageUrl = featuredImage,
Expand All @@ -95,8 +97,6 @@ internal class DailyReadTask @AssistedInject constructor(
title = "Your daily read is ready",
featuredImage = image
)

DailyReadWidgetReceiver.updateWidget(articleTitle, context)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,104 @@
package kasem.sm.article.widget

import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import androidx.glance.GlanceId
import androidx.glance.GlanceModifier
import androidx.glance.action.ActionParameters
import androidx.glance.action.clickable
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.SizeMode
import androidx.glance.appwidget.action.ActionCallback
import androidx.glance.appwidget.action.actionRunCallback
import androidx.glance.appwidget.appWidgetBackground
import androidx.glance.appwidget.state.updateAppWidgetState
import androidx.glance.appwidget.updateAll
import androidx.glance.background
import androidx.glance.currentState
import androidx.glance.layout.Column
import androidx.glance.layout.fillMaxSize
import androidx.glance.layout.padding
import androidx.glance.state.GlanceStateDefinition
import androidx.glance.text.FontWeight
import androidx.glance.text.Text
import androidx.glance.text.TextStyle
import androidx.glance.unit.ColorProvider
import java.io.File
import kotlin.random.Random

class DailyReadWidget(
private val articleTitle: String = DEFAULT,
) : GlanceAppWidget() {
class DailyReadWidget : GlanceAppWidget() {

override val stateDefinition = CustomGlanceStateDefinition

override val sizeMode: SizeMode
get() = SizeMode.Exact

@RequiresApi(Build.VERSION_CODES.S)
@Composable
override fun Content() {
val state = currentState<Preferences>()
val articleTitle = state[articleTitlePreference] ?: ""

Column(
modifier = GlanceModifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.primaryContainer)
.clickable(actionRunCallback<ActionUpdate>())
.appWidgetBackground()
.background(R.color.widget_background_color)
.clickable(actionRunCallback<ActionUpdate>()),
) {
Text(
text = articleTitle,
text = "Daily Read",
modifier = GlanceModifier.padding(10.dp),
style = TextStyle(
color = ColorProvider(MaterialTheme.colorScheme.onPrimaryContainer),
fontSize = 18.sp
color = ColorProvider(R.color.widget_text_color),
fontSize = 22.sp,
fontWeight = FontWeight.Bold
)
)
Text(
text = articleTitle,
modifier = GlanceModifier.padding(horizontal = 10.dp),
style = TextStyle(
color = ColorProvider(R.color.widget_text_color),
fontSize = 16.sp,
fontWeight = FontWeight.Normal
)
)
}
}

companion object {
const val DEFAULT = ""
private const val ARTICLE_TITLE_KEY = "kasem.sm.article.widget.article_title_key"
val articleTitlePreference =
stringPreferencesKey(ARTICLE_TITLE_KEY)
}

object CustomGlanceStateDefinition : GlanceStateDefinition<Preferences> {
override suspend fun getDataStore(context: Context, fileKey: String): DataStore<Preferences> {
return context.dataStore
}

override fun getLocation(context: Context, fileKey: String): File {
return File(context.applicationContext.filesDir, "datastore/$fileName")
}

private const val fileName = "widget_store"
private val Context.dataStore: DataStore<Preferences>
by preferencesDataStore(name = fileName)
}
}

class ActionUpdate : ActionCallback {

override suspend fun onRun(context: Context, glanceId: GlanceId, parameters: ActionParameters) {
DailyReadWidget("Updated from click").update(context, glanceId)
updateAppWidgetState(context, glanceId) { prefs ->
prefs[DailyReadWidget.articleTitlePreference] =
"Updated from Click ${Random.nextBits(100)}"
}
DailyReadWidget().updateAll(context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ package kasem.sm.article.widget

import android.content.Context
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetManager
import androidx.glance.appwidget.GlanceAppWidgetReceiver
import androidx.glance.appwidget.state.updateAppWidgetState
import androidx.glance.appwidget.updateAll

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

companion object {
suspend fun updateWidget(articleTitle: String, context: Context) {
DailyReadWidget(articleTitle).updateAll(context)
val glanceId = GlanceAppWidgetManager(context).getGlanceIds(DailyReadWidget::class.java).last()
updateAppWidgetState(context, glanceId) { prefs ->
prefs[DailyReadWidget.articleTitlePreference] = articleTitle
}
DailyReadWidget().updateAll(context)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?android:attr/colorAccent"/>
</selector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?android:attr/colorPrimary"/>
</selector>

0 comments on commit a9a072d

Please sign in to comment.