From 9dcdb43568dab75ecba5fee2c4ac31bfd3e39c74 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Thu, 8 Oct 2026 15:16:04 -0400 Subject: [PATCH] DRY pass #3: both sync work requests take one network-and-backoff policy (#5373) enable and pushSoon each set the CONNECTED constraint and the 30-second exponential backoff; B.online() on WorkRequest.Builder sets both. Co-Authored-By: Claude Opus 5.5 --- .../com/fabledsword/inkwell/SyncSchedule.kt | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/android/app/src/main/java/com/fabledsword/inkwell/SyncSchedule.kt b/android/app/src/main/java/com/fabledsword/inkwell/SyncSchedule.kt index e2fe4ce..973f460 100644 --- a/android/app/src/main/java/com/fabledsword/inkwell/SyncSchedule.kt +++ b/android/app/src/main/java/com/fabledsword/inkwell/SyncSchedule.kt @@ -9,6 +9,7 @@ import androidx.work.NetworkType import androidx.work.OneTimeWorkRequestBuilder import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkManager +import androidx.work.WorkRequest import java.util.concurrent.TimeUnit /** @@ -38,8 +39,7 @@ object SyncSchedule { fun enable(context: Context) { val request = PeriodicWorkRequestBuilder(PERIOD_MINUTES, TimeUnit.MINUTES) - .setConstraints(networkRequired()) - .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, BACKOFF_SECONDS, TimeUnit.SECONDS) + .online() .build() // UPDATE, not KEEP: this is called on every launch, and KEEP would ignore @@ -68,8 +68,7 @@ object SyncSchedule { fun pushSoon(context: Context) { val request = OneTimeWorkRequestBuilder() - .setConstraints(networkRequired()) - .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, BACKOFF_SECONDS, TimeUnit.SECONDS) + .online() .build() // REPLACE rather than KEEP: if an earlier attempt is sitting in a long @@ -79,11 +78,14 @@ object SyncSchedule { .enqueueUniqueWork(PUSH, ExistingWorkPolicy.REPLACE, request) } - private fun networkRequired(): Constraints = - Constraints - .Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .build() + /** What both requests share: a network to run on, and the same backoff. */ + private fun > B.online(): B = + setConstraints( + Constraints + .Builder() + .setRequiredNetworkType(NetworkType.CONNECTED) + .build(), + ).setBackoffCriteria(BackoffPolicy.EXPONENTIAL, BACKOFF_SECONDS, TimeUnit.SECONDS) private const val PERIODIC = "inkwell-periodic-sync" private const val PUSH = "inkwell-push-pending"