Reputation:
For many years I have been developing Android apps with the MVP pattern but I'm now trying to learn MVVM
with ViewModel
and LiveData
In the following example I'm not getting how I would communicate a failure or successful result upon a POST
or GET
request with Retrofit
Before with MVP I would with use a Listener to communicate presenter with either listener.onTodoFetched()
or listener.onTodoFetchError()
and then reacting differently based on which method gets called. Should I still communicate in this way with my ViewModel
class?
FetchTodoRepository.java
public MutableLiveData<String> fetchTodo() {
retrofitService.getRetrofitService().create(Endpoints.class).getTodo().enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful() && response.body() != null) {
listener.onTodoFetched(response.body()); //ViewModel equivalent?
} else {
listener.onTodoFetchError(response.message()); //ViewModel equivalent?
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
listener.onTodoFetchError(t.getMessage()); //ViewModel equivalent?
}
});
return mutableLiveData;
}
UPDATE:
Based on answers and further research one could use the following methods:
Upvotes: 9
Views: 5047
Reputation: 5
It's easy to implement.
Just use HTTP status code. In response you'll get status code, just send it to viewmodel and in viewmodel you can easily send it to Activity or fragment by observing it.
Eg. 200 - Ok 400 - Bad request etc.
I don't know it helps you or not but this thing works for me.
Upvotes: 0
Reputation: 2611
Communication between Repository and ViewModel using Higher-Oder Functions Pass function as Argument into repository method
For Example in Repository method like this:
fun repoMethod(id : Int, viewModelCallBack : (List<CustomeDto>) -> Unit)
{
//Your API call
.....
viewModelCallBack(response.body())
}
And View Model Method like
fun viewModelMethod()
{
repoObj.repoMethod(5){ listOfValueReturnFromApi ->
//Here listOfValueReturnFromApi is API response using Higher Order function in kotlin
}
}
Upvotes: 4
Reputation: 1420
When you are using mvvm with livedata you really don't need to use interface to pass the data to viewmodel.
MutableLiveData has method setValue() pass your response in that and then when you will call this fetchTodo() method in viewmodel you will get all your data.
Try below code
public MutableLiveData<String> fetchTodo() {
retrofitService.getRetrofitService().create(Endpoints.class).getTodo().enqueue(new
Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful() && response.body() != null) {
mutableLiveData.setValue(response.body())
} else {
mutableLiveData.setValue(response.message());
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
mutableLiveData.setValue(t.message()); //ViewModel equivalent?
}
});
return mutableLiveData;
}
In your viewmodel :
public LiveData<String> fetchTodo()
{
return yourRepoInstance.fetchTodo() ; here you will get result data
}
Upvotes: 2