Rename AsyncView to AsyncActionView
This commit is contained in:
parent
7b2341aec7
commit
e42005fc52
71 changed files with 369 additions and 315 deletions
|
|
@ -17,14 +17,15 @@
|
|||
package io.element.android.libraries.designsystem.components.async
|
||||
|
||||
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
|
||||
import io.element.android.libraries.architecture.AsyncData
|
||||
import io.element.android.libraries.architecture.AsyncAction
|
||||
|
||||
open class AsyncProvider : PreviewParameterProvider<AsyncData<Unit>> {
|
||||
override val values: Sequence<AsyncData<Unit>>
|
||||
open class AsyncActionProvider : PreviewParameterProvider<AsyncAction<Unit>> {
|
||||
override val values: Sequence<AsyncAction<Unit>>
|
||||
get() = sequenceOf(
|
||||
AsyncData.Uninitialized,
|
||||
AsyncData.Loading(),
|
||||
AsyncData.Failure(Exception("An error occurred")),
|
||||
AsyncData.Success(Unit),
|
||||
AsyncAction.Uninitialized,
|
||||
AsyncAction.Confirming,
|
||||
AsyncAction.Loading,
|
||||
AsyncAction.Failure(Exception("An error occurred")),
|
||||
AsyncAction.Success(Unit),
|
||||
)
|
||||
}
|
||||
|
|
@ -18,40 +18,55 @@ package io.element.android.libraries.designsystem.components.async
|
|||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.tooling.preview.PreviewParameter
|
||||
import io.element.android.libraries.architecture.AsyncData
|
||||
import io.element.android.libraries.architecture.AsyncAction
|
||||
import io.element.android.libraries.designsystem.components.ProgressDialog
|
||||
import io.element.android.libraries.designsystem.components.dialogs.ErrorDialog
|
||||
import io.element.android.libraries.designsystem.components.dialogs.ErrorDialogDefaults
|
||||
import io.element.android.libraries.designsystem.components.dialogs.RetryDialog
|
||||
import io.element.android.libraries.designsystem.preview.ElementPreview
|
||||
import io.element.android.libraries.designsystem.preview.PreviewsDayNight
|
||||
import io.element.android.libraries.ui.strings.CommonStrings
|
||||
|
||||
/**
|
||||
* Render an Async object.
|
||||
* Render an AsyncAction object.
|
||||
* - If Success, invoke the callback [onSuccess], only once.
|
||||
* - If Failure, display a dialog with the error, which can be transformed, using [errorMessage]. When
|
||||
* closed, [onErrorDismiss] will be invoked. If [onRetry] is not null, a retry button will be displayed.
|
||||
* - When loading, display a loading dialog, if [showProgressDialog] is true, with on optional [progressText].
|
||||
*/
|
||||
@Composable
|
||||
fun <T> AsyncView(
|
||||
async: AsyncData<T>,
|
||||
fun <T> AsyncActionView(
|
||||
async: AsyncAction<T>,
|
||||
onSuccess: (T) -> Unit,
|
||||
onErrorDismiss: () -> Unit,
|
||||
showConfirmationDialog: Boolean = false,
|
||||
confirmationText: String? = null,
|
||||
confirmationSubmit: String? = null,
|
||||
onConfirmation: (() -> Unit)? = null,
|
||||
showProgressDialog: Boolean = true,
|
||||
progressText: String? = null,
|
||||
errorTitle: @Composable (Throwable) -> String = { ErrorDialogDefaults.title },
|
||||
errorMessage: @Composable (Throwable) -> String = { it.message ?: it.toString() },
|
||||
onRetry: (() -> Unit)? = null,
|
||||
) {
|
||||
AsyncView(
|
||||
AsyncActionView(
|
||||
async = async,
|
||||
onSuccess = onSuccess,
|
||||
onErrorDismiss = onErrorDismiss,
|
||||
confirmingDialog = {
|
||||
if (showConfirmationDialog) {
|
||||
AsyncActionViewDefaults.ConfirmationDialog(
|
||||
confirmationText = confirmationText,
|
||||
confirmationSubmit = confirmationSubmit,
|
||||
onConfirmation = onConfirmation,
|
||||
)
|
||||
}
|
||||
},
|
||||
progressDialog = {
|
||||
if (showProgressDialog) {
|
||||
AsyncViewDefaults.ProgressDialog(progressText)
|
||||
AsyncActionViewDefaults.ProgressDialog(progressText)
|
||||
}
|
||||
},
|
||||
errorTitle = errorTitle,
|
||||
|
|
@ -61,19 +76,21 @@ fun <T> AsyncView(
|
|||
}
|
||||
|
||||
@Composable
|
||||
fun <T> AsyncView(
|
||||
async: AsyncData<T>,
|
||||
fun <T> AsyncActionView(
|
||||
async: AsyncAction<T>,
|
||||
onSuccess: (T) -> Unit,
|
||||
onErrorDismiss: () -> Unit,
|
||||
progressDialog: @Composable () -> Unit = { AsyncViewDefaults.ProgressDialog() },
|
||||
confirmingDialog: @Composable () -> Unit = { AsyncActionViewDefaults.ConfirmationDialog() },
|
||||
progressDialog: @Composable () -> Unit = { AsyncActionViewDefaults.ProgressDialog() },
|
||||
errorTitle: @Composable (Throwable) -> String = { ErrorDialogDefaults.title },
|
||||
errorMessage: @Composable (Throwable) -> String = { it.message ?: it.toString() },
|
||||
onRetry: (() -> Unit)? = null,
|
||||
) {
|
||||
when (async) {
|
||||
AsyncData.Uninitialized -> Unit
|
||||
is AsyncData.Loading -> progressDialog()
|
||||
is AsyncData.Failure -> {
|
||||
AsyncAction.Uninitialized -> Unit
|
||||
AsyncAction.Confirming -> confirmingDialog()
|
||||
is AsyncAction.Loading -> progressDialog()
|
||||
is AsyncAction.Failure -> {
|
||||
if (onRetry == null) {
|
||||
ErrorDialog(
|
||||
title = errorTitle(async.error),
|
||||
|
|
@ -89,7 +106,7 @@ fun <T> AsyncView(
|
|||
)
|
||||
}
|
||||
}
|
||||
is AsyncData.Success -> {
|
||||
is AsyncAction.Success -> {
|
||||
LaunchedEffect(async) {
|
||||
onSuccess(async.data)
|
||||
}
|
||||
|
|
@ -97,7 +114,23 @@ fun <T> AsyncView(
|
|||
}
|
||||
}
|
||||
|
||||
object AsyncViewDefaults {
|
||||
object AsyncActionViewDefaults {
|
||||
@Composable
|
||||
fun ConfirmationDialog(
|
||||
confirmationText: String? = null,
|
||||
confirmationSubmit: String? = null,
|
||||
onConfirmation: (() -> Unit)? = null,
|
||||
onDismiss: (() -> Unit)? = null,
|
||||
) {
|
||||
io.element.android.libraries.designsystem.components.dialogs.ConfirmationDialog(
|
||||
content = confirmationText.orEmpty(),
|
||||
onSubmitClicked = onConfirmation ?: {},
|
||||
onDismiss = onDismiss ?: {},
|
||||
submitText = confirmationSubmit ?: stringResource(CommonStrings.action_continue),
|
||||
cancelText = stringResource(CommonStrings.action_cancel),
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ProgressDialog(progressText: String? = null) {
|
||||
ProgressDialog(
|
||||
|
|
@ -108,10 +141,10 @@ object AsyncViewDefaults {
|
|||
|
||||
@PreviewsDayNight
|
||||
@Composable
|
||||
internal fun AsyncViewPreview(
|
||||
@PreviewParameter(AsyncProvider::class) async: AsyncData<Unit>,
|
||||
internal fun AsyncActionViewPreview(
|
||||
@PreviewParameter(AsyncActionProvider::class) async: AsyncAction<Unit>,
|
||||
) = ElementPreview {
|
||||
AsyncView(
|
||||
AsyncActionView(
|
||||
async = async,
|
||||
onSuccess = {},
|
||||
onErrorDismiss = {},
|
||||
Loading…
Add table
Add a link
Reference in a new issue