-
Notifications
You must be signed in to change notification settings - Fork 841
/
ViewGroupMviDelegateImpl.java
283 lines (239 loc) · 8.76 KB
/
ViewGroupMviDelegateImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright 2016 Hannes Dorfmann.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.hannesdorfmann.mosby3;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import com.hannesdorfmann.mosby3.mvi.MviPresenter;
import com.hannesdorfmann.mosby3.mvp.MvpView;
import java.util.UUID;
/**
* The default implementation of {@link ViewGroupMviDelegate}
*
* @author Hannes Dorfmann
* @see ViewGroupMviDelegate
* @since 3.0.0
*/
public class ViewGroupMviDelegateImpl<V extends MvpView, P extends MviPresenter<V, ?>>
implements ViewGroupMviDelegate<V, P>, Application.ActivityLifecycleCallbacks {
// TODO allow custom save state hook in
public static boolean DEBUG = false;
private static final String DEBUG_TAG = "ViewGroupMviDelegateImp";
private ViewGroupMviDelegateCallback<V, P> delegateCallback;
private String mosbyViewId;
private final boolean keepPresenterDuringScreenOrientationChange;
private final boolean isInEditMode;
private P presenter;
private final Activity activity;
private boolean checkedActivityFinishing = false;
private boolean presenterDetached = false;
private boolean presenterDestroeyed = false;
public ViewGroupMviDelegateImpl(@NonNull View view,
@NonNull ViewGroupMviDelegateCallback<V, P> delegateCallback,
boolean keepPresenterDuringScreenOrientationChange) {
if (view == null) {
throw new NullPointerException("View is null!");
}
if (delegateCallback == null) {
throw new NullPointerException("MvpDelegateCallback is null!");
}
this.delegateCallback = delegateCallback;
this.keepPresenterDuringScreenOrientationChange = keepPresenterDuringScreenOrientationChange;
this.isInEditMode = view.isInEditMode();
if (!isInEditMode) {
this.activity = PresenterManager.getActivity(delegateCallback.getContext());
this.activity.getApplication().registerActivityLifecycleCallbacks(this);
} else {
this.activity = null;
}
}
/**
* Generates the unique (mosby internal) viewState id and calls {@link
* MviDelegateCallback#createPresenter()}
* to create a new presenter instance
*
* @return The new created presenter instance
*/
private P createViewIdAndCreatePresenter() {
P presenter = delegateCallback.createPresenter();
if (presenter == null) {
throw new NullPointerException("Presenter returned from createPresenter() is null.");
}
if (keepPresenterDuringScreenOrientationChange) {
Context context = delegateCallback.getContext();
mosbyViewId = UUID.randomUUID().toString();
PresenterManager.putPresenter(PresenterManager.getActivity(context), mosbyViewId, presenter);
}
return presenter;
}
@NonNull private Context getContext() {
Context c = delegateCallback.getContext();
if (c == null) {
throw new NullPointerException("Context returned from " + delegateCallback + " is null");
}
return c;
}
@Override public void onAttachedToWindow() {
if (isInEditMode) return;
boolean viewStateWillBeRestored = false;
if (mosbyViewId == null) {
// No presenter available,
// Activity is starting for the first time (or keepPresenterInstance == false)
presenter = createViewIdAndCreatePresenter();
if (DEBUG) {
Log.d(DEBUG_TAG, "new Presenter instance created: " + presenter);
}
} else {
Context context = getContext();
presenter = PresenterManager.getPresenter(activity, mosbyViewId);
if (presenter == null) {
// Process death,
// hence no presenter with the given viewState id stored, although we have a viewState id
presenter = createViewIdAndCreatePresenter();
if (DEBUG) {
Log.d(DEBUG_TAG,
"No Presenter instance found in cache, although MosbyView ID present. This was caused by process death, therefore new Presenter instance created: "
+ presenter);
}
} else {
viewStateWillBeRestored = true;
if (DEBUG) {
Log.d(DEBUG_TAG, "Presenter instance reused from internal cache: " + presenter);
}
}
}
// presenter is ready, so attach viewState
V view = delegateCallback.getMvpView();
if (view == null) {
throw new NullPointerException(
"MvpView returned from getMvpView() is null. Returned by " + delegateCallback);
}
if (viewStateWillBeRestored) {
delegateCallback.setRestoringViewState(true);
}
presenter.attachView(view);
if (viewStateWillBeRestored) {
delegateCallback.setRestoringViewState(false);
}
if (DEBUG) {
Log.d(DEBUG_TAG,
"MvpView attached to Presenter. MvpView: " + view + " Presenter: " + presenter);
}
}
@Override public void onDetachedFromWindow() {
if (isInEditMode) return;
detachPresenterIfNotDoneYet();
if (!checkedActivityFinishing) {
boolean destroyPermanently = !ActivityMviDelegateImpl.retainPresenterInstance(
keepPresenterDuringScreenOrientationChange, activity);
if (destroyPermanently) {
destroyPresenterIfnotDoneYet();
} else if (!activity.isFinishing()) {
// View removed manually from screen
destroyPresenterIfnotDoneYet();
}
} // else --> see onActivityDestroyed()
}
/**
* Must be called from {@link View#onSaveInstanceState()}
*/
public Parcelable onSaveInstanceState() {
if (isInEditMode) return null;
Parcelable superState = delegateCallback.superOnSaveInstanceState();
if (keepPresenterDuringScreenOrientationChange) {
return new MosbySavedState(superState, mosbyViewId);
} else {
return superState;
}
}
/**
* Restore the data from SavedState
*
* @param state The state to read data from
*/
private void restoreSavedState(MosbySavedState state) {
mosbyViewId = state.getMosbyViewId();
}
/**
* Must be called from {@link View#onRestoreInstanceState(Parcelable)}
*/
public void onRestoreInstanceState(Parcelable state) {
if (isInEditMode) return;
if (!(state instanceof MosbySavedState)) {
delegateCallback.superOnRestoreInstanceState(state);
return;
}
MosbySavedState savedState = (MosbySavedState) state;
restoreSavedState(savedState);
delegateCallback.superOnRestoreInstanceState(savedState.getSuperState());
}
@Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
@Override public void onActivityStarted(Activity activity) {
}
@Override public void onActivityResumed(Activity activity) {
}
@Override public void onActivityPaused(Activity activity) {
}
@Override public void onActivityStopped(Activity activity) {
}
@Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override public void onActivityDestroyed(Activity activity) {
if (activity == this.activity) {
// The hosting activity of this view has been destroyed, so time to destoryed the presenter too?
activity.getApplication().unregisterActivityLifecycleCallbacks(this);
checkedActivityFinishing = true;
boolean destroyedPermanently = !ActivityMviDelegateImpl.retainPresenterInstance(
keepPresenterDuringScreenOrientationChange, activity);
if (destroyedPermanently) {
// Whole activity will be destroyed
detachPresenterIfNotDoneYet();
destroyPresenterIfnotDoneYet();
}
}
}
private void destroyPresenterIfnotDoneYet() {
if (!presenterDestroeyed) {
presenter.destroy();
presenterDestroeyed = true;
if (DEBUG) {
Log.d(DEBUG_TAG, "Presenter destroyed: " + presenter);
}
if (mosbyViewId != null) {
// mosbyViewId == null if keepPresenterDuringScreenOrientationChange == false
PresenterManager.remove(activity, mosbyViewId);
}
mosbyViewId = null;
}
}
private void detachPresenterIfNotDoneYet() {
if (!presenterDetached) {
presenter.detachView();
presenterDetached = true;
if (DEBUG) {
Log.d(DEBUG_TAG,
"view " + delegateCallback.getMvpView() + " detached from Presenter " + presenter);
}
}
}
}