Skip to content

Commit

Permalink
Presenter now has destroy() and detach() method. Deprecating Presente…
Browse files Browse the repository at this point in the history
…r.detach(boolean retainInstance) #262 #271 #272
  • Loading branch information
sockeqwe committed Sep 4, 2017
1 parent 417fcab commit 4d77b36
Show file tree
Hide file tree
Showing 17 changed files with 222 additions and 144 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ buildscript {
}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.android.tools.build:gradle:3.0.0-beta4'
// classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.5.2'
classpath 'me.tatarka:gradle-retrolambda:3.5.0'
//classpath 'me.tatarka:gradle-retrolambda:3.5.0'
}
}

Expand Down Expand Up @@ -40,7 +40,7 @@ allprojects {
ext {
minSdk = 14
targetSdk = 25
buildToolsVersion = '26.0.0'
buildToolsVersion = '26.0.1'
compileSdkVersion = 25

javaSourceCompatibility = JavaVersion.VERSION_1_7
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ POM_LICENCE_NAME=The Apache Software License, Version 2.0
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=hannesdorfmann
POM_DEVELOPER_NAME=Hannes Dorfmann
POM_DEVELOPER_NAME=Hannes Dorfmann

android.enableAapt2=false
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
* viewState is a object (typically a POJO) that holds all the data the view needs to display</li>
* </ul>
*
* By using {@link #intent(ViewIntentBinder)} and {@link #subscribeViewState(Observable, * ViewStateConsumer)}
* By using {@link #intent(ViewIntentBinder)} and {@link #subscribeViewState(Observable, *
* ViewStateConsumer)}
* a relay will be established between the view and this presenter that allows the view to be
* temporarily detached, without unsubscribing the underlying reactive business logic workflow and
* without causing memory leaks (caused by recreation of the view).
Expand Down Expand Up @@ -117,7 +118,8 @@ protected interface ViewIntentBinder<V extends MvpView, I> {
* This "binder" is responsible to bind the view state to the currently attached view.
* This typically "renders" the view.
*
* Typically this is used in {@link #bindIntents()} with {@link MviBasePresenter#subscribeViewState(Observable, * ViewStateConsumer)}
* Typically this is used in {@link #bindIntents()} with {@link MviBasePresenter#subscribeViewState(Observable,
* * ViewStateConsumer)}
* like this:
* <pre><code>
* Observable<MyViewState> viewState = ... ;
Expand Down Expand Up @@ -265,23 +267,11 @@ protected Observable<VS> getViewStateObservable() {
bindIntentActually(view, intentRelayBinderPair);
}


viewAttachedFirstTime = false;
}

@Override @CallSuper public void detachView(boolean retainInstance) {
if (!retainInstance) {
if (viewStateDisposable != null) {
// Cancel the overall observable stream
viewStateDisposable.dispose();
}

unbindIntents();
reset();
// TODO should we re emit the inital state? What if no initial state has been set.
// TODO should we rather throw an exception if presenter is reused after view has been detached permanently
}

@Override @CallSuper public void detachView() {
detachView(true);
if (viewRelayConsumerDisposable != null) {
// Cancel subscription from View to viewState Relay
viewRelayConsumerDisposable.dispose();
Expand All @@ -295,6 +285,26 @@ protected Observable<VS> getViewStateObservable() {
}
}

@Override @CallSuper public void destroy() {
detachView(false);
if (viewStateDisposable != null) {
// Cancel the overall observable stream
viewStateDisposable.dispose();
}

unbindIntents();
reset();
// TODO should we re emit the inital state? What if no initial state has been set.
// TODO should we rather throw an exception if presenter is reused after view has been detached permanently

}

/**
* {@inheritDoc}
*/
@Deprecated @Override @CallSuper public void detachView(boolean retainInstance) {
}

/**
* This is called when the View has been detached permantently (view is destroyed permanently)
* to reset the internal state of this Presenter to be ready for being reused (even thought
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class FirstPresenter extends MviBasePresenter<FirstView, Object> {
public AtomicInteger unbindIntentCalls = new AtomicInteger(0);
public AtomicInteger attachViewCalls = new AtomicInteger(0);
public AtomicInteger detachViewCalls = new AtomicInteger(0);
public AtomicInteger destoryCalls = new AtomicInteger(0);

@Override protected void bindIntents() {
bindIntentCalls.incrementAndGet();
Expand All @@ -47,9 +48,14 @@ public class FirstPresenter extends MviBasePresenter<FirstView, Object> {
attachViewCalls.incrementAndGet();
}

@Override public void detachView(boolean retainInstance) {
super.detachView(retainInstance);
Log.d("Presenters", "First Retain Presenter "+retainInstance);
@Override public void detachView() {
super.detachView();
Log.d("Presenters", "First Retain Presenter detachView");
detachViewCalls.incrementAndGet();
}

@Override public void destroy() {
super.destroy();
destoryCalls.incrementAndGet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class SecondPresenter extends MviBasePresenter<SecondView, Object> {
public AtomicInteger unbindIntentCalls = new AtomicInteger(0);
public AtomicInteger attachViewCalls = new AtomicInteger(0);
public AtomicInteger detachViewCalls = new AtomicInteger(0);
public AtomicInteger destroyCalls = new AtomicInteger(0);


@Override protected void bindIntents() {
bindIntentCalls.incrementAndGet();
Expand All @@ -47,9 +49,14 @@ public class SecondPresenter extends MviBasePresenter<SecondView, Object> {
attachViewCalls.incrementAndGet();
}

@Override public void detachView(boolean retainInstance) {
super.detachView(retainInstance);
Log.d("Presenters", "SecondPresenter Retain Presenter "+retainInstance);
@Override public void detachView() {
super.detachView();
Log.d("Presenters", "SecondPresenter detachView Presenter");
detachViewCalls.incrementAndGet();
}

@Override public void destroy() {
super.destroy();
destroyCalls.incrementAndGet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class LifecycleTestPresenter extends MviBasePresenter<LifecycleTestView,
Log.d(getClass().getSimpleName(), "attachView " + attachViewInvokations + " " + attachedView);
}

// TODO replace with not deprecated
@Override public void detachView(boolean retainInstance) {
super.detachView(retainInstance);
attachedView = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ static boolean retainPresenterInstance(boolean keepPresenterInstance, Activity a
}

@Override public void onStop() {
boolean retainPresenterInstance = retainPresenterInstance(keepPresenterInstance, activity);
presenter.detachView(retainPresenterInstance);
presenter.detachView();

if (DEBUG) {
Log.d(DEBUG_TAG, "detached MvpView from Presenter. MvpView "
Expand All @@ -204,15 +203,20 @@ static boolean retainPresenterInstance(boolean keepPresenterInstance, Activity a
+ presenter);
}


}

@Override public void onDestroy() {

boolean retainPresenterInstance = retainPresenterInstance(keepPresenterInstance, activity);
if (!retainPresenterInstance){
presenter.destroy();
if (mosbyViewId != null) { // mosbyViewId == null if keepPresenterInstance == false
PresenterManager.remove(activity, mosbyViewId);
}
Log.d(DEBUG_TAG, "Destroying Presenter permanently " + presenter);
}
}

@Override public void onDestroy() {
presenter = null;
activity = null;
delegateCallback = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,6 @@ private boolean retainPresenterInstance(boolean keepPresenterOnBackstack, Activi
return false;
}

boolean contains = fragment.getFragmentManager().getFragments().contains(fragment);

if (keepPresenterOnBackstack && BackstackAccessor.isFragmentOnBackStack(fragment)) {
return true;
}
Expand All @@ -193,26 +191,13 @@ private boolean retainPresenterInstance(boolean keepPresenterOnBackstack, Activi

@Override public void onStop() {

Activity activity = getActivity();
boolean retainPresenterInstance =
retainPresenterInstance(keepPresenterOnBackstack, activity, fragment);

presenter.detachView(retainPresenterInstance);
if (!retainPresenterInstance
&& mosbyViewId
!= null) { // mosbyViewId == null if keepPresenterDuringScreenOrientationChange == false
PresenterManager.remove(activity, mosbyViewId);
}
presenter.detachView();

if (DEBUG) {
Log.d(DEBUG_TAG, "detached MvpView from Presenter. MvpView "
+ delegateCallback.getMvpView()
+ " Presenter: "
+ presenter);
Log.d(DEBUG_TAG, "Retaining presenter instance: "
+ Boolean.toString(retainPresenterInstance)
+ " "
+ presenter);
}
}

Expand All @@ -229,6 +214,27 @@ private boolean retainPresenterInstance(boolean keepPresenterOnBackstack, Activi
}

@Override public void onDestroy() {

Activity activity = getActivity();
boolean retainPresenterInstance =
retainPresenterInstance(keepPresenterOnBackstack, activity, fragment);

if (!retainPresenterInstance) {
presenter.destroy();
if (mosbyViewId
!= null) { // mosbyViewId == null if keepPresenterDuringScreenOrientationChange == false
PresenterManager.remove(activity, mosbyViewId);
}
if (DEBUG) {
Log.d(DEBUG_TAG, "Presenter destroyed");
}
} else if (DEBUG) {
Log.d(DEBUG_TAG, "Retaining presenter instance: "
+ Boolean.toString(retainPresenterInstance)
+ " "
+ presenter);
}

presenter = null;
delegateCallback = null;
fragment = null;
Expand Down
Loading

0 comments on commit 4d77b36

Please sign in to comment.