CLOVIS
09/23/2023, 5:15 PMResult
with children Success
, Failure
and Loading
. But if you do that and display a value to the user, and they retry the operation, or you update the screen for some reason, the previous value disappears and is replaced by a loading indicator…
Introducing Pedestal State
Pedestal State represents on-going operations with two sealed class hierarchies: Outcome
to store the final result, ProgressiveOutcome
to represent intermediate states, including "the last result was X, and another attempt is currently ongoing to fetch a more recent value".
Thanks to #arrow, it's as easy as:
captureProgress {
out {
report(loading(0.0))
ensure(someCheck) { MyFailures.InvalidCondition }
report(loading(0.5))
doSomeOperation()
}
}.collect { println("Event: $it") }
// Output on success:
Event: Incomplete(progress = Loading(0%))
Event: Failure(failure = InvalidCondition, progress = Done)
// Output on failure:
Event: Incomplete(progress = Loading(0%))
Event: Success(value = …, progress = Done)
The accessors make it a breeze to use in Compose or another declarative framework:
@Composable
fun ShowUser(user: ProgressiveOutcome<NetworkFailure, User>) {
user.onSuccess {
Text("User: ${it.name}")
}
user.onFailure {
Text("Network error")
}
user.onLoading {
LoadingIndicator(it)
}
}
Because successful and failed states can still be loading, this will display a loading indicator while still displaying the previous value.
Learn more: Documentation • Compatibility with Arrow • Maven Central • Repository • or ask questions in this thread!