daivid
03/10/2022, 3:53 AMGlanceStateDefinition
shared across multiple instances of the same widget?Piotr Prus
03/10/2022, 6:07 AMdaivid
03/10/2022, 1:28 PMGlanceStateDefinition
GlanceStateDefinition
data storePiotr Prus
03/10/2022, 1:50 PMMyWidget::class
daivid
03/10/2022, 2:24 PMRemoteViewsService
then update it in RemoteViewsService.RemoteViewsFactory
Piotr Prus
03/10/2022, 4:07 PMPierre Barbier de Reuille
03/10/2022, 10:29 PMrememberSaveable
would be used for, but adapted for Glance instancesdaivid
03/11/2022, 3:47 AMdata class Thing(val label: String, val id: Long)
and my widget renders List<Thing>
, wouldn't be ok to save this list in the widget state (aka the GlanceStateDefinition
)? Otherwise, how would we do it?Mehmet Peker
03/16/2022, 6:06 PMdaivid
03/16/2022, 10:26 PMGlanceStateDefinition
), we tried both a data store and room.
2. created a helper that loads the data from X and maps it to the GlanceStateDefinition
for each widget
3. we call the helper every time the data is updated to make sure the widget is updatedMehmet Peker
03/20/2022, 1:36 PMdaivid
03/21/2022, 1:09 PMinterface WidgetStateManager<T> : WidgetStateProvider<T> {
suspend fun persist(preferences: Preferences, data: T?): Preferences
suspend fun load(): T?
}
suspend inline fun Context.updateMyWidget(myWidgetStateManager: WidgetStateManager) {
val data = myWidgetStateManager.load() // imagine WidgetStateManager uses a DataStore<Preferences> internally
val glanceIds = GlanceAppWidgetManager(this).getGlanceIds(MyGlanceWidget::class.java)
for (glanceId in glanceIds) {
updateAppWidgetState(this, PreferencesGlanceStateDefinition, glanceId) { state: Preferences ->
myWidgetStateManager.persist(state, data) // WidgetStateManager here works as a helper function, mapping the loaded data to the widget's preferences state definition
}
MyGlanceWidget().update(this, glanceId)
}
}
In your widget you could have something like this:
@Composable
fun MyWidget() {
val widgetPrefState = currentState<Preferences>
val widgetData = widgetPrefState.mapToWidgetData()
}
Not the most intuitive, but it works ok-ishMehmet Peker
03/21/2022, 8:55 PMdaivid
03/21/2022, 9:27 PM