CurrencyListEditorPresenter extends BasePresenter<CurrencyListEditorView> { @Override @CallSuper public void subscribe() { final ConnectableObservable<List<CharSequence>> currenciesConnectableObservable = Observable.fromCallable(this.databaseHelper::getCurrenciesList) .subscribeOn(subscribeOnScheduler) .observeOn(observeOnScheduler) .publish(); this.compositeDisposable.add(currenciesConnectableObservable .subscribe(view.displayCurrencies())); this.compositeDisposable.add(currenciesConnectableObservable .map(currenciesList -> { final String currencyCode; if (savedInstanceState != null && savedInstanceState.containsKey(OUT_STATE_SELECTED_CURRENCY_POSITION)) { currencyCode = currenciesList.get(savedInstanceState.getInt(OUT_STATE_SELECTED_CURRENCY_POSITION)).toString(); } else if (lastSelectedCurrencyCodeIndex >= 0) { currencyCode = currenciesList.get(lastSelectedCurrencyCodeIndex).toString(); } else { currencyCode = defaultCurrencyCodeSupplier.get(); } final int currencyPosition = currenciesList.indexOf(currencyCode); if (currencyPosition >= 0) { return currencyPosition; } else { return 0; } }) .subscribe(view.displayCurrencySelection())); this.compositeDisposable.add(currenciesConnectableObservable .flatMap(currenciesList -> { return view.currencyClicks() .filter(currencyIndex -> currencyIndex >= 0); }) .doOnNext(currencyIndex -> lastSelectedCurrencyCodeIndex = currencyIndex) .subscribe(view.displayCurrencySelection())); this.compositeDisposable.add(currenciesConnectableObservable.connect()); } CurrencyListEditorPresenter(@NonNull CurrencyListEditorView view,
                                       @NonNull DatabaseHelper databaseHelper,
                                       @NonNull CurrencyCodeSupplier defaultCurrencyCodeSupplier,
                                       @Nullable Bundle savedInstanceState); @VisibleForTesting  CurrencyListEditorPresenter(@NonNull CurrencyListEditorView view,
                                @NonNull DatabaseHelper databaseHelper,
                                @NonNull CurrencyCodeSupplier defaultCurrencyCodeSupplier,
                                @Nullable Bundle savedInstanceState,
                                @NonNull Scheduler subscribeOnScheduler,
                                @NonNull Scheduler observeOnScheduler); @Override @CallSuper void subscribe(); void onSaveInstanceState(@NonNull Bundle outState);  }
@Test public void subscribeDisplaysCurrenciesListForNullState() throws Exception { nullSavedStatePresenter.subscribe(); verify(displayCurrenciesConsumer).accept(CURRENCIES); }
@Test public void subscribeDisplaysCurrenciesListForSavedState() throws Exception { savedStatePresenter.subscribe(); verify(displayCurrenciesConsumer).accept(CURRENCIES); }
@Test public void subscribeDisplaysCurrencySelectionForNullState() throws Exception { nullSavedStatePresenter.subscribe(); verify(displayCurrencySelectionConsumer).accept(0); }
@Test public void subscribeDisplaysCurrencySelectionNonNullStateWithoutExtra() throws Exception { savedStatePresenter.subscribe(); verify(displayCurrencySelectionConsumer).accept(0); }
@Test public void subscribeListensToClicksAndUpdatesSelection() throws Exception { nullSavedStatePresenter.subscribe(); currencyClicks.onNext(1); verify(displayCurrencySelectionConsumer).accept(0); verify(displayCurrencySelectionConsumer).accept(1); }
@Test public void subscribeUnsubscribeAndReSubscribeTracksLastCurrencyWhenNotSavingState() throws Exception { savedStatePresenter.subscribe(); verify(displayCurrenciesConsumer).accept(CURRENCIES); verify(displayCurrencySelectionConsumer).accept(0); currencyClicks.onNext(1); verify(displayCurrencySelectionConsumer).accept(1); savedStatePresenter.unsubscribe(); savedStatePresenter.subscribe(); verify(displayCurrenciesConsumer, times(2)).accept(CURRENCIES); verify(displayCurrencySelectionConsumer, times(2)).accept(1); }