Resource

Resource multiplatform utilities

License

License

GroupId

GroupId

com.javiersc.resource
ArtifactId

ArtifactId

resource-jvm
Last Version

Last Version

4.0.0
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

Resource
Resource multiplatform utilities
Project URL

Project URL

http://github.com/JavierSegoviaCordoba/Resource
Source Code Management

Source Code Management

https://github.com/JavierSegoviaCordoba/Resource

Download resource-jvm

Dependencies

compile (3)

Group / Artifact Type Version
org.jetbrains.kotlinx : kotlinx-serialization-core-jvm jar 1.0.1
org.jetbrains.kotlinx : kotlinx-coroutines-core-jvm jar 1.4.2
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.4.20

Project Modules

There are no modules declared in this project.
Master Download Coverage Master Master Build Quality Master
Develop Download Coverage Develop Develop Build Quality Develop

Resource

Resource is a sealed class that allows you to wrap any object based on a state. It has these options:

  • ๐Ÿ”„ Loading: To use at that moment that a loading indicator should appear.
  • ๐Ÿ‘ Success: When the happy path occurs.
  • โŒ Error: If there is a problem you will get this.

This library works very well when used in conjunction with NetworkResponse which is very similar to Resource but is intended for use with Retrofit.

For more information see the website

Download

This library is Kotlin Multiplatform but at this moment jvm is the only artifact generated. It is available at Maven Central.

implementation("com.javiersc.resource:resource:$version")

Fold your Resource

Fold a Resource invokes multiple callbacks to manage its state for any event. A normal flow can be:

  1. Emit Loading to show the progress indicator.
  2. Emit Success to populate your data or emit Error if something were wrong to show an error.
val dog: Dog = Dog("Auri")
val resource: Resource<Dog, Error> = Resource.Success(dog)

resource.folder {
    loading { println("Loading: Yes") }
    noLoading { println("Loading: no") }  // Invoked

    success { dog: Dog -> println("Success: $dog") } // Invoked
    noSuccess { println("Success: no") }

    error { error: Error -> println("Error: $error") }
    noError { println("Error: no") }  // Invoked
}

Exists a fold function similar to folder buth without the builder pattern

val dog: Dog = Dog("Auri")
val resource: Resource<Dog, Error> = Resource.Success(dog)

resource.fold(
    loading = { println("Loading: Yes") },
    noLoading = { println("Loading: no") },  // Invoked
    success = { dog: Dog -> println("Success: $dog") }, // Invoked
    noSuccess = { println("Success: no") },
    error = { error: Error -> println("Error: $error") },
    noError = { println("Error: no") },  // Invoked
)

You don't have to add all those functions, for example, you usually only have to use:

  • loading to show a progress indicator.
  • noLoading to hide the progress indicator.
  • success to load the data.
  • error to show and error.

Mappers and common extension functions

Map a Resource to another Resource is possible with the following extension function:

  • Resource to Resource
val anotherResource: Resource<AnotherUser, AnotherError> = resource.map(
    success = { user: User -> user.toAnotherUser() },
    error = { error: Error -> error.toAnotherError() }
)
// toAnotherUser() and toAnotherError() mappers should be created by yourself, if they are
// extension functions and the resource uses inference for the type:
val anotherResource = resource.map(User::toAnotherUser, Error::toAnotherError)
  • Some value to Resource
val name: String = "Auri"
val nameResource = name.toResourceSuccess()

val message: String = "Some error message"
val messageResource = message.toResourceError()
  • A lot of checkers for each state, for example:
val resource: Resource<String> = Resource.Success("Auri")
resource.ifSuccess { data: String ->
    println(data) // "Auri"
}

You can see all the common extension functions here and here

Flow

There are four Flow extension functions:

  • Flow<R>.map(...) included in Kotlin, let you to easily map the object inside your Flow to any Resource:
val usersFlow: Flow<List<User>>

val usersResourceFlow: Flow<Resource<List<User>, Error>> =
    usersFlow.map { users: List<User> -> Resource.Success(users) }
  • Flow<R>.toResourceSuccess()
val usersSuccessFlow: Flow<Resource<List<User>, Error>> = usersFlow.toResourceSuccess()
  • Flow<R>.toResourceError()
val usersErrorFlow: Flow<Resource<List<User>, Error>> = usersFlow.toResourceError()

Versions

Version
4.0.0