Skip to content

Commit 4d77b36

Browse files
committed
Presenter now has destroy() and detach() method. Deprecating Presenter.detach(boolean retainInstance) #262 #271 #272
1 parent 417fcab commit 4d77b36

File tree

17 files changed

+222
-144
lines changed

17 files changed

+222
-144
lines changed

build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ buildscript {
99
}
1010
}
1111
dependencies {
12-
classpath 'com.android.tools.build:gradle:2.3.3'
12+
classpath 'com.android.tools.build:gradle:3.0.0-beta4'
1313
// classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.5.2'
14-
classpath 'me.tatarka:gradle-retrolambda:3.5.0'
14+
//classpath 'me.tatarka:gradle-retrolambda:3.5.0'
1515
}
1616
}
1717

@@ -40,7 +40,7 @@ allprojects {
4040
ext {
4141
minSdk = 14
4242
targetSdk = 25
43-
buildToolsVersion = '26.0.0'
43+
buildToolsVersion = '26.0.1'
4444
compileSdkVersion = 25
4545

4646
javaSourceCompatibility = JavaVersion.VERSION_1_7

gradle.properties

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ POM_LICENCE_NAME=The Apache Software License, Version 2.0
3131
POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
3232
POM_LICENCE_DIST=repo
3333
POM_DEVELOPER_ID=hannesdorfmann
34-
POM_DEVELOPER_NAME=Hannes Dorfmann
34+
POM_DEVELOPER_NAME=Hannes Dorfmann
35+
36+
android.enableAapt2=false

gradle/wrapper/gradle-wrapper.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

mvi-common/src/main/java/com/hannesdorfmann/mosby3/mvi/MviBasePresenter.java

+26-16
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
* viewState is a object (typically a POJO) that holds all the data the view needs to display</li>
6060
* </ul>
6161
*
62-
* By using {@link #intent(ViewIntentBinder)} and {@link #subscribeViewState(Observable, * ViewStateConsumer)}
62+
* By using {@link #intent(ViewIntentBinder)} and {@link #subscribeViewState(Observable, *
63+
* ViewStateConsumer)}
6364
* a relay will be established between the view and this presenter that allows the view to be
6465
* temporarily detached, without unsubscribing the underlying reactive business logic workflow and
6566
* without causing memory leaks (caused by recreation of the view).
@@ -117,7 +118,8 @@ protected interface ViewIntentBinder<V extends MvpView, I> {
117118
* This "binder" is responsible to bind the view state to the currently attached view.
118119
* This typically "renders" the view.
119120
*
120-
* Typically this is used in {@link #bindIntents()} with {@link MviBasePresenter#subscribeViewState(Observable, * ViewStateConsumer)}
121+
* Typically this is used in {@link #bindIntents()} with {@link MviBasePresenter#subscribeViewState(Observable,
122+
* * ViewStateConsumer)}
121123
* like this:
122124
* <pre><code>
123125
* Observable<MyViewState> viewState = ... ;
@@ -265,23 +267,11 @@ protected Observable<VS> getViewStateObservable() {
265267
bindIntentActually(view, intentRelayBinderPair);
266268
}
267269

268-
269270
viewAttachedFirstTime = false;
270271
}
271272

272-
@Override @CallSuper public void detachView(boolean retainInstance) {
273-
if (!retainInstance) {
274-
if (viewStateDisposable != null) {
275-
// Cancel the overall observable stream
276-
viewStateDisposable.dispose();
277-
}
278-
279-
unbindIntents();
280-
reset();
281-
// TODO should we re emit the inital state? What if no initial state has been set.
282-
// TODO should we rather throw an exception if presenter is reused after view has been detached permanently
283-
}
284-
273+
@Override @CallSuper public void detachView() {
274+
detachView(true);
285275
if (viewRelayConsumerDisposable != null) {
286276
// Cancel subscription from View to viewState Relay
287277
viewRelayConsumerDisposable.dispose();
@@ -295,6 +285,26 @@ protected Observable<VS> getViewStateObservable() {
295285
}
296286
}
297287

288+
@Override @CallSuper public void destroy() {
289+
detachView(false);
290+
if (viewStateDisposable != null) {
291+
// Cancel the overall observable stream
292+
viewStateDisposable.dispose();
293+
}
294+
295+
unbindIntents();
296+
reset();
297+
// TODO should we re emit the inital state? What if no initial state has been set.
298+
// TODO should we rather throw an exception if presenter is reused after view has been detached permanently
299+
300+
}
301+
302+
/**
303+
* {@inheritDoc}
304+
*/
305+
@Deprecated @Override @CallSuper public void detachView(boolean retainInstance) {
306+
}
307+
298308
/**
299309
* This is called when the View has been detached permantently (view is destroyed permanently)
300310
* to reset the internal state of this Presenter to be ready for being reused (even thought

mvi-integration-test/src/main/java/com/hannesdorfmann/mosby3/mvi/integrationtest/backstack/first/FirstPresenter.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class FirstPresenter extends MviBasePresenter<FirstView, Object> {
3232
public AtomicInteger unbindIntentCalls = new AtomicInteger(0);
3333
public AtomicInteger attachViewCalls = new AtomicInteger(0);
3434
public AtomicInteger detachViewCalls = new AtomicInteger(0);
35+
public AtomicInteger destoryCalls = new AtomicInteger(0);
3536

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

50-
@Override public void detachView(boolean retainInstance) {
51-
super.detachView(retainInstance);
52-
Log.d("Presenters", "First Retain Presenter "+retainInstance);
51+
@Override public void detachView() {
52+
super.detachView();
53+
Log.d("Presenters", "First Retain Presenter detachView");
5354
detachViewCalls.incrementAndGet();
5455
}
56+
57+
@Override public void destroy() {
58+
super.destroy();
59+
destoryCalls.incrementAndGet();
60+
}
5561
}

mvi-integration-test/src/main/java/com/hannesdorfmann/mosby3/mvi/integrationtest/backstack/second/SecondPresenter.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class SecondPresenter extends MviBasePresenter<SecondView, Object> {
3232
public AtomicInteger unbindIntentCalls = new AtomicInteger(0);
3333
public AtomicInteger attachViewCalls = new AtomicInteger(0);
3434
public AtomicInteger detachViewCalls = new AtomicInteger(0);
35+
public AtomicInteger destroyCalls = new AtomicInteger(0);
36+
3537

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

50-
@Override public void detachView(boolean retainInstance) {
51-
super.detachView(retainInstance);
52-
Log.d("Presenters", "SecondPresenter Retain Presenter "+retainInstance);
52+
@Override public void detachView() {
53+
super.detachView();
54+
Log.d("Presenters", "SecondPresenter detachView Presenter");
5355
detachViewCalls.incrementAndGet();
5456
}
57+
58+
@Override public void destroy() {
59+
super.destroy();
60+
destroyCalls.incrementAndGet();
61+
}
5562
}

mvi-integration-test/src/main/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/LifecycleTestPresenter.java

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class LifecycleTestPresenter extends MviBasePresenter<LifecycleTestView,
4040
Log.d(getClass().getSimpleName(), "attachView " + attachViewInvokations + " " + attachedView);
4141
}
4242

43+
// TODO replace with not deprecated
4344
@Override public void detachView(boolean retainInstance) {
4445
super.detachView(retainInstance);
4546
attachedView = null;

mvi/src/main/java/com/hannesdorfmann/mosby3/ActivityMviDelegateImpl.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ static boolean retainPresenterInstance(boolean keepPresenterInstance, Activity a
194194
}
195195

196196
@Override public void onStop() {
197-
boolean retainPresenterInstance = retainPresenterInstance(keepPresenterInstance, activity);
198-
presenter.detachView(retainPresenterInstance);
197+
presenter.detachView();
199198

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

206+
207+
}
208+
209+
@Override public void onDestroy() {
210+
211+
boolean retainPresenterInstance = retainPresenterInstance(keepPresenterInstance, activity);
207212
if (!retainPresenterInstance){
213+
presenter.destroy();
208214
if (mosbyViewId != null) { // mosbyViewId == null if keepPresenterInstance == false
209215
PresenterManager.remove(activity, mosbyViewId);
210216
}
211217
Log.d(DEBUG_TAG, "Destroying Presenter permanently " + presenter);
212218
}
213-
}
214219

215-
@Override public void onDestroy() {
216220
presenter = null;
217221
activity = null;
218222
delegateCallback = null;

mvi/src/main/java/com/hannesdorfmann/mosby3/FragmentMviDelegateImpl.java

+22-16
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ private boolean retainPresenterInstance(boolean keepPresenterOnBackstack, Activi
176176
return false;
177177
}
178178

179-
boolean contains = fragment.getFragmentManager().getFragments().contains(fragment);
180-
181179
if (keepPresenterOnBackstack && BackstackAccessor.isFragmentOnBackStack(fragment)) {
182180
return true;
183181
}
@@ -193,26 +191,13 @@ private boolean retainPresenterInstance(boolean keepPresenterOnBackstack, Activi
193191

194192
@Override public void onStop() {
195193

196-
Activity activity = getActivity();
197-
boolean retainPresenterInstance =
198-
retainPresenterInstance(keepPresenterOnBackstack, activity, fragment);
199-
200-
presenter.detachView(retainPresenterInstance);
201-
if (!retainPresenterInstance
202-
&& mosbyViewId
203-
!= null) { // mosbyViewId == null if keepPresenterDuringScreenOrientationChange == false
204-
PresenterManager.remove(activity, mosbyViewId);
205-
}
194+
presenter.detachView();
206195

207196
if (DEBUG) {
208197
Log.d(DEBUG_TAG, "detached MvpView from Presenter. MvpView "
209198
+ delegateCallback.getMvpView()
210199
+ " Presenter: "
211200
+ presenter);
212-
Log.d(DEBUG_TAG, "Retaining presenter instance: "
213-
+ Boolean.toString(retainPresenterInstance)
214-
+ " "
215-
+ presenter);
216201
}
217202
}
218203

@@ -229,6 +214,27 @@ private boolean retainPresenterInstance(boolean keepPresenterOnBackstack, Activi
229214
}
230215

231216
@Override public void onDestroy() {
217+
218+
Activity activity = getActivity();
219+
boolean retainPresenterInstance =
220+
retainPresenterInstance(keepPresenterOnBackstack, activity, fragment);
221+
222+
if (!retainPresenterInstance) {
223+
presenter.destroy();
224+
if (mosbyViewId
225+
!= null) { // mosbyViewId == null if keepPresenterDuringScreenOrientationChange == false
226+
PresenterManager.remove(activity, mosbyViewId);
227+
}
228+
if (DEBUG) {
229+
Log.d(DEBUG_TAG, "Presenter destroyed");
230+
}
231+
} else if (DEBUG) {
232+
Log.d(DEBUG_TAG, "Retaining presenter instance: "
233+
+ Boolean.toString(retainPresenterInstance)
234+
+ " "
235+
+ presenter);
236+
}
237+
232238
presenter = null;
233239
delegateCallback = null;
234240
fragment = null;

0 commit comments

Comments
 (0)