From 0f58e2b1a32a84a946dfb89d7ab78e53adc5128b 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 | 14 ++++++++++++-- .../com/tip/lunchbox/viewmodel/HomeViewModel.java | 14 ++++++++++---- 2 files changed, 22 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..72e0043 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); @@ -109,13 +109,23 @@ private void showData() { 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 -> { if (categoryResponse != null) { List categories = categoryResponse.getCategories(); for (CategoryContainer categoryContainer : categories) { - Chip categoryChip = (Chip)(getLayoutInflater() + Chip categoryChip = (Chip) (getLayoutInflater() .inflate(R.layout.item_chip_category, homeBinding.filterChipGroup, false)); categoryChip.setText(categoryContainer.getCategories().getName()); 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); } }); }