0

I am using MVVM architectural component in my app.The task is to add retrofit with MVVM.

I am calling the webserver in every 10s to get the response data . To update this value i am using Mediator live data . This is code for it

public LiveData<LiveResponse.ResponseData> getLiveResponse(String userId, String tokenId, BodyRequest bodyRequest) {
    final MutableLiveData<LiveResponse.ResponseData> liveResponseMutableLiveData = new MutableLiveData<>();
    restApiService.getLiveResponse(userId, tokenId,bodyRequest).enqueue(new Callback<LiveResponse>() {
        @Override
        public void onResponse(@NotNull Call<LiveResponse> call, @NotNull Response<LiveResponse> response) {
            try {
                if (response.body()!=null){
                    if (response.body().isSuccess()){
                        Timber.i(" liveResponseMutableLiveData onResponse:");
                        liveResponseMutableLiveData.setValue(response.body().getResponseData());
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(@NotNull Call<LiveResponse> call, @NotNull Throwable t) {
            liveResponseMutableLiveData.setValue(null);
        }
    });
    return liveResponseMutableLiveData;
}

I am facing the problem in getting the updated value even when the response has changed.

and how could i show the progress bar when this method is called and show the error if any in my view model .

1 Answers1

0

YourViewModel.ktx

private val TAG = YourViewModel::class.java.name val observableProject: MutableLiveData = MutableLiveData() var userID: MutableLiveData = MutableLiveData()

var livedata = ObservableField<LiveResponse.ResponseData>()


fun setID(id: String,token: String,request BodyRequest) {
    this.userID.setValue(id)
    fetchDataFromServer(id,token,request)
}

fun getServerFetchdata() : LiveData<LiveResponse.ResponseData>{
  return livedata

}

private fun fetchDataFromServer(userId : String,tokenId : String,bodyRequest : BodyRequest ) {
    restApiService.getLiveResponse(userId, tokenId,bodyRequest).enqueue(new Callback<LiveResponse>() {
    @Override
    public void onResponse(@NotNull Call<LiveResponse> call, @NotNull Response<LiveResponse> response) {
        try {
            if (response.body()!=null){
                if (response.body().isSuccess()){
                    Timber.i(" liveResponseMutableLiveData onResponse:");
                    livedata.setValue(response.body().getResponseData());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(@NotNull Call<LiveResponse> call, @NotNull Throwable t) {
        liveResponseMutableLiveData.setValue(null);
    }
});

}

MainActivity.java

public fun getServerdata(){

viewModel.getProjectListdata().observe(this, Observer { projects ->

// Your data recieve here
    })

}

Samset
  • 109
  • 7