Skip to content

Commit

Permalink
add a workmanager worker to be able to sync dirty tools in the backgr…
Browse files Browse the repository at this point in the history
…ound
  • Loading branch information
frett committed Oct 3, 2023
1 parent f44f06a commit 7da63c1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -112,8 +113,9 @@ class GodToolsSyncService @VisibleForTesting internal constructor(
}
suspend fun syncDirtyFavoriteTools() = try {
executeSync<UserFavoriteToolsSyncTasks> { syncDirtyFavoriteTools() }
.also { if (!it) workManager.scheduleSyncDirtyFavoriteToolsWork() }
} catch (e: CancellationException) {
// TODO: work manager job
workManager.scheduleSyncDirtyFavoriteToolsWork()
throw e
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<SyncDirtyFavoriteToolsWorker>().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()
}

0 comments on commit 7da63c1

Please sign in to comment.