https://kotlinlang.org logo
#feed
Title
# feed
c

CLOVIS

09/23/2023, 5:15 PM
Have you ever tried to represent loading times in your UI? You may be tempted to have a sealed class
Result
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:
Copy code
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:
Copy code
@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: DocumentationCompatibility with ArrowMaven CentralRepository • or ask questions in this thread!
❤️ 4
neat 4
🎉 1
K 1