diff --git a/ground/src/main/java/com/google/android/ground/persistence/sync/MutationSyncWorkManager.kt b/ground/src/main/java/com/google/android/ground/persistence/sync/MutationSyncWorkManager.kt index b36d9afeb0..ab429c0f75 100644 --- a/ground/src/main/java/com/google/android/ground/persistence/sync/MutationSyncWorkManager.kt +++ b/ground/src/main/java/com/google/android/ground/persistence/sync/MutationSyncWorkManager.kt @@ -17,7 +17,6 @@ package com.google.android.ground.persistence.sync import androidx.work.ExistingWorkPolicy import androidx.work.WorkManager -import com.google.android.ground.persistence.sync.LocalMutationSyncWorker.Companion.createInputData import javax.inject.Inject /** Enqueues data sync work to be done in the background. */ @@ -27,18 +26,14 @@ class MutationSyncWorkManager @Inject constructor(private val workManager: WorkM * Enqueues a worker that sends changes made locally to the remote data store once a network * connection is available. The returned `Completable` completes immediately as soon as the worker * is added to the work queue (not once the sync job completes). + * + * Rather than having the running worker monitor the queue for new mutations, we instead queue the + * worker again on each new mutation. This simplifies the worker implementation and avoids race + * conditions in the rare event the worker finishes just when new mutations are added to the db. */ - fun enqueueSyncWorker(locationOfInterestId: String) { - // Rather than having running workers monitor the queue for new mutations for their respective - // locationOfInterestId, we instead queue a new worker on each new mutation. This simplifies the - // worker - // implementation and avoids race conditions in the rare event the worker finishes just when new - // mutations are added to the db. - val inputData = createInputData(locationOfInterestId) + fun enqueueSyncWorker() { val request = - WorkRequestBuilder() - .setWorkerClass(LocalMutationSyncWorker::class.java) - .buildWorkerRequest(inputData) + WorkRequestBuilder().setWorkerClass(LocalMutationSyncWorker::class.java).buildWorkerRequest() workManager.enqueueUniqueWork( LocalMutationSyncWorker::class.java.name, ExistingWorkPolicy.APPEND, diff --git a/ground/src/main/java/com/google/android/ground/repository/LocationOfInterestRepository.kt b/ground/src/main/java/com/google/android/ground/repository/LocationOfInterestRepository.kt index 3efbcb1853..f2340343ef 100644 --- a/ground/src/main/java/com/google/android/ground/repository/LocationOfInterestRepository.kt +++ b/ground/src/main/java/com/google/android/ground/repository/LocationOfInterestRepository.kt @@ -136,7 +136,7 @@ constructor( */ suspend fun applyAndEnqueue(mutation: LocationOfInterestMutation) { localLoiStore.applyAndEnqueue(mutation) - mutationSyncWorkManager.enqueueSyncWorker(mutation.locationOfInterestId) + mutationSyncWorkManager.enqueueSyncWorker() } /** Returns a flow of all valid (not deleted) [LocationOfInterest] in the given [Survey]. */ diff --git a/ground/src/main/java/com/google/android/ground/repository/SubmissionRepository.kt b/ground/src/main/java/com/google/android/ground/repository/SubmissionRepository.kt index c348dca40b..89e5179436 100644 --- a/ground/src/main/java/com/google/android/ground/repository/SubmissionRepository.kt +++ b/ground/src/main/java/com/google/android/ground/repository/SubmissionRepository.kt @@ -97,7 +97,7 @@ constructor( private suspend fun applyAndEnqueue(mutation: SubmissionMutation) { localSubmissionStore.applyAndEnqueue(mutation) - mutationSyncWorkManager.enqueueSyncWorker(mutation.locationOfInterestId) + mutationSyncWorkManager.enqueueSyncWorker() } suspend fun getTotalSubmissionCount(loi: LocationOfInterest) = diff --git a/ground/src/main/java/com/google/android/ground/ui/home/HomeScreenViewModel.kt b/ground/src/main/java/com/google/android/ground/ui/home/HomeScreenViewModel.kt index 7c2793a0bd..0289357371 100644 --- a/ground/src/main/java/com/google/android/ground/ui/home/HomeScreenViewModel.kt +++ b/ground/src/main/java/com/google/android/ground/ui/home/HomeScreenViewModel.kt @@ -75,7 +75,7 @@ internal constructor( val incompleteLoiIds = mutations.filter { it.syncStatus != COMPLETED }.map { it.locationOfInterestId }.toSet() incompleteLoiIds.forEach { loiId -> - mutationSyncWorkManager.enqueueSyncWorker(loiId) + mutationSyncWorkManager.enqueueSyncWorker() mediaUploadWorkManager.enqueueSyncWorker(loiId) } } diff --git a/ground/src/test/java/com/google/android/ground/repository/LocationOfInterestRepositoryTest.kt b/ground/src/test/java/com/google/android/ground/repository/LocationOfInterestRepositoryTest.kt index 17e9edff4c..034eea3066 100644 --- a/ground/src/test/java/com/google/android/ground/repository/LocationOfInterestRepositoryTest.kt +++ b/ground/src/test/java/com/google/android/ground/repository/LocationOfInterestRepositoryTest.kt @@ -38,7 +38,6 @@ import kotlinx.coroutines.test.advanceUntilIdle import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.mockito.ArgumentMatchers.anyString import org.mockito.Mock import org.mockito.Mockito.`when` import org.mockito.kotlin.times @@ -103,12 +102,12 @@ class LocationOfInterestRepositoryTest : BaseHiltTest() { fun testApplyAndEnqueue_enqueuesWorker() = runWithTestDispatcher { locationOfInterestRepository.applyAndEnqueue(mutation) - verify(mockWorkManager).enqueueSyncWorker(LOCATION_OF_INTEREST.id) + verify(mockWorkManager).enqueueSyncWorker() } @Test fun testApplyAndEnqueue_returnsErrorOnWorkerSyncFailure() = runWithTestDispatcher { - `when`(mockWorkManager.enqueueSyncWorker(anyString())).thenThrow(Error()) + `when`(mockWorkManager.enqueueSyncWorker()).thenThrow(Error()) assertFailsWith { locationOfInterestRepository.applyAndEnqueue( @@ -116,7 +115,7 @@ class LocationOfInterestRepositoryTest : BaseHiltTest() { ) } - verify(mockWorkManager, times(1)).enqueueSyncWorker(LOCATION_OF_INTEREST.id) + verify(mockWorkManager, times(1)).enqueueSyncWorker() } // TODO(#1373): Add tests for new LOI sync once implemented (create, update, delete, error).