Skip to content

Commit

Permalink
Register custom sync status.
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 4, 2024
1 parent a74b9ae commit 2a082e2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import org.smartregister.fhircore.engine.configuration.workflow.ActionTrigger
import org.smartregister.fhircore.engine.domain.model.ActionConfig
import org.smartregister.fhircore.engine.domain.model.Language
import org.smartregister.fhircore.engine.domain.model.ResourceData
import org.smartregister.fhircore.engine.sync.CustomSyncState
import org.smartregister.fhircore.quest.integration.Faker
import org.smartregister.fhircore.quest.ui.main.appMainUiStateOf
import org.smartregister.fhircore.quest.ui.register.FAB_BUTTON_REGISTER_TEST_TAG
Expand Down Expand Up @@ -610,6 +611,7 @@ class RegisterScreenTest {
currentSyncJobStatus = CurrentSyncJobStatus.Succeeded(OffsetDateTime.now()),
),
onAppMainEvent = {},
customSyncState = CustomSyncState.Success,
searchQuery = searchText,
currentPage = currentPage,
pagingItems = pagingItems,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.smartregister.fhircore.engine.configuration.QuestionnaireConfig
import org.smartregister.fhircore.engine.configuration.geowidget.GeoWidgetConfiguration
import org.smartregister.fhircore.engine.domain.model.ResourceData
import org.smartregister.fhircore.engine.domain.model.ToolBarHomeNavigation
import org.smartregister.fhircore.engine.sync.CustomSyncState
import org.smartregister.fhircore.engine.util.extension.showToast
import org.smartregister.fhircore.geowidget.model.GeoJsonFeature
import org.smartregister.fhircore.geowidget.screens.GeoWidgetFragment
Expand Down Expand Up @@ -65,6 +66,7 @@ fun GeoWidgetLauncherScreen(
launchQuestionnaire: (QuestionnaireConfig, GeoJsonFeature, Context) -> Unit,
decodeImage: ((String) -> Bitmap?)?,
onAppMainEvent: (AppMainEvent) -> Unit,
customSyncState: CustomSyncState = CustomSyncState.Idle,
) {
val context = LocalContext.current
Scaffold(
Expand Down Expand Up @@ -114,6 +116,7 @@ fun GeoWidgetLauncherScreen(
appDrawerUIState = appDrawerUIState,
onAppMainEvent = onAppMainEvent,
openDrawer = openDrawer,
customSyncState = customSyncState,
)
},
) { innerPadding ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class RegisterFragment : Fragment(), OnSyncListener {
navController = findNavController(),
toolBarHomeNavigation = registerFragmentArgs.toolBarHomeNavigation,
decodeImage = { registerViewModel.getImageBitmap(it) },
customSyncState = appMainViewModel.customSyncState.collectAsState().value,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import org.smartregister.fhircore.engine.domain.model.FhirResourceConfig
import org.smartregister.fhircore.engine.domain.model.ResourceConfig
import org.smartregister.fhircore.engine.domain.model.ResourceData
import org.smartregister.fhircore.engine.domain.model.ToolBarHomeNavigation
import org.smartregister.fhircore.engine.sync.CustomSyncState
import org.smartregister.fhircore.engine.ui.components.register.LoaderDialog
import org.smartregister.fhircore.engine.ui.components.register.RegisterHeader
import org.smartregister.fhircore.engine.ui.theme.AppTheme
Expand Down Expand Up @@ -102,6 +103,7 @@ fun RegisterScreen(
navController: NavController,
toolBarHomeNavigation: ToolBarHomeNavigation = ToolBarHomeNavigation.OPEN_DRAWER,
decodeImage: ((String) -> Bitmap?)?,
customSyncState: CustomSyncState = CustomSyncState.Success,
) {
val lazyListState: LazyListState = rememberLazyListState()
Scaffold(
Expand Down Expand Up @@ -166,6 +168,7 @@ fun RegisterScreen(
appDrawerUIState = appDrawerUIState,
onAppMainEvent = onAppMainEvent,
openDrawer = openDrawer,
customSyncState = customSyncState,
)
},
) { innerPadding ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,17 @@ import java.time.OffsetDateTime
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.smartregister.fhircore.engine.sync.CustomSyncState
import org.smartregister.fhircore.engine.ui.theme.AppTheme
import org.smartregister.fhircore.engine.ui.theme.DangerColor
import org.smartregister.fhircore.engine.ui.theme.DefaultColor
import org.smartregister.fhircore.engine.ui.theme.SubtitleTextColor
import org.smartregister.fhircore.engine.ui.theme.SuccessColor
import org.smartregister.fhircore.engine.ui.theme.SyncBarBackgroundColor
import org.smartregister.fhircore.engine.util.annotation.PreviewWithBackgroundExcludeGenerated
import org.smartregister.fhircore.quest.ui.main.AppMainEvent
import org.smartregister.fhircore.quest.ui.shared.models.AppDrawerUIState
import org.smartregister.fhircore.quest.util.extensions.conditional
import timber.log.Timber

const val TRANSPARENCY = 0.2f
const val SYNC_PROGRESS_INDICATOR_TEST_TAG = "syncProgressIndicatorTestTag"
Expand All @@ -81,27 +82,36 @@ const val SYNC_PROGRESS_INDICATOR_TEST_TAG = "syncProgressIndicatorTestTag"
fun SyncBottomBar(
isFirstTimeSync: Boolean,
appDrawerUIState: AppDrawerUIState,
customSyncState: CustomSyncState,
onAppMainEvent: (AppMainEvent) -> Unit,
openDrawer: (Boolean) -> Unit,
) {
val coroutineScope = rememberCoroutineScope()
val currentSyncJobStatus = appDrawerUIState.currentSyncJobStatus
val hideSyncCompleteStatus = remember { mutableStateOf(false) }
if (currentSyncJobStatus is CurrentSyncJobStatus.Succeeded) {

if (
currentSyncJobStatus is CurrentSyncJobStatus.Succeeded &&
customSyncState is CustomSyncState.Success
) {
LaunchedEffect(Unit) {
coroutineScope.launch {
delay(7.seconds)
hideSyncCompleteStatus.value = true
}
}
}

val syncBackgroundColor =
when (currentSyncJobStatus) {
is CurrentSyncJobStatus.Failed -> DangerColor.copy(alpha = 0.2f)
is CurrentSyncJobStatus.Succeeded -> SuccessColor.copy(alpha = 0.2f)
is CurrentSyncJobStatus.Running -> SyncBarBackgroundColor
when {
currentSyncJobStatus is CurrentSyncJobStatus.Failed ||
customSyncState is CustomSyncState.Failed -> DangerColor.copy(alpha = 0.2f)
currentSyncJobStatus is CurrentSyncJobStatus.Succeeded &&
customSyncState is CustomSyncState.Success -> SuccessColor.copy(alpha = 0.2f)
currentSyncJobStatus is CurrentSyncJobStatus.Running -> SyncBarBackgroundColor
else -> Color.Transparent
}

var syncNotificationBarExpanded by remember { mutableStateOf(true) }
val bottomRadius =
if (!hideSyncCompleteStatus.value || currentSyncJobStatus is CurrentSyncJobStatus.Running) {
Expand All @@ -115,12 +125,16 @@ fun SyncBottomBar(
if (currentSyncJobStatus is CurrentSyncJobStatus.Running) 114.dp else 80.dp
else -> 60.dp
}

if (
!isFirstTimeSync &&
currentSyncJobStatus != null &&
(currentSyncJobStatus is CurrentSyncJobStatus.Running ||
currentSyncJobStatus is CurrentSyncJobStatus.Failed ||
(!hideSyncCompleteStatus.value && currentSyncJobStatus is CurrentSyncJobStatus.Succeeded))
(currentSyncJobStatus is CurrentSyncJobStatus.Failed ||
customSyncState is CustomSyncState.Failed) ||
(!hideSyncCompleteStatus.value &&
currentSyncJobStatus is CurrentSyncJobStatus.Succeeded &&
customSyncState is CustomSyncState.Success))
) {
Box(
modifier =
Expand Down Expand Up @@ -154,9 +168,11 @@ fun SyncBottomBar(
},
contentDescription = null,
tint =
when (currentSyncJobStatus) {
is CurrentSyncJobStatus.Failed -> DangerColor
is CurrentSyncJobStatus.Succeeded -> SuccessColor
when {
currentSyncJobStatus is CurrentSyncJobStatus.Failed ||
customSyncState is CustomSyncState.Failed -> DangerColor
currentSyncJobStatus is CurrentSyncJobStatus.Succeeded &&
customSyncState is CustomSyncState.Success -> SuccessColor
else -> Color.White
},
modifier = Modifier.size(16.dp),
Expand All @@ -168,34 +184,39 @@ fun SyncBottomBar(
contentAlignment = Alignment.Center,
) {
val context = LocalContext.current
when (currentSyncJobStatus) {
is CurrentSyncJobStatus.Running -> {
when {
currentSyncJobStatus is CurrentSyncJobStatus.Running -> {
SyncStatusView(
isSyncUpload = appDrawerUIState.isSyncUpload,
currentSyncJobStatus = currentSyncJobStatus,
minimized = !syncNotificationBarExpanded,
progressPercentage = appDrawerUIState.percentageProgress,
customSyncState = customSyncState,
onCancel = { onAppMainEvent(AppMainEvent.CancelSyncData(context)) },
)
SideEffect { hideSyncCompleteStatus.value = false }
}
is CurrentSyncJobStatus.Failed -> {
currentSyncJobStatus is CurrentSyncJobStatus.Failed ||
customSyncState is CustomSyncState.Failed -> {
SyncStatusView(
isSyncUpload = appDrawerUIState.isSyncUpload,
currentSyncJobStatus = currentSyncJobStatus,
minimized = !syncNotificationBarExpanded,
customSyncState = customSyncState,
onRetry = {
openDrawer(false)
onAppMainEvent(AppMainEvent.SyncData(context))
},
)
}
is CurrentSyncJobStatus.Succeeded -> {
currentSyncJobStatus is CurrentSyncJobStatus.Succeeded &&
customSyncState is CustomSyncState.Success -> {
if (!hideSyncCompleteStatus.value) {
SyncStatusView(
isSyncUpload = appDrawerUIState.isSyncUpload,
currentSyncJobStatus = currentSyncJobStatus,
minimized = !syncNotificationBarExpanded,
customSyncState = customSyncState,
)
}
}
Expand All @@ -212,15 +233,27 @@ fun SyncBottomBar(
fun SyncStatusView(
isSyncUpload: Boolean?,
currentSyncJobStatus: CurrentSyncJobStatus?,
customSyncState: CustomSyncState = CustomSyncState.Idle,
progressPercentage: Int? = null,
minimized: Boolean = false,
onRetry: () -> Unit = {},
onCancel: () -> Unit = {},
) {
val height =
if (minimized) {
36.dp
} else if (currentSyncJobStatus is CurrentSyncJobStatus.Running) 88.dp else 56.dp
when {
minimized -> 36.dp
currentSyncJobStatus is CurrentSyncJobStatus.Running -> 88.dp
else -> 56.dp
}

val isSucceeded =
currentSyncJobStatus is CurrentSyncJobStatus.Succeeded &&
customSyncState is CustomSyncState.Success
val isFailed =
currentSyncJobStatus is CurrentSyncJobStatus.Failed || customSyncState is CustomSyncState.Failed

Timber.d("current sync status evaluates to : $isSucceeded")

Row(
modifier =
Modifier.height(height)
Expand All @@ -231,35 +264,27 @@ fun SyncStatusView(
.conditional(minimized, { padding(vertical = 4.dp) }, { padding(vertical = 16.dp) }),
verticalAlignment = Alignment.CenterVertically,
) {
if (
(currentSyncJobStatus is CurrentSyncJobStatus.Failed ||
currentSyncJobStatus is CurrentSyncJobStatus.Succeeded)
) {
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.weight(1f)) {
if (isSucceeded || isFailed) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.weight(1f),
) {
if (!minimized) {
Icon(
imageVector =
if (currentSyncJobStatus is CurrentSyncJobStatus.Succeeded) {
Icons.Default.CheckCircle
} else {
Icons.Default.Error
},
imageVector = if (isSucceeded) Icons.Default.CheckCircle else Icons.Default.Error,
contentDescription = null,
tint =
when (currentSyncJobStatus) {
is CurrentSyncJobStatus.Failed -> DangerColor
is CurrentSyncJobStatus.Succeeded -> SuccessColor
else -> DefaultColor
},
tint = if (isFailed) DangerColor else SuccessColor,
)
}
SyncStatusTitle(
text =
if (currentSyncJobStatus is CurrentSyncJobStatus.Succeeded) {
stringResource(org.smartregister.fhircore.engine.R.string.sync_complete)
} else {
stringResource(org.smartregister.fhircore.engine.R.string.sync_error)
},
stringResource(
if (isSucceeded) {
org.smartregister.fhircore.engine.R.string.sync_complete
} else {
org.smartregister.fhircore.engine.R.string.sync_error
},
),
minimized = minimized,
startPadding = if (minimized) 0 else 16,
)
Expand Down Expand Up @@ -304,28 +329,17 @@ fun SyncStatusView(
}
}
}

if (
(currentSyncJobStatus is CurrentSyncJobStatus.Failed ||
currentSyncJobStatus is CurrentSyncJobStatus.Running) && !minimized
) {
if (isFailed && !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()
}
},
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,
)
Expand Down

0 comments on commit 2a082e2

Please sign in to comment.