From 85ba4b87022ec2f3cf975be6455b0b42a6753b37 Mon Sep 17 00:00:00 2001 From: Raj Date: Mon, 23 Dec 2024 17:59:11 +0530 Subject: [PATCH] Created global UiState and it's UI handler as default states in all screens --- .../ui/components/ShowAlertDialog.kt | 97 +++++++++++++++++++ .../composeactors/ui/components/UiState.kt | 12 +++ .../ui/components/UiStateHandler.kt | 50 ++++++++++ 3 files changed, 159 insertions(+) create mode 100644 app/src/main/java/com/developersbreach/composeactors/ui/components/ShowAlertDialog.kt create mode 100644 app/src/main/java/com/developersbreach/composeactors/ui/components/UiState.kt create mode 100644 app/src/main/java/com/developersbreach/composeactors/ui/components/UiStateHandler.kt diff --git a/app/src/main/java/com/developersbreach/composeactors/ui/components/ShowAlertDialog.kt b/app/src/main/java/com/developersbreach/composeactors/ui/components/ShowAlertDialog.kt new file mode 100644 index 0000000..44c1f28 --- /dev/null +++ b/app/src/main/java/com/developersbreach/composeactors/ui/components/ShowAlertDialog.kt @@ -0,0 +1,97 @@ +package com.developersbreach.composeactors.ui.components + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.material.AlertDialog +import androidx.compose.material.MaterialTheme +import androidx.compose.material.Text +import androidx.compose.material.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.tooling.preview.PreviewParameter +import androidx.compose.ui.tooling.preview.datasource.LoremIpsum +import androidx.compose.ui.window.DialogProperties +import com.developersbreach.composeactors.ui.theme.ComposeActorsTheme + +@Composable +fun ShowAlertDialog( + modifier: Modifier = Modifier, + title: String, + description: String, + onButtonClick: () -> Unit = {}, +) { + AlertDialog( + title = { + Text( + text = title, + modifier = Modifier, + style = MaterialTheme.typography.h5 + ) + }, + text = { + Text( + text = description, + modifier = Modifier, + style = MaterialTheme.typography.body1 + ) + }, + confirmButton = { + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.End + ) { + TextButton( + onClick = {}, + modifier = Modifier, + shape = MaterialTheme.shapes.medium, + content = { + Text( + text = "Dismiss", + modifier = Modifier, + style = MaterialTheme.typography.button + ) + } + ) + } + }, + onDismissRequest = onButtonClick, + modifier = modifier.testTag("TestTag:InfoDialog"), + properties = DialogProperties( + dismissOnBackPress = true, + dismissOnClickOutside = true + ), + shape = MaterialTheme.shapes.medium, + backgroundColor = MaterialTheme.colors.surface, + ) +} + +@Preview(showBackground = true, backgroundColor = 0xFF211a18) +@Composable +private fun ShowAlertDialogUIDarkPreview( + @PreviewParameter(LoremIpsum::class) text: String +) { + ComposeActorsTheme(darkTheme = true) { + ShowAlertDialog( + title = "Error occurred", + description = text, + onButtonClick = {} + ) + } +} + +@Preview(showBackground = true) +@Composable +private fun ShowAlertDialogUILightPreview( + @PreviewParameter(LoremIpsum::class) text: String +) { + ComposeActorsTheme(darkTheme = true) { + ShowAlertDialog( + title = "Error occurred", + description = text, + onButtonClick = {} + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/developersbreach/composeactors/ui/components/UiState.kt b/app/src/main/java/com/developersbreach/composeactors/ui/components/UiState.kt new file mode 100644 index 0000000..c554ec6 --- /dev/null +++ b/app/src/main/java/com/developersbreach/composeactors/ui/components/UiState.kt @@ -0,0 +1,12 @@ +package com.developersbreach.composeactors.ui.components + +sealed class UiState { + data object Loading : UiState() + data class Success( + val data: T + ) : UiState() + + data class Error( + val throwable: Throwable + ) : UiState() +} \ No newline at end of file diff --git a/app/src/main/java/com/developersbreach/composeactors/ui/components/UiStateHandler.kt b/app/src/main/java/com/developersbreach/composeactors/ui/components/UiStateHandler.kt new file mode 100644 index 0000000..78af7a9 --- /dev/null +++ b/app/src/main/java/com/developersbreach/composeactors/ui/components/UiStateHandler.kt @@ -0,0 +1,50 @@ +package com.developersbreach.composeactors.ui.components + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material.CircularProgressIndicator +import androidx.compose.material.MaterialTheme +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier + +@Composable +fun UiStateHandler( + uiState: UiState, + content: @Composable (T) -> Unit, +) { + val shouldDismissErrorDialog = rememberSaveable { mutableStateOf(false) } + + LaunchedEffect(Unit) { + shouldDismissErrorDialog.value = false + } + + Box( + modifier = Modifier.fillMaxSize() + ) { + when (uiState) { + is UiState.Loading -> { + CircularProgressIndicator( + modifier = Modifier.align(Alignment.Center), + color = MaterialTheme.colors.onBackground + ) + } + + is UiState.Error -> { + if (!shouldDismissErrorDialog.value) { + val errorDetails = uiState.throwable + ShowAlertDialog( + onButtonClick = { shouldDismissErrorDialog.value = true }, + modifier = Modifier, + title = "Error occurred", + description = errorDetails.localizedMessage ?: "Error information not available", + ) + } + } + is UiState.Success -> content(uiState.data) + } + } +} \ No newline at end of file