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
var productData = MediatorLiveData<Resource<Product>>() | |
init { | |
fetchData() | |
} | |
fun fetchData() { | |
productData.addSource(productRepository.getproduct(id)) { | |
productData.value = it | |
} |
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> | |
) { |
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
sealed class Resource<T>( | |
val data: T? = null, | |
val message: String? = null | |
) { | |
class Success<T>(data: T) : Resource<T>(data) | |
class Loading<T>(data: T? = null) : Resource<T>(data) | |
class Error<T>(message: String, data: T? = null) : Resource<T>(data, message) | |
} | |
enum class Status { |
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
private void subscribeToModel() { | |
// Observe product data | |
viewModel.getObservableProduct().observe(getViewLifecycleOwner(), new Observer<Product>() { | |
@Override | |
public void onChanged(@Nullable Product product) { | |
mTitle.setText(product.title); | |
} | |
}); | |
} |
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
//this is wrong | |
//writing logic for filtering items in your Fragmet | |
var filterList = simpleList.filter { it%2 == 0 } | |
//this is correct | |
//function created for it in your ViewModel | |
fun getEvenNumbers(simpleList : List<Int>):List<Int> { | |
return simpleList.filter { it%2 == 0 } | |
} |