From 7da63c1c7e1b79c0fd8b936948c0f8425364ea17 Mon Sep 17 00:00:00 2001 From: Daniel Frett Date: Mon, 2 Oct 2023 17:16:12 -0600 Subject: [PATCH] add a workmanager worker to be able to sync dirty tools in the background --- .../cru/godtools/sync/GodToolsSyncService.kt | 4 ++- .../sync/work/SyncDirtyFavoriteToolsWorker.kt | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 library/sync/src/main/kotlin/org/cru/godtools/sync/work/SyncDirtyFavoriteToolsWorker.kt diff --git a/library/sync/src/main/kotlin/org/cru/godtools/sync/GodToolsSyncService.kt b/library/sync/src/main/kotlin/org/cru/godtools/sync/GodToolsSyncService.kt index a56000dc2a..f017054153 100644 --- a/library/sync/src/main/kotlin/org/cru/godtools/sync/GodToolsSyncService.kt +++ b/library/sync/src/main/kotlin/org/cru/godtools/sync/GodToolsSyncService.kt @@ -25,6 +25,7 @@ import org.cru.godtools.sync.task.ToolSyncTasks import org.cru.godtools.sync.task.UserCounterSyncTasks import org.cru.godtools.sync.task.UserFavoriteToolsSyncTasks import org.cru.godtools.sync.task.UserSyncTasks +import org.cru.godtools.sync.work.scheduleSyncDirtyFavoriteToolsWork import org.cru.godtools.sync.work.scheduleSyncFollowupsWork import org.cru.godtools.sync.work.scheduleSyncLanguagesWork import org.cru.godtools.sync.work.scheduleSyncToolSharesWork @@ -112,8 +113,9 @@ class GodToolsSyncService @VisibleForTesting internal constructor( } suspend fun syncDirtyFavoriteTools() = try { executeSync { syncDirtyFavoriteTools() } + .also { if (!it) workManager.scheduleSyncDirtyFavoriteToolsWork() } } catch (e: CancellationException) { - // TODO: work manager job + workManager.scheduleSyncDirtyFavoriteToolsWork() throw e } diff --git a/library/sync/src/main/kotlin/org/cru/godtools/sync/work/SyncDirtyFavoriteToolsWorker.kt b/library/sync/src/main/kotlin/org/cru/godtools/sync/work/SyncDirtyFavoriteToolsWorker.kt new file mode 100644 index 0000000000..c9f99befe4 --- /dev/null +++ b/library/sync/src/main/kotlin/org/cru/godtools/sync/work/SyncDirtyFavoriteToolsWorker.kt @@ -0,0 +1,29 @@ +package org.cru.godtools.sync.work + +import android.content.Context +import androidx.hilt.work.HiltWorker +import androidx.work.CoroutineWorker +import androidx.work.ExistingWorkPolicy +import androidx.work.WorkManager +import androidx.work.WorkerParameters +import dagger.assisted.Assisted +import dagger.assisted.AssistedInject +import org.cru.godtools.sync.task.UserFavoriteToolsSyncTasks + +private const val WORK_NAME = "SyncDirtyFavoriteTools" + +internal fun WorkManager.scheduleSyncDirtyFavoriteToolsWork() = enqueueUniqueWork( + WORK_NAME, + ExistingWorkPolicy.KEEP, + SyncWorkRequestBuilder().build() +) + +@HiltWorker +internal class SyncDirtyFavoriteToolsWorker @AssistedInject constructor( + @Assisted context: Context, + @Assisted workerParams: WorkerParameters, + private val favoriteToolsSyncTasks: UserFavoriteToolsSyncTasks +) : CoroutineWorker(context, workerParams) { + override suspend fun doWork() = + if (favoriteToolsSyncTasks.syncDirtyFavoriteTools()) Result.success() else Result.retry() +}