Skip to content

Commit

Permalink
Show error view in home fragment.
Browse files Browse the repository at this point in the history
  • Loading branch information
epicadk committed Oct 29, 2020
1 parent ad17f06 commit 0f58e2b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 12 additions & 2 deletions app/src/main/java/com/tip/lunchbox/view/fragment/HomeFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<CategoryContainer> 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());
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/java/com/tip/lunchbox/viewmodel/HomeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +28,7 @@ public class HomeViewModel extends ViewModel {
private MutableLiveData<GeocodeResponse> restaurantLiveData = new MutableLiveData<>();
private MutableLiveData<CategoryResponse> categoriesLiveData = new MutableLiveData<>();
private MutableLiveData<SearchResponse> filteredRestaurantLiveData = new MutableLiveData<>();
private MutableLiveData<Boolean> isError = new MutableLiveData<>();

public LiveData<CategoryResponse> getCategoriesLiveData() {
// Since list of categories will rarely change, previous data is used as much is possible
Expand All @@ -40,6 +43,9 @@ public LiveData<GeocodeResponse> getRestaurantLiveData() {
return restaurantLiveData;
}

public LiveData<Boolean> getIsErrorLiveData() {
return isError;
}
public LiveData<SearchResponse> getFilteredLiveData() {
return filteredRestaurantLiveData;
}
Expand All @@ -65,7 +71,7 @@ public void onSuccess(@NonNull CategoryResponse categoryResponse) {

@Override
public void onError(@NonNull Throwable error) {

isError.postValue(true);
}
});
}
Expand All @@ -91,7 +97,7 @@ public void onSuccess(@NonNull GeocodeResponse geocodeResponse) {

@Override
public void onError(@NonNull Throwable error) {

isError.postValue(true);
}
});

Expand All @@ -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<SearchResponse>() {
@Override
public void onSubscribe(@NonNull Disposable disposable) {
Expand All @@ -114,7 +120,7 @@ public void onSuccess(@NonNull SearchResponse searchResponse) {

@Override
public void onError(@NonNull Throwable error) {

isError.postValue(true);
}
});
}
Expand Down

0 comments on commit 0f58e2b

Please sign in to comment.