use of com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.MainMenuItem in project mosby by sockeqwe.
the class MainMenuPresenter method bindIntents.
@Override
protected void bindIntents() {
Observable<List<String>> loadCategories = intent(MainMenuView::loadCategoriesIntent).doOnNext(categoryName -> Timber.d("intent: load category %s", categoryName)).flatMap(ignored -> backendApi.getAllCategories().subscribeOn(Schedulers.io()));
Observable<String> selectCategory = intent(MainMenuView::selectCategoryIntent).doOnNext(categoryName -> Timber.d("intent: select category %s", categoryName)).startWith(MainMenuItem.HOME);
List<Observable<?>> allIntents = new ArrayList<>(2);
allIntents.add(loadCategories);
allIntents.add(selectCategory);
Observable<MenuViewState> menuViewStateObservable = Observable.combineLatest(allIntents, (Function<Object[], MenuViewState>) objects -> {
List<String> categories = (List<String>) objects[0];
String selectedCategory = (String) objects[1];
List<MainMenuItem> categoriesItems = new ArrayList<MainMenuItem>(categories.size() + 1);
categoriesItems.add(new MainMenuItem(MainMenuItem.HOME, selectedCategory.equals(MainMenuItem.HOME)));
for (int i = 0; i < categories.size(); i++) {
String category = categories.get(i);
categoriesItems.add(new MainMenuItem(category, category.equals(selectedCategory)));
}
return new MenuViewState.DataState(categoriesItems);
}).startWith(new MenuViewState.LoadingState()).onErrorReturn(MenuViewState.ErrorState::new).observeOn(AndroidSchedulers.mainThread());
subscribeViewState(menuViewStateObservable, MainMenuView::render);
}
Aggregations