Introduce AsyncViewDefaults for ProgressDialog
This commit is contained in:
parent
36a7e6b57b
commit
f5a6682cd6
1 changed files with 34 additions and 12 deletions
|
|
@ -18,7 +18,6 @@ package io.element.android.libraries.designsystem.components.async
|
||||||
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.compose.ui.tooling.preview.PreviewParameter
|
import androidx.compose.ui.tooling.preview.PreviewParameter
|
||||||
import io.element.android.libraries.architecture.Async
|
import io.element.android.libraries.architecture.Async
|
||||||
import io.element.android.libraries.designsystem.components.ProgressDialog
|
import io.element.android.libraries.designsystem.components.ProgressDialog
|
||||||
|
|
@ -40,35 +39,49 @@ fun <T> AsyncView(
|
||||||
async: Async<T>,
|
async: Async<T>,
|
||||||
onSuccess: (T) -> Unit,
|
onSuccess: (T) -> Unit,
|
||||||
onErrorDismiss: () -> Unit,
|
onErrorDismiss: () -> Unit,
|
||||||
modifier: Modifier = Modifier,
|
|
||||||
showProgressDialog: Boolean = true,
|
showProgressDialog: Boolean = true,
|
||||||
progressText: String? = null,
|
progressText: String? = null,
|
||||||
errorTitle: @Composable (Throwable) -> String = { ErrorDialogDefaults.title },
|
errorTitle: @Composable (Throwable) -> String = { ErrorDialogDefaults.title },
|
||||||
errorMessage: @Composable (Throwable) -> String = { it.message ?: it.toString() },
|
errorMessage: @Composable (Throwable) -> String = { it.message ?: it.toString() },
|
||||||
onRetry: (() -> Unit)? = null,
|
onRetry: (() -> Unit)? = null,
|
||||||
) {
|
) {
|
||||||
|
AsyncView(
|
||||||
|
async = async,
|
||||||
|
onSuccess = onSuccess,
|
||||||
|
onErrorDismiss = onErrorDismiss,
|
||||||
|
progressDialog = {
|
||||||
|
if (showProgressDialog) {
|
||||||
|
AsyncViewDefaults.ProgressDialog(progressText)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
errorTitle = errorTitle,
|
||||||
|
errorMessage = errorMessage,
|
||||||
|
onRetry = onRetry,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun <T> AsyncView(
|
||||||
|
async: Async<T>,
|
||||||
|
onSuccess: (T) -> Unit,
|
||||||
|
onErrorDismiss: () -> Unit,
|
||||||
|
progressDialog: @Composable () -> Unit = { AsyncViewDefaults.ProgressDialog() },
|
||||||
|
errorTitle: @Composable (Throwable) -> String = { ErrorDialogDefaults.title },
|
||||||
|
errorMessage: @Composable (Throwable) -> String = { it.message ?: it.toString() },
|
||||||
|
onRetry: (() -> Unit)? = null,
|
||||||
|
) {
|
||||||
when (async) {
|
when (async) {
|
||||||
Async.Uninitialized -> Unit
|
Async.Uninitialized -> Unit
|
||||||
is Async.Loading -> {
|
is Async.Loading -> progressDialog()
|
||||||
if (showProgressDialog) {
|
|
||||||
ProgressDialog(
|
|
||||||
modifier = modifier,
|
|
||||||
text = progressText,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
is Async.Failure -> {
|
is Async.Failure -> {
|
||||||
if (onRetry == null) {
|
if (onRetry == null) {
|
||||||
ErrorDialog(
|
ErrorDialog(
|
||||||
modifier = modifier,
|
|
||||||
title = errorTitle(async.error),
|
title = errorTitle(async.error),
|
||||||
content = errorMessage(async.error),
|
content = errorMessage(async.error),
|
||||||
onDismiss = onErrorDismiss
|
onDismiss = onErrorDismiss
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
RetryDialog(
|
RetryDialog(
|
||||||
modifier = modifier,
|
|
||||||
title = errorTitle(async.error),
|
title = errorTitle(async.error),
|
||||||
content = errorMessage(async.error),
|
content = errorMessage(async.error),
|
||||||
onDismiss = onErrorDismiss,
|
onDismiss = onErrorDismiss,
|
||||||
|
|
@ -84,6 +97,15 @@ fun <T> AsyncView(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object AsyncViewDefaults {
|
||||||
|
@Composable
|
||||||
|
fun ProgressDialog(progressText: String? = null) {
|
||||||
|
ProgressDialog(
|
||||||
|
text = progressText,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@PreviewsDayNight
|
@PreviewsDayNight
|
||||||
@Composable
|
@Composable
|
||||||
internal fun AsyncViewPreview(
|
internal fun AsyncViewPreview(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue