-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created global UiState and it's UI handler as default states in all s…
…creens
- Loading branch information
1 parent
3a71529
commit 85ba4b8
Showing
3 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
app/src/main/java/com/developersbreach/composeactors/ui/components/ShowAlertDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {} | ||
) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/developersbreach/composeactors/ui/components/UiState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
} |
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/developersbreach/composeactors/ui/components/UiStateHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |