Skip to content

Commit

Permalink
Simplified the update data code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tuuhin authored and igorescodro committed Aug 15, 2023
1 parent ad7534f commit b712b2f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ internal object TaskListStateDefinition : GlanceStateDefinition<List<Task>> {
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<Task>) =
getDataStore(context, DATA_STORE_FILENAME).updateData { newTasks }

/**
* Custom serializer to write and read data from [DataStore].
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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<GlanceId>, list: List<Task>) {
glanceIds.forEach {
updateAppWidgetState(
context = context,
definition = TaskListStateDefinition,
glanceId = it,
updateState = { list.toImmutableList() },
)
TaskListGlanceWidget().updateAll(context)
}
private suspend fun updateWidgets(list: List<Task>) {
TaskListStateDefinition.updateData(context, list)
TaskListGlanceWidget().updateAll(context)

}

companion object {
Expand Down

0 comments on commit b712b2f

Please sign in to comment.