I\'m new to Android development and i am trying to understand Live Data with MVVM architecture. I am trying to make the main activity recognize when there is a change in an obje
Solution by OP.
As Neha Rathore suggested this is the solution that works for me:
in the view Model:
public void login(String userName , String hashedPassword) {
usersRepository.login(userName, hashedPassword, new Callback() {
@Override
public void onResponse(Call call, Response response) {
loggedInUser.setValue(response.body());
}
@Override
public void onFailure(Call call, Throwable t) {
loggedInUser.setValue(null);
}
});
}
and in the Repository:
public void login(String username, String hashedPassword,@Nullable final Callback callback){
final MutableLiveData loggedInUser = new MutableLiveData<>();
User user = new User(username,hashedPassword);
usersRepositoryApi.login(user).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful()) {
callback.onResponse(call,response);
}
}
@Override
public void onFailure(Call call, Throwable t) {
callback.onFailure(call,t);
}
});
}