Skip to content

Commit

Permalink
Created global UiState and it's UI handler as default states in all s…
Browse files Browse the repository at this point in the history
…creens
  • Loading branch information
RajashekarRaju committed Dec 23, 2024
1 parent 3a71529 commit 85ba4b8
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 = {}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.developersbreach.composeactors.ui.components

sealed class UiState<out T> {
data object Loading : UiState<Nothing>()
data class Success<out T>(
val data: T
) : UiState<T>()

data class Error(
val throwable: Throwable
) : UiState<Nothing>()
}
Original file line number Diff line number Diff line change
@@ -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 <T> UiStateHandler(
uiState: UiState<T>,
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)
}
}
}

0 comments on commit 85ba4b8

Please sign in to comment.