Skip to content

nicolashaan/resultat

Repository files navigation

Résultat logo

Maven Central

What is Résultat?

Résultat is a fork of Kotlin Result with a loading state.

Why?

Because sometimes you start a project based on Kotlin Result and you realise you need a loading state. This library can be used as a drop-in replacement, with the same API and convenient conversion utility functions.

Integration

Manual integration

The recommended way to integrate it is to copy the Resultat.kt into you project, because it doesn't depend on any other library and you shouldn't add a dependency for such a small and critical piece of software. It also allows to rename it to whatever you want including Result, to ease migration and usage.

Using Gradle

Résultat is available on MavenCentral.

implementation("fr.haan.resultat:resultat:1.0.0")

Usage

The usage of this class is the same as kotlin Result. You may take a look at this article if you are not already familiar with it. Resultat keep the exact same behaviour, and add similar helper and side effect methods to handle the loading state. For instance:

val resulat: Resultat<Customer>
resultat
    .onFailure { exception ->
        println("Error: ${exception.message}")
    }
    .onLoading { println("Loading customer with ID: $id...") }
    .onSuccess { customer ->
        println("Hello, ${customer.firstName} ${customer.lastName} !")
    }

Similarly, the fold method have a third loading parameter:

val resulat: Resultat<Customer>
resultat.fold(
    onSuccess = {
        Resultat.success(it)
    },
    onLoading = {
        Resultat.success(defaultCustomer)
    },
    onFailure = {
        Resultat.success(defaultCustomer)
    },
)

Interoperability with Kotlin Result

Convenient methods are provided to convert a Kotlin Result to a Resultat and vice versa.

val resultat: Resultat<String> = Resultat.success("Hello")
val result: Result<String> = resultat.toResult()
// Notice the nullable type because loading state is mapped to null
val newResultat: Resultat<String>? = result.ToResultat()

API Reference

See API documentation

Made with ❤️ at BAM.tech

About

Resultat is kotlin.Result with a loading state

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages