Created
February 25, 2018 05:01
-
-
Save AkshayChordiya/eac7a67e8bd9f2fe419c9583d93c3198 to your computer and use it in GitHub Desktop.
Generic class to hold data and state with LiveData
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A generic class that holds a value with its loading status. | |
* @param <T> | |
</T> */ | |
data class Resource<ResultType>(var status: Status, var data: ResultType? = null, var message: String? = null) { | |
companion object { | |
/** | |
* Creates [Resource] object with `SUCCESS` status and [data]. | |
*/ | |
fun <ResultType> success(data: ResultType): Resource<ResultType> = Resource(Status.SUCCESS, data) | |
/** | |
* Creates [Resource] object with `LOADING` status to notify | |
* the UI to showing loading. | |
*/ | |
fun <ResultType> loading(): Resource<ResultType> = Resource(Status.LOADING) | |
/** | |
* Creates [Resource] object with `ERROR` status and [message]. | |
*/ | |
fun <ResultType> error(message: String?): Resource<ResultType> = Resource(Status.ERROR, message = message) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Status of a resource that is provided to the UI. | |
* | |
* | |
* These are usually created by the Repository classes where they return | |
* `LiveData<Resource<T>>` to pass back the latest data to the UI with its fetch status. | |
*/ | |
enum class Status { | |
SUCCESS, | |
ERROR, | |
LOADING; | |
/** | |
* Returns `true` if the [Status] is loading else `false`. | |
*/ | |
fun isLoading() = this == LOADING | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment