Search in sources :

Example 1 with MainMenuItem

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);
}
Also used : List(java.util.List) MviBasePresenter(com.hannesdorfmann.mosby3.mvi.MviBasePresenter) Function(io.reactivex.functions.Function) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) ProductBackendApiDecorator(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.http.ProductBackendApiDecorator) MainMenuItem(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.MainMenuItem) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Timber(timber.log.Timber) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Observable(io.reactivex.Observable) Function(io.reactivex.functions.Function) List(java.util.List) ArrayList(java.util.ArrayList) MainMenuItem(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.MainMenuItem)

Aggregations

MviBasePresenter (com.hannesdorfmann.mosby3.mvi.MviBasePresenter)1 ProductBackendApiDecorator (com.hannesdorfmann.mosby3.sample.mvi.businesslogic.http.ProductBackendApiDecorator)1 MainMenuItem (com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.MainMenuItem)1 Observable (io.reactivex.Observable)1 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)1 Function (io.reactivex.functions.Function)1 Schedulers (io.reactivex.schedulers.Schedulers)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Timber (timber.log.Timber)1