From 09de74dbf8010a6404c15b95e3a3a2fbad59e4d1 Mon Sep 17 00:00:00 2001 From: Epicadk Date: Fri, 30 Oct 2020 02:51:10 +0530 Subject: [PATCH] Show error view in home fragment. --- .../tip/lunchbox/view/fragment/HomeFragment.java | 11 +++++++++-- .../com/tip/lunchbox/viewmodel/HomeViewModel.java | 14 ++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/tip/lunchbox/view/fragment/HomeFragment.java b/app/src/main/java/com/tip/lunchbox/view/fragment/HomeFragment.java index 5fd6949..902f5ee 100644 --- a/app/src/main/java/com/tip/lunchbox/view/fragment/HomeFragment.java +++ b/app/src/main/java/com/tip/lunchbox/view/fragment/HomeFragment.java @@ -79,7 +79,7 @@ public View onCreateView(@NotNull LayoutInflater inflater, .getRestaurant().getId()); requireActivity().startActivity(intent); }); - + onError(); BottomSheetBehavior.from(homeBinding.nsvRestaurantList); homeBinding.rvRestaurant.setLayoutManager(new LinearLayoutManager(getActivity())); homeBinding.rvRestaurant.setAdapter(adapter); @@ -108,7 +108,14 @@ private void showData() { homeBinding.llErrorOccurred.setVisibility(View.GONE); homeBinding.rvRestaurant.setVisibility(View.VISIBLE); } - + private void onError(){ + viewModel.getIsErrorLiveData().observe(getViewLifecycleOwner(), aBoolean -> { + if (aBoolean) + showErrorView(); + else + showData(); + }); + } private void loadData() { showLoadingView(); viewModel.getCategoriesLiveData().observe(getViewLifecycleOwner(), categoryResponse -> { diff --git a/app/src/main/java/com/tip/lunchbox/viewmodel/HomeViewModel.java b/app/src/main/java/com/tip/lunchbox/viewmodel/HomeViewModel.java index 9c695e9..f51f750 100644 --- a/app/src/main/java/com/tip/lunchbox/viewmodel/HomeViewModel.java +++ b/app/src/main/java/com/tip/lunchbox/viewmodel/HomeViewModel.java @@ -10,6 +10,8 @@ import com.tip.lunchbox.model.GeocodeResponse; import com.tip.lunchbox.model.SearchResponse; +import java.io.IOException; + import io.reactivex.rxjava3.annotations.NonNull; import io.reactivex.rxjava3.core.Scheduler; import io.reactivex.rxjava3.core.Single; @@ -26,6 +28,7 @@ public class HomeViewModel extends ViewModel { private MutableLiveData restaurantLiveData = new MutableLiveData<>(); private MutableLiveData categoriesLiveData = new MutableLiveData<>(); private MutableLiveData filteredRestaurantLiveData = new MutableLiveData<>(); + private MutableLiveData isError = new MutableLiveData<>(); public LiveData getCategoriesLiveData() { // Since list of categories will rarely change, previous data is used as much is possible @@ -40,6 +43,9 @@ public LiveData getRestaurantLiveData() { return restaurantLiveData; } + public LiveData getIsErrorLiveData() { + return isError; + } public LiveData getFilteredLiveData() { return filteredRestaurantLiveData; } @@ -65,7 +71,7 @@ public void onSuccess(@NonNull CategoryResponse categoryResponse) { @Override public void onError(@NonNull Throwable error) { - + isError.postValue(true); } }); } @@ -91,7 +97,7 @@ public void onSuccess(@NonNull GeocodeResponse geocodeResponse) { @Override public void onError(@NonNull Throwable error) { - + isError.postValue(true); } }); @@ -100,7 +106,7 @@ public void onError(@NonNull Throwable error) { public void fetchFilteredRestaurantData(Integer categoryId) { repository.getSearchResponseObservable(new SearchQuery().addCategory(categoryId) .addGeoLocation(18.5, 73.8)) - .subscribeOn(Schedulers.io()) + .subscribeOn(Schedulers.newThread()) .subscribe(new SingleObserver() { @Override public void onSubscribe(@NonNull Disposable disposable) { @@ -114,7 +120,7 @@ public void onSuccess(@NonNull SearchResponse searchResponse) { @Override public void onError(@NonNull Throwable error) { - + isError.postValue(true); } }); }