Skip to content

Instantly share code, notes, and snippets.

@akshaybhange
Created July 23, 2019 12:47
Show Gist options
  • Save akshaybhange/1d2dd6691a5a9bd21d223f795016dab1 to your computer and use it in GitHub Desktop.
Save akshaybhange/1d2dd6691a5a9bd21d223f795016dab1 to your computer and use it in GitHub Desktop.
how to use Resource.kt class to wrap data
fun getProduct(id:Int): LiveData<Resource<Product>> {
val productData = MutableLiveData<Resource<Product>>()
productData.value = Resource.Loading()
productWebService
.getProduct(id)
.enqueue(object : Callback<Product> {
override fun onResponse(
call: Call<Product>,
response: Response<JsonObject>
) {
if (response.isSuccessful && response.body() != null) {
productData.value = response.body()?.let { Resource.Success(it) }
} else {
productData.value = Resource.Error("No Data Found")
}
}
override fun onFailure(call: Call<Product>, t: Throwable) {
productData.value = Resource.Error("Error : ${t.message}")
}
})
return productData
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment