i have a simple app that polls a service to display country list in a recyclerview. Here i am using LiveData to update the recyclerview whenever there is any change in the country list. Trouble is, LiveData's onChanged method is invoked only the first time setValue is called. But after that if there are any further changes to the data the onChanged is not invoked??
Following is my code for more info -
CountryListFragment
//Setup observer
Observer<List<CountryModel>> myObserver = new Observer<List<CountryModel>>() {
#Override
public void onChanged(#Nullable List<CountryModel> countryModels) {
mCountryList = countryModels;
//Update recyclerview
myAdapter.updateRecyclerView(mCountryList);
}
};
//Set Observer for Viewmodel
countryViewModel.getmCountryList().observe(this,myObserver);
CountryViewModel
public class CountryViewModel extends AndroidViewModel {
private MutableLiveData> mCountryList;
private MyRetrofitClient myRetrofitClient;
public CountryViewModel(#NonNull Application application) {
super(application);
}
public void init(){
mCountryList = new MutableLiveData<>();
myRetrofitClient = new MyRetrofitClient();
**mCountryList = myRetrofitClient.getCountryList(); //This works**
pollCountryList();
}
//Create polling request for every 10 secs
private void pollCountryList(){
final Handler mHandler = new Handler();
new Thread(new Runnable() {
#Override
public void run() {
for (int i=0; i<30;i++){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Call API on main thread
mHandler.post(new Runnable() {
#Override
public void run() {
**myRetrofitClient.getCountryList(); //NOT CALLING ONCHANGED??**
}
});
}
}
}).start();
}
public MutableLiveData<List<CountryModel>> getmCountryList() {
return mCountryList;
}
MyRetrofitClient.getCountryList()
public MutableLiveData<List<CountryModel>> getCountryList(){
final MutableLiveData<List<CountryModel>> lstResult = new MutableLiveData<>();
MockServiceInterface serviceInterface = mRetrofit.create(MockServiceInterface.class);
Call<List<CountryModel>> countryList = serviceInterface.getCountryList();
countryList.enqueue(new Callback<List<CountryModel>>() {
#Override
public void onResponse(Call<List<CountryModel>> call, Response<List<CountryModel>> response) {
if (response.isSuccessful()){
List<CountryModel> lstResponse = response.body();
lstResult.setValue(lstResponse);
}else {
System.out.print(response.errorBody());
}
}
#Override
public void onFailure(Call<List<CountryModel>> call, Throwable t) {
t.printStackTrace();
}
});
return lstResult;
}
Thanks!
EDIT:
Some additional observations-
When i call setValue method of MutableLiveData instance (mCountryList) inside my CountryViewModel it invokes the onChanged method each time.
However its different in case of MyRetrofitClient. The first time setValue is called in MyRetrofitClient.getCountryList(), it invokes the onChanged method. But later it does not.
Sorry, I misunderstood question at first.
You are not receiving changes because you never called setValue on mCountryList.
Method getCountryList() is returning new object MutableLiveData<List<CountryModel>> lstResult everything it is called, to which no one is observing.
Solution:
Instead of returning MutableLiveData object with getCountryList, set mCountryList in onResponse().
Code
public void init(){
mCountryList = new MutableLiveData<>();
myRetrofitClient = new MyRetrofitClient();
myRetrofitClient.getCountryList();
pollCountryList();
}
public LiveData<List<CountryModel>> getCountryListener() {
return mCountryList;
}
public void getCountryList(){
MockServiceInterface serviceInterface = mRetrofit.create(MockServiceInterface.class);
Call<List<CountryModel>> countryList = serviceInterface.getCountryList();
countryList.enqueue(new Callback<List<CountryModel>>() {
#Override
public void onResponse(Call<List<CountryModel>> call, Response<List<CountryModel>> response) {
if (response.isSuccessful()){
List<CountryModel> lstResponse = response.body();
mCountryList.setValue(lstResponse);
}else {
System.out.print(response.errorBody());
}
}
#Override
public void onFailure(Call<List<CountryModel>> call, Throwable t) {
t.printStackTrace();
}
});
}
Use getCountryListener() to observe.
Activity:
countryViewModel.getCountryListener().observe(this,myObserver);
Related
Even though I am using ViewModel, whenever the device is rotated, the data in the Recyclerview disappears. I had to put the makeSearch() method inside the onClick() method because I need to get the text that the button grabs and use it as the search parameter. Is there a better way I can handle this to avoid this problem? My code is right here:
SearchActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
// What happens when the search button is clicked
materialButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Objects.requireNonNull(textInputEditText.getText()).toString().isEmpty()) {
textInputEditText.setError("Type a search query");
} else {
mSearchInput = Objects.requireNonNull(textInputEditText.getText()).toString();
textInputEditText.setText("");
makeSearch();
}
}
});
}
// Gets the ViewModel, Observes the Question LiveData and delivers it to the Recyclerview
private void makeSearch() {
final SearchAdapter searchAdapter = new SearchAdapter();
SearchViewModel mSearchViewModel = new ViewModelProvider(this,
new CustomSearchViewModelFactory(new SearchRepository())).get(SearchViewModel.class);
mSearchViewModel.setQuery(mSearchInput);
mSearchViewModel.getQuestionLiveData().observe(this, new Observer<List<Question>>() {
#Override
public void onChanged(List<Question> questions) {
mQuestions = questions;
searchAdapter.setQuestions(questions);
}
});
mRecyclerView.setAdapter(searchAdapter);
searchAdapter.setOnClickListener(mOnClickListener);
}
SearchViewModel:
public class SearchViewModel extends ViewModel {
private SearchRepository mSearchRepository;
private MutableLiveData<String> mSearchLiveData = new MutableLiveData<>();
private LiveData<List<Question>> mQuestionLiveData = Transformations.switchMap(mSearchLiveData, (query) -> {
return mSearchRepository.getQuestions(query);
});
SearchViewModel(SearchRepository searchRepository) {
this.mSearchRepository = searchRepository;
}
public LiveData<List<Question>> getQuestionLiveData() {
return mQuestionLiveData;
}
public void setQuery(String query) {
mSearchLiveData.setValue(query);
}
}
SearchRepository:
public class SearchRepository {
//private String inTitle;
private MutableLiveData<List<Question>> mQuestions = new MutableLiveData<>();
public SearchRepository() {
//getQuestionsWithTextInTitle();
}
private void getQuestionsWithTextInTitle(String inTitle) {
ApiService apiService = RestApiClient.getApiService(ApiService.class);
Call<QuestionsResponse> call = apiService.getQuestionsWithTextInTitle(inTitle);
call.enqueue(new Callback<QuestionsResponse>() {
#Override
public void onResponse(Call<QuestionsResponse> call, Response<QuestionsResponse> response) {
QuestionsResponse questionsResponse = response.body();
if (questionsResponse != null) {
mQuestions.postValue(questionsResponse.getItems());
//shouldShowData = true;
} else {
Log.d("SearchRepository", "No matching question");
//shouldShowData = false;
}
}
#Override
public void onFailure(Call<QuestionsResponse> call, Throwable t) {
//shouldShowData = false;
t.printStackTrace();
}
});
}
public LiveData<List<Question>> getQuestions(String inTitle) {
getQuestionsWithTextInTitle(inTitle);
return mQuestions;
}
}
Your approach of passing the search input in through your CustomSearchViewModelFactory and into the constructor for the ViewModel and into the constructor for your SearchRepository isn't going to work in any case. While the first time you search your CustomSearchViewModelFactory creates the ViewModel, the second time you hit search, your SearchViewModel is already created and your factory is not invoked a second time, meaning you never get the second query.
Instead, you should file the ViewModel Overview documentation, and use Transformations.switchMap() to convert your input (the search string) into a new LiveData<List<Question>> for that given query.
This means that your ViewModel would look something like
public class SearchViewModel extends ViewModel {
private SearchRepository mSearchRepository;
private MutableLiveData<String> mSearchLiveData = new MutableLiveData<String>();
private LiveData<List<Question>> mQuestionLiveData =
Transformations.switchMap(mSearchLiveData, (query) -> {
return mSearchRepository.getQuestions(query);
});
public SearchViewModel() {
mSearchRepository = new SearchRepository();
}
public void setQuery(String query) {
mSearchLiveData.setValue(query);
}
public LiveData<List<Question>> getQuestionLiveData() {
return mQuestionLiveData;
}
}
You'd then update your Activity to:
Always observe the getQuestionLiveData() (note that you won't get a callback to your Observer until you actually set the first query)
Call setQuery() on your SearchViewModel in your makeSearch()
Remove your CustomSearchViewModelFactory entirely (it would no longer be needed).
I've been struggling for many hours on how to do this... So I have an Activity which creates a fragment.
mAddCommentButton.setOnClickListener((View v) ->{
BottomSheetAddComment bottomSheetAddComment = new BottomSheetAddComment();
bottomSheetAddComment.show(getSupportFragmentManager(), null);
});
In that fragment, it makes a network call and I want to send the results of that network call back to the Activity's Presenter, but I can't seem to understand how to do it...
private void makeNetworkCall(Comment comment){
RetrofitInterfaces.IPostNewComment service = RetrofitClientInstance.getRetrofitInstance().create(RetrofitInterfaces.IPostNewComment.class);
Call<EventCommentsDao> call = service.listRepos(comment);
call.enqueue(new Callback<EventCommentsDao>() {
#Override
public void onResponse(Call<EventCommentsDao> call, Response<EventCommentsDao> response) {
// Send response back to Activity Presenter
}
#Override
public void onFailure(Call<EventCommentsDao> call, Throwable t) {
}
});
}
Presenter:
public class EventPresenter implements EventContract.Presenter{
private EventContract.View eventView;
private EventContract.Model eventModel;
public EventPresenter(EventContract.View eventView) {
this.eventView = eventView;
eventModel = new EventModel();
}
#Override
public void onDestroy() {
this.eventView = null;
}
#Override
public void requestDataFromServer() {
if(eventView != null){
eventView.hideProgress();
}
eventModel.getEventInfo(this);
}
}
How do I get reference to the Activity Presenter so I can send the results back?
Add a method in your Activity to return event presenter:
public EventPresenter getPresenter() {
return this.eventPresenter;
}
And in your Fragment:
private void makeNetworkCall(Comment comment){
RetrofitInterfaces.IPostNewComment service = RetrofitClientInstance.getRetrofitInstance().create(RetrofitInterfaces.IPostNewComment.class);
Call<EventCommentsDao> call = service.listRepos(comment);
call.enqueue(new Callback<EventCommentsDao>() {
#Override
public void onResponse(Call<EventCommentsDao> call, Response<EventCommentsDao> response) {
// get your presenter by:
EventPresenter mPresenter = ((MyActivity) getActivity()).getPresenter();
}
#Override
public void onFailure(Call<EventCommentsDao> call, Throwable t) {
}
});
}
Different alternatives in terms of communication between fragments would be to create callback interfaces or use event bus. See this post for more details Android MVP : One Activity with Multiple Fragments
I am using LiveData to get data from the server.
In the onResume method, Calling same function every after 5 second
I am able to get data only on the First API call.
Second time the observer is not triggered and not able to get data in the fragment.
this is my fragment:
private int delay = 5 * 1000;
private ViewModel mViewModel;
private DetailsModel details = new DetailsModel();
mViewModel = ViewModelProviders.of(this).get(ViewModel.class);
mViewModel.getDetailsResponse("token", "ids");
mViewModel.getData().observe(this, new Observer< DetailsModel >() {
#Override
public void onChanged(DetailsModel response) {
details = response;
}});
//getting data in every 5 seconds
#Override
public void onResume() {
super.onResume();
liveHandler.postDelayed(runnable = new Runnable() {
public void run() {
mViewModel. getDetailsResponse("token", "ids");
liveHandler.postDelayed(runnable, delay);
}
}, delay);
}
ViewModel.java
private MutableLiveData<DetailsModel> detailsResponse;
private ProjectRepository repository = new ProjectRepository();
public void getDetailsResponse(String token, String ids) {
detailsResponse = repository.getMapData("token", "ids");
}
public MutableLiveData<DetailsModel> getData() {
return detailsResponse;
}
ProjectRepository.java
public MutableLiveData<DetailsModel> getMapData(String token, String ids) {
final MutableLiveData<DetailsModel> responseMutableLiveData = new MutableLiveData<>();
Call<DetailsModel> call = service.getMapDetails(token, ids);
call.enqueue(new Callback<DetailsModel>() {
#Override
public void onResponse(#NonNull Call<DetailsModel> call, #NonNull Response<DetailsModel> response) {
responseMutableLiveData.postValue(response.body());
}
#Override
public void onFailure(#NonNull Call<DetailsModel> call, #NonNull Throwable t) {
t.printStackTrace();
}
});
return responseMutableLiveData;
}
Whenever you call getDetailsResponse, you create a new LiveData object, which is the problem, you should do this in your ProjectRepository
final MutableLiveData<DetailsModel> responseMutableLiveData = new MutableLiveData<>();
public MutableLiveData<DetailsModel> getMapData(String token, String ids) {
Call<DetailsModel> call = service.getMapDetails(token, ids);
call.enqueue(new Callback<DetailsModel>() {
#Override
public void onResponse(#NonNull Call<DetailsModel> call, #NonNull Response<DetailsModel> response) {
responseMutableLiveData.postValue(response.body());
}
#Override
public void onFailure(#NonNull Call<DetailsModel> call, #NonNull Throwable t) {
t.printStackTrace();
}
});
return responseMutableLiveData;
}
And in your VM:
private MutableLiveData<DetailsModel> detailsResponse = null;
private ProjectRepository repository = new ProjectRepository();
public void getDetailsResponse(String token, String ids) {
if (detailsResponse == null) {
detailsResponse = repository.getMapData("token", "ids");
} else {
// Just call it, you already assigned before
repository.getMapData("token", "ids");
}
}
public MutableLiveData<DetailsModel> getData() {
return detailsResponse;
}
So, basically move the object creation out of the function itself. However, the design of your MVVM implementation can be simplified a lot. I would urge to check some examples!
You are using postDelayed() twice, so it is not working. Change your onResume() code to below mentioned.
#Override
public void onResume() {
super.onResume();
liveHandler.postDelayed(runnable = new Runnable() {
public void run() {
mViewModel.getDetailsResponse("token", "ids");
liveHandler.post(runnable);
}
}, delay);
}
I use MVVM structure in my project.
I have the main fragment with list observed a web service as you can see in the code
fragment :
mViewModel.getHomePageList().observe(this, homeDataWrapper -> {
if (homeDataWrapper!=null) {
if (homeDataWrapper.isStatus()) {
binding.homeProgressBar.setVisibility(View.INVISIBLE);
ToastUtil.showTosat(homeDataWrapper.getData().getMessage(), getContext());
Log.d(TAG, "onChanged: ");
}
}
});
view model:
ublic class HomePageViewModel extends AndroidViewModel {
private MutableLiveData<DataWrapper<Home>> data;
public ObservableInt loading;
private HomeRepository homeRepository;
private HomePageAdapter adapter;
public HomePageViewModel(#NonNull Application application) {
super(application);
}
public void init() {
adapter = new HomePageAdapter(R.layout.main_page_list, this);
homeRepository = new HomeRepository();
if (this.data != null) {
// ViewModel is created per Fragment so
// we know the userId won't change
return;
}
data = homeRepository.getHomeScreen();
}
public HomePageAdapter getAdapter() {
return adapter;
}
public void onItemClick(Integer index) {
}
public void onSerachClicked(View view) {
Navigation.findNavController(view).navigate(R.id.action_homePageFragment_to_searchActivity);
}
public MutableLiveData<DataWrapper<Home>> getHomePageList() {
return this.data;
}
}
HomeRepository :
public MutableLiveData<DataWrapper<Home>> getHomeScreen() {
final MutableLiveData<DataWrapper<Home>> homeMutableLiveData = new MutableLiveData<>();
final DataWrapper<Home> dataWrapper = new DataWrapper<>();
RetrofitInstance.getApiService().getHome().enqueue(new Callback<Home>() {
#Override
public void onResponse(#NotNull Call<Home> call, #NotNull Response<Home> response) {
Log.d("", "onResponse: " + response);
if (response.code() == 200) {
dataWrapper.setData(response.body());
dataWrapper.setStatus(true);
homeMutableLiveData.postValue(dataWrapper);
}
}
#Override
public void onFailure(Call<Home> call, Throwable t) {
Log.d("", "onResponse: " + t);
dataWrapper.setApiException((Exception) t);
dataWrapper.setStatus(false);
homeMutableLiveData.postValue(dataWrapper);
}
});
return homeMutableLiveData;
}
I would like to add SwipeRefreshLayout to update the main list. what is the correct way to call the web service again and update the list?
can anyone help me?
You can just call getHomeScreen form your Repository class to trigger data pulling from the server again, after pulling request completed, the observers will be notified using the the MutableLiveData.
But here is your issue, you are creating a new MutableLiveData object each time you call getHomeScreen. Thus, the first one will not be notified and the list will not be updated!
To solve the problem you have to initialize your MutableLiveData somewhere else so it will not be created again every time you call getHomeScreen.
I suggest you make your HomeRepository class a singleton class and initialize the MutableLiveData object inside the constructor, then you can use this object to post data to observers once you got new data from the server.
public class HomeRepository {
private static HomeRepository instance;
private MutableLiveData<DataWrapper<Home>> homeMutableLiveData;
public static HomeRepository getInstance() {
if(instance == null) instance = new HomeRepository();
return instance;
}
private HomeRepository() {
homeMutableLiveData = new MutableLiveData<>();
}
public MutableLiveData<DataWrapper<Home>> getHomeScreen() {
final DataWrapper<Home> dataWrapper = new DataWrapper<>();
RetrofitInstance.getApiService().getHome().enqueue(new Callback<Home>() {
#Override
public void onResponse(#NotNull Call<Home> call, #NotNull Response<Home> response) {
Log.d("", "onResponse: " + response);
if (response.code() == 200) {
dataWrapper.setData(response.body());
dataWrapper.setStatus(true);
homeMutableLiveData.postValue(dataWrapper);
}
}
#Override
public void onFailure(Call<Home> call, Throwable t) {
Log.d("", "onResponse: " + t);
dataWrapper.setApiException((Exception) t);
dataWrapper.setStatus(false);
homeMutableLiveData.postValue(dataWrapper);
}
});
return homeMutableLiveData;
}
}
Inside onRefereshListener of fragment
swifeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
mViewModel.getHomeScreenDetail();
}
});
In Viewmodel create getHomeScreenDetail method
public void getHomeScreenDetail(){
data = homeRepository.getHomeScreen();
}
I am working on Google's new Android Architecture Component AAC
My problem
method inside ViewModel class gets called multiple time , causing error
1 getLoggedInUser() gets called multiple times
2 Before retrofit's onNext() or onError() , the onChanged() gets called
MyActivity
LoginPojo loginPojo=new LoginPojo();
loginPojo.setEmailId(viewFunctions.getText(etLoginEmailId));
loginPojo.setPassword(viewFunctions.getText(etLoginPwd));
viewModel.loginUser(loginPojo);
viewModel.getLoggedInUser().observe(this, new Observer<LoginPojo>() {
#Override
public void onChanged(#Nullable LoginPojo pojo) {
viewFunctions.hideCustomProgress();
if (pojo.isError()) {
if (pojo.isNetworkError()) {
} else {
}
} else {
if (pojo.getStatus().equalsIgnoreCase(constants.ERROR)) {
} else {
}
}
}
});
ViewModel
private MutableLiveData<LoginPojo> mutableLogin = new MutableLiveData<>();
public final LiveData<LoginPojo> loginData = Transformations.switchMap(mutableLogin, new Function<LoginPojo, LiveData<LoginPojo>>() {
#Override
public LiveData<LoginPojo> apply(LoginPojo input) {
return repository.loginUser(input.getEmailId(), input.getPassword());
}
});
public void loginUser(LoginPojo loginPojo) {
mutableLogin.setValue(loginPojo);
}
public LiveData<LoginPojo> getLoggedInUser() {
return loginData;
}