Created
July 23, 2019 12:47
-
-
Save akshaybhange/1d2dd6691a5a9bd21d223f795016dab1 to your computer and use it in GitHub Desktop.
how to use Resource.kt class to wrap data
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
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