Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,19 @@ internal class DebugFragment : Fragment(R.layout.fragment_debug) {
}

binding.syncStart.setOnClickListener {
syncOrchestrator.startEventSync()
lifecycleScope.launch(dispatcher) {
syncOrchestrator.startEventSync()
}
}

binding.syncStop.setOnClickListener {
syncOrchestrator.stopEventSync()
}

binding.syncSchedule.setOnClickListener {
syncOrchestrator.rescheduleEventSync()
lifecycleScope.launch(dispatcher) {
syncOrchestrator.rescheduleEventSync()
}
}

binding.clearFirebaseToken.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import com.simprints.infra.eventsync.status.models.EventSyncWorkerType
import com.simprints.infra.sync.SyncOrchestrator
import com.simprints.testtools.common.coroutines.TestCoroutineRule
import io.mockk.MockKAnnotations
import io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.justRun
import io.mockk.verify
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
Expand All @@ -38,7 +39,7 @@ class RunBlockingEventSyncUseCaseTest {
fun setUp() {
MockKAnnotations.init(this)

justRun { syncOrchestrator.startEventSync() }
coJustRun { syncOrchestrator.startEventSync() }

usecase = RunBlockingEventSyncUseCase(
syncManager,
Expand All @@ -58,7 +59,7 @@ class RunBlockingEventSyncUseCaseTest {
liveData.postValue(createSyncState("sync", EventSyncWorkerState.Succeeded))
testScheduler.advanceUntilIdle()

verify { syncOrchestrator.startEventSync() }
coVerify { syncOrchestrator.startEventSync() }
verify { syncManager.getLastSyncState() }
}

Expand All @@ -74,7 +75,7 @@ class RunBlockingEventSyncUseCaseTest {
liveData.postValue(createSyncState("sync", EventSyncWorkerState.Failed()))
testScheduler.advanceUntilIdle()

verify { syncOrchestrator.startEventSync() }
coVerify { syncOrchestrator.startEventSync() }
verify { syncManager.getLastSyncState() }
}

Expand All @@ -90,7 +91,7 @@ class RunBlockingEventSyncUseCaseTest {
liveData.postValue(createSyncState("sync", EventSyncWorkerState.Cancelled))
testScheduler.advanceUntilIdle()

verify { syncOrchestrator.startEventSync() }
coVerify { syncOrchestrator.startEventSync() }
verify { syncManager.getLastSyncState() }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ interface SyncOrchestrator {
*/
fun refreshConfiguration(): Flow<Unit>

fun rescheduleEventSync(withDelay: Boolean = false)
suspend fun rescheduleEventSync(withDelay: Boolean = false)

fun cancelEventSync()

fun startEventSync(isDownSyncAllowed: Boolean = true)
suspend fun startEventSync(isDownSyncAllowed: Boolean = true)

fun stopEventSync()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.work.workDataOf
import com.simprints.core.AppScope
import com.simprints.infra.authstore.AuthStore
import com.simprints.infra.config.store.models.imagesUploadRequiresUnmeteredConnection
import com.simprints.infra.config.store.models.isCommCareEventDownSyncAllowed
import com.simprints.infra.config.sync.ConfigManager
import com.simprints.infra.eventsync.EventSyncManager
import com.simprints.infra.eventsync.sync.master.EventSyncMasterWorker
Expand Down Expand Up @@ -112,11 +113,12 @@ internal class SyncOrchestratorImpl @Inject constructor(
}.map { } // Converts flow emissions to Unit value as we only care about when it happens, not the value
}

override fun rescheduleEventSync(withDelay: Boolean) {
override suspend fun rescheduleEventSync(withDelay: Boolean) {
workManager.schedulePeriodicWorker<EventSyncMasterWorker>(
SyncConstants.EVENT_SYNC_WORK_NAME,
SyncConstants.EVENT_SYNC_WORKER_INTERVAL,
workName = SyncConstants.EVENT_SYNC_WORK_NAME,
repeatInterval = SyncConstants.EVENT_SYNC_WORKER_INTERVAL,
initialDelay = if (withDelay) SyncConstants.EVENT_SYNC_WORKER_INTERVAL else 0,
constraints = getEventSyncConstraints(),
tags = eventSyncManager.getPeriodicWorkTags(),
)
}
Expand All @@ -126,9 +128,10 @@ internal class SyncOrchestratorImpl @Inject constructor(
stopEventSync()
}

override fun startEventSync(isDownSyncAllowed: Boolean) {
override suspend fun startEventSync(isDownSyncAllowed: Boolean) {
workManager.startWorker<EventSyncMasterWorker>(
SyncConstants.EVENT_SYNC_WORK_NAME_ONE_TIME,
workName = SyncConstants.EVENT_SYNC_WORK_NAME_ONE_TIME,
constraints = getEventSyncConstraints(),
tags = eventSyncManager.getOneTimeWorkTags(),
inputData = workDataOf(EventSyncMasterWorker.IS_DOWN_SYNC_ALLOWED to isDownSyncAllowed),
)
Expand Down Expand Up @@ -238,4 +241,13 @@ internal class SyncOrchestratorImpl @Inject constructor(
.let { if (it) NetworkType.UNMETERED else NetworkType.CONNECTED }
return Constraints.Builder().setRequiredNetworkType(networkType).build()
}

private suspend fun getEventSyncConstraints(): Constraints {
// CommCare doesn't require network connection
val networkType = configManager
.getProjectConfiguration()
.isCommCareEventDownSyncAllowed()
.let { if (it) NetworkType.NOT_REQUIRED else NetworkType.CONNECTED }
return Constraints.Builder().setRequiredNetworkType(networkType).build()
}
Comment thread
BurningAXE marked this conversation as resolved.
}