PendingActions
An immutable action holder data class to be used in UiStates and to be consumed in Compose to indicate pending loading async states.
Example usage:
// Screen actions
interface Action {
data object LongOperation : Action
}
// Screen ui state
data class UiState(
val title: String,
val pendingActions: PendingActions<Action> = PendingActions()
)
val uiState = MutableStateFlow(UiState(title = "PendingActions"))
// Adding pending action before executing a blocking operation and removing afterwards
fun onAction(action: Action) {
when (action) {
is Action.LongOperation -> {
uiState.update { it.copy(pendingActions = it.pendingActions + action) }
// perform blocking operation
uiState.update { it.copy(pendingActions = it.pendingActions - action) }
}
}
}
// Using isLoading inside compose
@Composable
fun PendingActions(uiState: UiState) {
val isLoading = uiState.pendingActions.isLoading(action = Action.LongOperation)
// rest of the screen
}
Content copied to clipboard