Search code examples
androidretrofit2android-livedatamutablelivedata

How to relay retrofit response from data repository to view model using mutable live data in android?


I am able to make a network request and get back a response inside my data repository but not able to get that inside my view model.

Data repository:

public class DataRepository {

private APIService apiService;
private static DataRepository INSTANCE = null;

public MutableLiveData<ResponseEntity> loginUser(UserEntity userEntity){

    final MutableLiveData<ResponseEntity> responseEntity = new MutableLiveData<>();
    apiService.loginUser(userEntity)
            .enqueue(new Callback<ResponseEntity>() {
                @Override
                public void onResponse(Call<ResponseEntity> call, Response<ResponseEntity> response) {
                    Log.d(Constants.LOGGER, "from data repository " + response.body());
                    responseEntity.setValue(response.body());
                }

                @Override
                public void onFailure(Call<ResponseEntity> call, Throwable t) {
                    Log.d(Constants.LOGGER, "from data repository: there was an error");
                    responseEntity.setValue(null);
                }
            });
    return responseEntity;
}
}

View model:

public class LoginViewModel extends AndroidViewModel {  

private MutableLiveData<ResponseEntity> networkResponse;


public void sendLoginNetworkRequest(UserEntity userEntity){
    networkResponse = mRepository.loginUser(userEntity);
}

public MutableLiveData<ResponseEntity> getResponse(){
    return networkResponse;
} 

Activity:

public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    loginViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
        loginViewModel.getResponse()
            .observe(this, new Observer<ResponseEntity>() {
                @Override
                public void onChanged(@Nullable ResponseEntity responseEntity) {
                    Log.d(Constants.LOGGER, "response entity changed " + responseEntity);
                }
            });
}

    public void loginClicked(View view) {
       loginViewModel.sendLoginNetworkRequest(userEntity);
   }
}

The log from the data repository shows up but the one from the activity doesn't. What am I doing wrong?


Solution

  • I found the answer! I had to make the responseEntity MutableLiveData variable in my DataRepository class into a class variable and create a function which returns that and now it works!

    Repository:

    public class DataRepository {
    
    private APIService apiService;
    private static DataRepository INSTANCE = null;
    final MutableLiveData<ResponseEntity> responseEntity = new MutableLiveData<>();
    
    public void loginUser(UserEntity userEntity){
    
        apiService.loginUser(userEntity)
                .enqueue(new Callback<ResponseEntity>() {
                    @Override
                    public void onResponse(Call<ResponseEntity> call, Response<ResponseEntity> response) {
                        Log.d(Constants.LOGGER, "from data repository " + response.body());
                        responseEntity.setValue(response.body());
                    }
    
                    @Override
                    public void onFailure(Call<ResponseEntity> call, Throwable t) {
                        Log.d(Constants.LOGGER, "from data repository: there was an error");
                        responseEntity.setValue(null);
                    }
                });
    }
    public MutableLiveData<ResponseEntity> getLiveResponses(){
            return responseEntity;
        }
    }
    

    Viewmodel:

    public class LoginViewModel extends AndroidViewModel {      
    
    public void sendLoginNetworkRequest(UserEntity userEntity){
        mRepository.loginUser(userEntity);
    }
    
    public MutableLiveData<ResponseEntity> getResponse(){
        return mRepository.getLiveResponse;
    } 
    }