-
Notifications
You must be signed in to change notification settings - Fork 994
/
BaseActivity.java
271 lines (232 loc) · 8.12 KB
/
BaseActivity.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
package com.kickstarter.libs;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.AnimRes;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Pair;
import com.kickstarter.ApplicationComponent;
import com.kickstarter.KSApplication;
import com.kickstarter.libs.qualifiers.RequiresActivityViewModel;
import com.kickstarter.libs.utils.BundleUtils;
import com.kickstarter.ui.data.ActivityResult;
import com.trello.rxlifecycle.ActivityEvent;
import com.trello.rxlifecycle.RxLifecycle;
import com.trello.rxlifecycle.components.ActivityLifecycleProvider;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.subjects.BehaviorSubject;
import rx.subjects.PublishSubject;
import rx.subscriptions.CompositeSubscription;
import timber.log.Timber;
public abstract class BaseActivity<ViewModelType extends ActivityViewModel> extends AppCompatActivity implements ActivityLifecycleProvider,
ActivityLifecycleType {
private final PublishSubject<Void> back = PublishSubject.create();
private final BehaviorSubject<ActivityEvent> lifecycle = BehaviorSubject.create();
private static final String VIEW_MODEL_KEY = "viewModel";
private final CompositeSubscription subscriptions = new CompositeSubscription();
protected ViewModelType viewModel;
/**
* Get viewModel.
*/
public ViewModelType viewModel() {
return viewModel;
}
/**
* Returns an observable of the activity's lifecycle events.
*/
public final Observable<ActivityEvent> lifecycle() {
return lifecycle.asObservable();
}
/**
* Completes an observable when an {@link ActivityEvent} occurs in the activity's lifecycle.
*/
public final <T> Observable.Transformer<T, T> bindUntilEvent(final ActivityEvent event) {
return RxLifecycle.bindUntilActivityEvent(lifecycle, event);
}
/**
* Completes an observable when the lifecycle event opposing the current lifecyle event is emitted.
* For example, if a subscription is made during {@link ActivityEvent#CREATE}, the observable will be completed
* in {@link ActivityEvent#DESTROY}.
*/
public final <T> Observable.Transformer<T, T> bindToLifecycle() {
return RxLifecycle.bindActivity(lifecycle);
}
/**
* Sends activity result data to the view model.
*/
@CallSuper
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final @Nullable Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
viewModel.activityResult(ActivityResult.create(requestCode, resultCode, intent));
}
@CallSuper
@Override
protected void onCreate(final @Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Timber.d("onCreate %s", this.toString());
lifecycle.onNext(ActivityEvent.CREATE);
assignViewModel(savedInstanceState);
viewModel.intent(getIntent());
}
/**
* Called when an activity is set to `singleTop` and it is relaunched while at the top of the activity stack.
*/
@CallSuper
@Override
protected void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
viewModel.intent(intent);
}
@CallSuper
@Override
protected void onStart() {
super.onStart();
Timber.d("onStart %s", this.toString());
lifecycle.onNext(ActivityEvent.START);
back
.compose(bindUntilEvent(ActivityEvent.STOP))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(__ -> goBack());
}
@CallSuper
@Override
protected void onResume() {
super.onResume();
Timber.d("onResume %s", this.toString());
lifecycle.onNext(ActivityEvent.RESUME);
assignViewModel(null);
if (viewModel != null) {
viewModel.onResume(this);
}
}
@CallSuper
@Override
protected void onPause() {
lifecycle.onNext(ActivityEvent.PAUSE);
super.onPause();
Timber.d("onPause %s", this.toString());
if (viewModel != null) {
viewModel.onPause();
}
}
@CallSuper
@Override
protected void onStop() {
lifecycle.onNext(ActivityEvent.STOP);
super.onStop();
Timber.d("onStop %s", this.toString());
}
@CallSuper
@Override
protected void onDestroy() {
lifecycle.onNext(ActivityEvent.DESTROY);
super.onDestroy();
Timber.d("onDestroy %s", this.toString());
subscriptions.clear();
if (isFinishing()) {
if (viewModel != null) {
ActivityViewModelManager.getInstance().destroy(viewModel);
viewModel = null;
}
}
}
/**
* @deprecated Use {@link #back()} instead.
*
* In rare situations, onBackPressed can be triggered after {@link #onSaveInstanceState(Bundle)} has been called.
* This causes an {@link IllegalStateException} in the fragment manager's `checkStateLoss` method, because the
* UI state has changed after being saved. The sequence of events might look like this:
*
* onSaveInstanceState -> onStop -> onBackPressed
*
* To avoid that situation, we need to ignore calls to `onBackPressed` after the activity has been saved. Since
* the activity is stopped after `onSaveInstanceState` is called, we can create an observable of back events,
* and a subscription that calls super.onBackPressed() only when the activity has not been stopped.
*/
@CallSuper
@Override
@Deprecated
public void onBackPressed() {
back();
}
/**
* Call when the user wants triggers a back event, e.g. clicking back in a toolbar or pressing the device back button.
*/
public void back() {
back.onNext(null);
}
/**
* Override in subclasses for custom exit transitions. First item in pair is the enter animation,
* second item in pair is the exit animation.
*/
protected @Nullable Pair<Integer, Integer> exitTransition() {
return null;
}
@CallSuper
@Override
protected void onSaveInstanceState(final @NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Timber.d("onSaveInstanceState %s", this.toString());
final Bundle viewModelEnvelope = new Bundle();
if (viewModel != null) {
ActivityViewModelManager.getInstance().save(viewModel, viewModelEnvelope);
}
outState.putBundle(VIEW_MODEL_KEY, viewModelEnvelope);
}
protected final void startActivityWithTransition(final @NonNull Intent intent, final @AnimRes int enterAnim,
final @AnimRes int exitAnim) {
startActivity(intent);
overridePendingTransition(enterAnim, exitAnim);
}
/**
* Returns the {@link KSApplication} instance.
*/
protected @NonNull KSApplication application() {
return (KSApplication) getApplication();
}
/**
* Convenience method to return a Dagger component.
*/
protected @NonNull ApplicationComponent component() {
return application().component();
}
/**
* Returns the application's {@link Environment}.
*/
protected @NonNull Environment environment() {
return component().environment();
}
/**
* @deprecated Use {@link #bindToLifecycle()} or {@link #bindUntilEvent(ActivityEvent)} instead.
*/
@Deprecated
protected final void addSubscription(final @NonNull Subscription subscription) {
subscriptions.add(subscription);
}
/**
* Triggers a back press with an optional transition.
*/
private void goBack() {
super.onBackPressed();
final Pair<Integer, Integer> exitTransitions = exitTransition();
if (exitTransitions != null) {
overridePendingTransition(exitTransitions.first, exitTransitions.second);
}
}
private void assignViewModel(final @Nullable Bundle viewModelEnvelope) {
if (viewModel == null) {
final RequiresActivityViewModel annotation = getClass().getAnnotation(RequiresActivityViewModel.class);
final Class<ViewModelType> viewModelClass = annotation == null ? null : (Class<ViewModelType>) annotation.value();
if (viewModelClass != null) {
viewModel = ActivityViewModelManager.getInstance().fetch(this,
viewModelClass,
BundleUtils.maybeGetBundle(viewModelEnvelope, VIEW_MODEL_KEY));
}
}
}
}