Ensure that the Loading Dialog and the toggles update at the same time.

This commit is contained in:
Benoit Marty 2024-06-12 11:38:24 +02:00 committed by Benoit Marty
parent 55a1ac4bb5
commit 4dff3e9cce
3 changed files with 48 additions and 19 deletions

View file

@ -125,6 +125,24 @@ suspend inline fun <T> MutableState<AsyncAction<T>>.runUpdatingState(
resultBlock = resultBlock,
)
/**
* Run the given block and update the state accordingly, using only Loading and Failure states.
* It's up to the caller to manage the Success state.
*/
@OptIn(ExperimentalContracts::class)
inline fun <T> MutableState<AsyncAction<T>>.runUpdatingStateNoSuccess(
resultBlock: () -> Result<Unit>,
): Result<Unit> {
contract {
callsInPlace(resultBlock, InvocationKind.EXACTLY_ONCE)
}
value = AsyncAction.Loading
return resultBlock()
.onFailure { failure ->
value = AsyncAction.Failure(failure)
}
}
/**
* Calls the specified [Result]-returning function [resultBlock]
* encapsulating its progress and return value into an [AsyncAction] while