Skip to content

Commit

Permalink
Chain custom worker to fhir sync worker and clean up sync view.
Browse files Browse the repository at this point in the history
Signed-off-by: Lentumunai-Mark <[email protected]>
  • Loading branch information
Lentumunai-Mark committed Dec 13, 2024
1 parent 4646b49 commit d66652c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@
package org.smartregister.fhircore.engine.sync

import android.content.Context
import androidx.work.BackoffPolicy
import androidx.work.Constraints
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.sync.BackoffCriteria
import com.google.android.fhir.sync.CurrentSyncJobStatus
import com.google.android.fhir.sync.LastSyncJobStatus
import com.google.android.fhir.sync.PeriodicSyncConfiguration
import com.google.android.fhir.sync.PeriodicSyncJobStatus
import com.google.android.fhir.sync.RepeatInterval
import com.google.android.fhir.sync.RetryConfiguration
import com.google.android.fhir.sync.Sync
import com.google.android.fhir.sync.SyncJobStatus
import com.google.android.fhir.sync.download.ResourceParamsBasedDownloadWorkManager
Expand Down Expand Up @@ -80,11 +77,6 @@ constructor(
.setConstraints(
Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build(),
)
.setBackoffCriteria(
BackoffPolicy.LINEAR,
10,
TimeUnit.SECONDS,
)
.build(),
)
}
Expand All @@ -103,16 +95,6 @@ constructor(
syncConstraints =
Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build(),
repeat = RepeatInterval(interval = interval, timeUnit = TimeUnit.MINUTES),
retryConfiguration =
RetryConfiguration(
backoffCriteria =
BackoffCriteria(
backoffDelay = 10,
timeUnit = TimeUnit.SECONDS,
backoffPolicy = BackoffPolicy.EXPONENTIAL,
),
maxRetries = 3,
),
),
)
.handlePeriodicSyncJobStatus(this)
Expand All @@ -121,14 +103,34 @@ constructor(
private fun Flow<PeriodicSyncJobStatus>.handlePeriodicSyncJobStatus(
coroutineScope: CoroutineScope,
) {
this.onEach {
this.onEach { status ->
syncListenerManager.onSyncListeners.forEach { onSyncListener ->
onSyncListener.onSync(
if (it.lastSyncJobStatus as? LastSyncJobStatus.Succeeded != null) {
CurrentSyncJobStatus.Succeeded((it.lastSyncJobStatus as LastSyncJobStatus).timestamp)
Timber.d("fhir sync worker...")

Check warning on line 108 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt#L108

Added line #L108 was not covered by tests

// Check if lastSyncJobStatus is not null and is of type Succeeded
val syncStatus =

Check warning on line 111 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt#L111

Added line #L111 was not covered by tests
if (status.lastSyncJobStatus as? LastSyncJobStatus.Succeeded != null) {
CurrentSyncJobStatus.Succeeded(
(status.lastSyncJobStatus as LastSyncJobStatus.Succeeded).timestamp,

Check warning on line 114 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt#L113-L114

Added lines #L113 - L114 were not covered by tests
)
} else {
it.currentSyncJobStatus
},
status.currentSyncJobStatus

Check warning on line 117 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt#L117

Added line #L117 was not covered by tests
}

onSyncListener.onSync(syncStatus)

Check warning on line 120 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt#L120

Added line #L120 was not covered by tests
}

if (
status.currentSyncJobStatus is CurrentSyncJobStatus.Succeeded ||
status.lastSyncJobStatus is LastSyncJobStatus.Succeeded
) {
Timber.d("Periodic sync succeeded. Triggering CustomSyncWorker...")
workManager.enqueue(
OneTimeWorkRequestBuilder<CustomSyncWorker>()
.setConstraints(
Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build(),

Check warning on line 131 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt#L127-L131

Added lines #L127 - L131 were not covered by tests
)
.build(),

Check warning on line 133 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/SyncBroadcaster.kt#L133

Added line #L133 was not covered by tests
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ import org.smartregister.fhircore.engine.data.local.register.RegisterRepository
import org.smartregister.fhircore.engine.domain.model.LauncherType
import org.smartregister.fhircore.engine.domain.model.MultiSelectViewAction
import org.smartregister.fhircore.engine.sync.CustomSyncState
import org.smartregister.fhircore.engine.sync.CustomSyncWorker
import org.smartregister.fhircore.engine.sync.CustomWorkerState
import org.smartregister.fhircore.engine.sync.SyncBroadcaster
import org.smartregister.fhircore.engine.task.FhirCarePlanGenerator
Expand Down Expand Up @@ -503,12 +502,6 @@ constructor(
initialDelay = INITIAL_DELAY,
)

schedulePeriodically<CustomSyncWorker>(
workId = CustomSyncWorker.WORK_ID,
repeatInterval = applicationConfiguration.syncInterval,
initialDelay = 0,
)

measureReportConfigurations.forEach { measureReportConfig ->
measureReportConfig.scheduledGenerationDuration?.let { scheduledGenerationDuration ->
schedulePeriodically<MeasureReportMonthPeriodWorker>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,57 +339,39 @@ fun SyncStatusView(
}
}
}
if (isFailed && !minimized) {
Text(
text = stringResource(org.smartregister.fhircore.engine.R.string.retry),
modifier = Modifier.padding(start = 16.dp).clickable { onRetry() },
color = MaterialTheme.colors.primary,
fontWeight = FontWeight.SemiBold,
)
} else if (currentSyncJobStatus is CurrentSyncJobStatus.Running && !minimized) {
Text(
text = stringResource(org.smartregister.fhircore.engine.R.string.cancel),
modifier = Modifier.padding(start = 16.dp).clickable { onCancel() },
color = MaterialTheme.colors.primary,
fontWeight = FontWeight.SemiBold,
)

Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.padding(start = 16.dp),
) {
if (currentSyncJobStatus is CurrentSyncJobStatus.Running) {
LineSpinFadeLoaderProgressIndicator(
color = Color.White,
lineLength = 8f,
innerRadius = 12f,
)
}
if (
(currentSyncJobStatus is CurrentSyncJobStatus.Failed ||
currentSyncJobStatus is CurrentSyncJobStatus.Running) && !minimized
) {
Text(
text =
stringResource(
if (currentSyncJobStatus is CurrentSyncJobStatus.Failed) {
org.smartregister.fhircore.engine.R.string.retry
} else {
org.smartregister.fhircore.engine.R.string.cancel
},
),
modifier =
Modifier.padding(start = 16.dp).clickable {
if (currentSyncJobStatus is CurrentSyncJobStatus.Failed) {
onRetry()
} else {
onCancel()
}
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.padding(start = 16.dp),
) {
if (currentSyncJobStatus is CurrentSyncJobStatus.Running) {
LineSpinFadeLoaderProgressIndicator(
color = Color.White,
lineLength = 8f,
innerRadius = 12f,
)
}
if ((isFailed || currentSyncJobStatus is CurrentSyncJobStatus.Running) && !minimized) {
Text(
text =
stringResource(
if (currentSyncJobStatus is CurrentSyncJobStatus.Failed) {
org.smartregister.fhircore.engine.R.string.retry
} else {
org.smartregister.fhircore.engine.R.string.cancel
},
color = MaterialTheme.colors.primary,
fontWeight = FontWeight.SemiBold,
)
}
),
modifier =
Modifier.padding(start = 16.dp).clickable {
if (currentSyncJobStatus is CurrentSyncJobStatus.Failed) {
onRetry()
} else {
onCancel()
}
},
color = MaterialTheme.colors.primary,
fontWeight = FontWeight.SemiBold,
)
}
}
}
Expand Down Expand Up @@ -419,6 +401,7 @@ fun SyncStatusSucceededPreview() {
SyncStatusView(
isSyncUpload = false,
currentSyncJobStatus = CurrentSyncJobStatus.Succeeded(OffsetDateTime.now()),
customSyncState = CustomSyncState.Success,

Check warning on line 404 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/shared/components/SyncStatusView.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/shared/components/SyncStatusView.kt#L404

Added line #L404 was not covered by tests
)
}
}
Expand Down

0 comments on commit d66652c

Please sign in to comment.