From b712b2fa74eed2befb167a9b02b93853d171f4c3 Mon Sep 17 00:00:00 2001 From: tuuhin Date: Fri, 11 Aug 2023 01:57:48 +0530 Subject: [PATCH] Simplified the update data code The app widget can be directly updated by using updateAll no need to use the updateAppWidget when the serializer data get updated the update is catch by the currentState. A updateData function that takes the new data and update the serializer --- .../glance/data/TaskListStateDefinition.kt | 10 +++++++ .../glance/data/TaskListUpdaterWorker.kt | 26 +++++-------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/features/glance/src/main/java/com/escodro/glance/data/TaskListStateDefinition.kt b/features/glance/src/main/java/com/escodro/glance/data/TaskListStateDefinition.kt index 593ef2fe4..284c5314f 100644 --- a/features/glance/src/main/java/com/escodro/glance/data/TaskListStateDefinition.kt +++ b/features/glance/src/main/java/com/escodro/glance/data/TaskListStateDefinition.kt @@ -39,6 +39,16 @@ internal object TaskListStateDefinition : GlanceStateDefinition> { override fun getLocation(context: Context, fileKey: String): File = context.dataStoreFile(DATA_STORE_FILENAME) + /** + * Updates the [DataStore] data + * @param context + * Context to get datastore + * @param newTasks + * List of new contents that are to be updated + */ + suspend fun updateData(context: Context, newTasks: List) = + getDataStore(context, DATA_STORE_FILENAME).updateData { newTasks } + /** * Custom serializer to write and read data from [DataStore]. */ diff --git a/features/glance/src/main/java/com/escodro/glance/data/TaskListUpdaterWorker.kt b/features/glance/src/main/java/com/escodro/glance/data/TaskListUpdaterWorker.kt index f61f1c5a2..056745bd4 100644 --- a/features/glance/src/main/java/com/escodro/glance/data/TaskListUpdaterWorker.kt +++ b/features/glance/src/main/java/com/escodro/glance/data/TaskListUpdaterWorker.kt @@ -1,9 +1,6 @@ package com.escodro.glance.data import android.content.Context -import androidx.glance.GlanceId -import androidx.glance.appwidget.GlanceAppWidgetManager -import androidx.glance.appwidget.state.updateAppWidgetState import androidx.glance.appwidget.updateAll import androidx.work.CoroutineWorker import androidx.work.ExistingWorkPolicy @@ -12,7 +9,6 @@ import androidx.work.WorkManager import androidx.work.WorkerParameters import com.escodro.glance.model.Task import com.escodro.glance.presentation.TaskListGlanceWidget -import kotlinx.collections.immutable.toImmutableList import kotlinx.coroutines.flow.first import org.koin.core.component.KoinComponent import org.koin.core.component.inject @@ -26,26 +22,18 @@ internal class TaskListUpdaterWorker( workerParameters: WorkerParameters, ) : CoroutineWorker(context, workerParameters), KoinComponent { - private val viewModel: TaskListGlanceUpdater by inject() + private val glanceUpdater: TaskListGlanceUpdater by inject() override suspend fun doWork(): Result { - val manager = GlanceAppWidgetManager(context) - val glanceIds = manager.getGlanceIds(TaskListGlanceWidget::class.java) - val list = viewModel.loadTaskList().first() - updateWidgets(glanceIds, list) + val list = glanceUpdater.loadTaskList().first() + updateWidgets(list) return Result.success() } - private suspend fun updateWidgets(glanceIds: List, list: List) { - glanceIds.forEach { - updateAppWidgetState( - context = context, - definition = TaskListStateDefinition, - glanceId = it, - updateState = { list.toImmutableList() }, - ) - TaskListGlanceWidget().updateAll(context) - } + private suspend fun updateWidgets(list: List) { + TaskListStateDefinition.updateData(context, list) + TaskListGlanceWidget().updateAll(context) + } companion object {