-
Notifications
You must be signed in to change notification settings - Fork 0
Using the Resource util class to relay data from the repository to view model
Devrath edited this page Jul 11, 2021
·
1 revision
- This
util
class is used to wrap the data object in case of success and failure object in case of failure and return a resource object. - This is very handy when we want to test the response and also handling the state.
Resource.kt
data class Resource<out T>(val status: Status, val data: T?, val message: String?) {
companion object {
fun <T> success(data: T?): Resource<T> {
return Resource(Status.SUCCESS, data, null)
}
fun <T> error(msg: String, data: T?): Resource<T> {
return Resource(Status.ERROR, data, msg)
}
fun <T> loading(data: T?): Resource<T> {
return Resource(Status.LOADING, data, null)
}
}
}
enum class Status {
SUCCESS,
ERROR,
LOADING
}