Skip to content

Commit

Permalink
Fixing Reusing of a singleton presenter; Fixes sockeqwe#199 and maybe s…
Browse files Browse the repository at this point in the history
  • Loading branch information
sockeqwe committed Feb 2, 2017
1 parent 9b5775c commit 42efc4d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@
* to other components you can make this method public.
* </p>
*
* <p>
* <b>Please note that you should not reuse a MviBasePresenter once the View who originally has
* instantiated this Presenter has been destroyed permanently</b>. App wide singletons for
* Presenters is not a good idea in Model-View-Intent. Reusing singleton scoped Presenters for
* different view instances may cause emitting the previous state of the previous attached view
* (which already has been destroyed permanently).
* </p>
*
* @param <V> The type of the viewState this presenter responds to
* @param <VS> The type of the viewState state
* @author Hannes Dorfmann
Expand Down Expand Up @@ -143,7 +151,7 @@ public IntentRelayBinderPair(PublishSubject<I> intentRelaySubject,
* List of internal relays, bridging the gap between intents coming from the viewState (will be
* unsubscribed temporarly when viewState is detached i.e. during config changes)
*/
private List<IntentRelayBinderPair<?>> intentRelays = new ArrayList<>(4);
private List<IntentRelayBinderPair<?>> intentRelaysBinders = new ArrayList<>(4);

/**
* Composite Desposals holding subscriptions to all intents observable offered by the viewState.
Expand Down Expand Up @@ -278,9 +286,9 @@ protected Observable<VS> getViewStateObservable() {
if (viewAttachedFirstTime) {
bindIntents();
}
int intentsSize = intentRelays.size();
int intentsSize = intentRelaysBinders.size();
for (int i = 0; i < intentsSize; i++) {
IntentRelayBinderPair<?> intentRelayBinderPair = intentRelays.get(i);
IntentRelayBinderPair<?> intentRelayBinderPair = intentRelaysBinders.get(i);
bindIntentActually(view, intentRelayBinderPair);
}

Expand All @@ -299,6 +307,9 @@ protected Observable<VS> getViewStateObservable() {
}

unbindIntents();
viewAttachedFirstTime = true;
intentRelaysBinders.clear();
// TODO should we re emit the inital state? What if no initial state has been set.
}

if (viewRelayConsumerDisposable != null) {
Expand Down Expand Up @@ -363,7 +374,7 @@ protected void unbindIntents() {
*/
@MainThread protected <I> Observable<I> intent(ViewIntentBinder<V, I> binder) {
PublishSubject<I> intentRelay = PublishSubject.create();
intentRelays.add(new IntentRelayBinderPair<I>(intentRelay, binder));
intentRelaysBinders.add(new IntentRelayBinderPair<I>(intentRelay, binder));
return intentRelay;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ public static RefWatcher getRefWatcher(Context context) {
return;
}
refWatcher = LeakCanary.install(this);
Timber.d("Starting Application");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ public MainMenuPresenter(ProductBackendApiDecorator backendApi) {

@Override protected void bindIntents() {

Observable<List<String>> loadCategories = intent(MainMenuView::loadCategoriesIntent)
.doOnNext(categoryName -> Timber.d("intent: load category %s", categoryName))
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);
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);
Expand Down

0 comments on commit 42efc4d

Please sign in to comment.