Skip to content

Commit

Permalink
Make sync after save configurable (#110)
Browse files Browse the repository at this point in the history
* Settings

* Update QuestionnaireViewModel.kt

* Update SettingsScreen.kt

* Add auto sync chip switch

* Update build.gradle.kts

* Update SettingsScreen.kt
  • Loading branch information
sevenreup authored Sep 23, 2024
1 parent e600512 commit 3391b0d
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.StringType
import org.smartregister.fhircore.engine.R
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.sync.SyncBroadcaster
import org.smartregister.fhircore.engine.trace.PerformanceReporter
import org.smartregister.fhircore.engine.ui.base.AlertDialogue
import org.smartregister.fhircore.engine.ui.base.AlertDialogue.showConfirmAlert
Expand All @@ -80,8 +79,6 @@ open class QuestionnaireActivity : BaseMultiLanguageActivity(), View.OnClickList

@Inject lateinit var dispatcherProvider: DefaultDispatcherProvider

@Inject lateinit var syncBroadcaster: SyncBroadcaster

@Inject lateinit var tracer: PerformanceReporter

open val questionnaireViewModel: QuestionnaireViewModel by viewModels()
Expand Down Expand Up @@ -393,7 +390,6 @@ open class QuestionnaireActivity : BaseMultiLanguageActivity(), View.OnClickList
extras: List<Resource>? = null,
) {
dismissSaveProcessing()
syncBroadcaster.runSync()
postSaveSuccessful(questionnaireResponse, extras)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import org.hl7.fhir.r4.model.Task
import org.hl7.fhir.r4.model.Task.TaskStatus
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.sync.SyncBroadcaster
import org.smartregister.fhircore.engine.task.FhirCarePlanGenerator
import org.smartregister.fhircore.engine.trace.PerformanceReporter
import org.smartregister.fhircore.engine.util.AssetUtil
Expand Down Expand Up @@ -103,9 +104,10 @@ constructor(
val defaultRepository: DefaultRepository,
val configurationRegistry: ConfigurationRegistry,
val transformSupportServices: TransformSupportServices,
val simpleWorkerContext: SimpleWorkerContext,
private val simpleWorkerContext: SimpleWorkerContext,
val dispatcherProvider: DispatcherProvider,
val sharedPreferencesHelper: SharedPreferencesHelper,
var syncBroadcaster: SyncBroadcaster,
var tracer: PerformanceReporter,
) : ViewModel() {
@Inject lateinit var fhirCarePlanGenerator: FhirCarePlanGenerator
Expand Down Expand Up @@ -308,6 +310,9 @@ constructor(
backReference,
)
extractionProgress.postValue(ExtractionProgress.Success(questionnaireResponse, extras))
if (sharedPreferencesHelper.read(SharedPreferenceKey.SYNC_ON_SAVE.name, true)) {
syncBroadcaster.runSync()
}
} catch (e: Exception) {
Timber.e(e)
extractionProgress.postValue(ExtractionProgress.Failed(questionnaireResponse, e))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.material.Divider
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material.Scaffold
Expand Down Expand Up @@ -159,10 +160,30 @@ fun SettingsScreen(
values = SyncUploadStrategy.entries.map { it.name },
)

switchPreference(
key = SharedPreferenceKey.SYNC_ON_SAVE.name,
defaultValue = true,
title = { Text(text = "Sync on form answered") },
summary = {
Text(
text =
"When disabled, form saves will not start sync automatically, you have to manually sync the changes.",
style = MaterialTheme.typography.caption,
)
},
)

switchPreference(
key = SharedPreferenceKey.PATIENT_FIX_TYPE.name,
defaultValue = false,
title = { Text(text = "Fix patients offline") },
summary = {
Text(
text =
"When enable the app will attempt to fix the patient completely offline which my fail",
style = MaterialTheme.typography.caption,
)
},
)

item {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ enum class SharedPreferenceKey {
USER_CLAIM_INFO,
SYNC_UPLOAD_STRATEGY,
PATIENT_FIX_TYPE,
SYNC_ON_SAVE,
}
6 changes: 3 additions & 3 deletions android/quest/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,21 @@ android {
applicationIdSuffix = ".mwcore"
versionNameSuffix = "-mwcore"
versionCode = 37
versionName = "0.2.0.2-beta1"
versionName = "0.2.0.2-beta2"
}
create("mwcoreDev") {
dimension = "apps"
applicationIdSuffix = ".mwcoreDev"
versionNameSuffix = "-mwcoreDev"
versionCode = 37
versionName = "0.2.0.2-beta1"
versionName = "0.2.0.2-beta2"
}
create("mwcoreStaging") {
dimension = "apps"
applicationIdSuffix = ".mwcoreStaging"
versionNameSuffix = "-mwcoreStaging"
versionCode = 37
versionName = "0.2.0.2-beta1"
versionName = "0.2.0.2-beta2"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,20 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Button
import androidx.compose.material.Chip
import androidx.compose.material.ChipDefaults
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.LinearProgressIndicator
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.outlined.MoreVert
import androidx.compose.material.icons.outlined.Refresh
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -93,9 +96,10 @@ fun PatientProfileScreen(
var showOverflowMenu by remember { mutableStateOf(false) }
val viewState = patientProfileViewModel.patientProfileUiState.value
val taskId by appMainViewModel.completedTaskId.collectAsState()
val syncing by remember { patientProfileViewModel.isSyncing }
val syncing by patientProfileViewModel.isSyncing.collectAsState()
val tasksId = profileViewData.tasks.map { it.actionFormId }
val loadingState by patientProfileViewModel.loadingState.collectAsState()
val autoSyncEnabled by patientProfileViewModel.autoSyncOn.collectAsState()

val launchQuestionnaireActivityForResults =
rememberLauncherForActivityResult(
Expand All @@ -120,6 +124,11 @@ fun PatientProfileScreen(
}
},
actions = {
AutoSyncButton(
enabled = autoSyncEnabled,
isSyncing = syncing,
runSync = patientProfileViewModel::runSync,
)
IconButton(onClick = { patientProfileViewModel.reFetch() }, enabled = !syncing) {
Icon(
imageVector = Icons.Outlined.Refresh,
Expand Down Expand Up @@ -339,3 +348,27 @@ fun PatientProfileScreen(
}
}
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun AutoSyncButton(enabled: Boolean, isSyncing: Boolean, runSync: () -> Unit) {
if (!enabled) {
Chip(
onClick = runSync,
colors =
if (isSyncing) {
ChipDefaults.chipColors()
} else
ChipDefaults.chipColors(
backgroundColor = MaterialTheme.colors.error,
contentColor = MaterialTheme.colors.onError,
),
) {
if (isSyncing) {
Text(text = "Syncing...")
} else {
Text(text = "Auto sync off")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ import org.smartregister.fhircore.engine.domain.util.PaginationConstant
import org.smartregister.fhircore.engine.sync.SyncBroadcaster
import org.smartregister.fhircore.engine.ui.questionnaire.QuestionnaireActivity
import org.smartregister.fhircore.engine.ui.questionnaire.QuestionnaireType
import org.smartregister.fhircore.engine.util.SharedPreferenceKey
import org.smartregister.fhircore.engine.util.SharedPreferencesHelper
import org.smartregister.fhircore.engine.util.extension.asReference
import org.smartregister.fhircore.engine.util.extension.isGuardianVisit
import org.smartregister.fhircore.quest.R
Expand Down Expand Up @@ -82,6 +84,7 @@ constructor(
val configurationRegistry: ConfigurationRegistry,
private val profileViewDataMapper: ProfileViewDataMapper,
val registerViewDataMapper: RegisterViewDataMapper,
val sharedPreferences: SharedPreferencesHelper,
) : ViewModel() {

val appFeatureName = savedStateHandle.get<String>(NavigationArg.FEATURE)
Expand All @@ -91,7 +94,7 @@ constructor(
val familyId = savedStateHandle.get<String>(NavigationArg.FAMILY_ID)

// TODO: replace later with actual implementation from the engine
val isSyncing = mutableStateOf(false)
val isSyncing = MutableStateFlow(false)

var patientProfileUiState: MutableState<PatientProfileUiState> =
mutableStateOf(
Expand All @@ -114,6 +117,8 @@ constructor(

val loadingState = MutableStateFlow<DataLoadState<Boolean>>(DataLoadState.Idle)

val autoSyncOn = MutableStateFlow(true)

init {
syncBroadcaster.registerSyncListener(
{ state ->
Expand All @@ -126,6 +131,7 @@ constructor(
is SyncJobStatus.Started -> {
isSyncing.value = true
}
is SyncJobStatus.InProgress -> {}
else -> {
isSyncing.value = false
}
Expand All @@ -135,6 +141,15 @@ constructor(
)

fetchPatientProfileDataWithChildren()
checkAutoSyncStatus()
}

private fun checkAutoSyncStatus() {
autoSyncOn.value = sharedPreferences.read(SharedPreferenceKey.SYNC_ON_SAVE.name, true)
}

fun runSync() {
syncBroadcaster.runSync()
}

fun getOverflowMenuHostByPatientType(healthStatus: HealthStatus): OverflowMenuHost {
Expand Down

0 comments on commit 3391b0d

Please sign in to comment.