-
Notifications
You must be signed in to change notification settings - Fork 22
/
MvvmActivity.kt
358 lines (304 loc) · 10.5 KB
/
MvvmActivity.kt
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Copyright 2018 Arthur Ivanets, arthur.ivanets.work@gmail.com
*
* 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.arthurivanets.mvvm
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.annotation.CallSuper
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.Observable
import androidx.databinding.ObservableField
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Observer
import com.arthurivanets.mvvm.markers.*
import com.arthurivanets.mvvm.util.currentFragment
import com.arthurivanets.mvvm.util.handleBackPressEvent
import com.arthurivanets.mvvm.util.handleNewIntent
import com.arthurivanets.mvvm.util.onPropertyChanged
/**
* A base MVVM Activity to be used in conjunction with the concrete implementations of the [BaseViewModel].
*/
abstract class MvvmActivity<VDB : ViewDataBinding, VM : BaseViewModel>(
@LayoutRes
private val layoutId: Int
) : AppCompatActivity(), CanFetchExtras, CanHandleNewIntent {
/**
* Retrieves the id of the Data Binding variable.
* (This id should correspond to the id of the ViewModel
* variable defined in your xml layout file)
*
* @return the binding variable id
*/
protected open val bindingVariable: Int = 0
var extrasBundle = Bundle()
private set
private var viewDataBinding: VDB? = null
private var viewModel: VM? = null
private val registeredObservables = HashSet<Pair<Observable.OnPropertyChangedCallback, Observable>>()
/**
* Override this property in order to enable/disable
* the DataBinding for the Activity.
*/
protected open val isDataBindingEnabled = true
final override fun onCreate(savedInstanceState: Bundle?) {
injectDependencies()
intent?.extras?.let(::fetchExtras)
preInit()
super.onCreate(savedInstanceState)
initView()
init(savedInstanceState)
postInit()
performDataBinding()
subscribeViewStateObservers()
onRegisterObservables()
onBind()
}
final override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
intent?.let(::handleNewIntent)
}
/**
* Gets called when it's the right time for you to inject the dependencies.
*/
open fun injectDependencies() {
//
}
/**
* Gets called right before the pre-initialization stage ([preInit] method call),
* if the received [Bundle] is not null.
*
* @param extras the bundle of arguments
*/
@CallSuper
override fun fetchExtras(extras: Bundle) {
extrasBundle = extras
}
/**
* Gets called whenever the current [AppCompatActivity] receives the new [Intent]
* through the [AppCompatActivity.onNewIntent] method.
*
* @param intent the new message [Intent] (see: [android.app.Activity.onNewIntent])
*/
@CallSuper
override fun handleNewIntent(intent: Intent) {
supportFragmentManager.currentFragment?.handleNewIntent(intent)
}
/**
* Gets called right before the UI initialization.
*/
protected open fun preInit() {
//
}
/**
* Get's called when it's the right time for you to initialize the UI elements.
*
* @param savedInstanceState state bundle brought from the [android.app.Activity.onCreate]
*/
protected open fun init(savedInstanceState: Bundle?) {
//
}
/**
* Gets called right after the UI initialization.
*/
protected open fun postInit() {
//
}
private fun initView() {
if (isDataBindingEnabled) {
viewDataBinding = (viewDataBinding ?: DataBindingUtil.setContentView(this, layoutId))
} else {
setContentView(layoutId)
}
viewModel = (viewModel ?: createViewModel())
if (isDataBindingEnabled) {
viewDataBinding?.setVariable(bindingVariable, viewModel)
viewDataBinding?.lifecycleOwner = this
}
}
/**
* Creates the concrete version of the [BaseViewModel].
*
* @return the created concrete version of the [BaseViewModel]
*/
protected abstract fun createViewModel(): VM
private fun performDataBinding() {
if (isDataBindingEnabled) {
viewDataBinding?.executePendingBindings()
} else {
Log.i(this::class.java.canonicalName, "The DataBinding is disabled for this Activity.")
}
}
/**
* Override this lifecycle method if you need to perform the manual view-state specific binding.
*/
protected open fun onBind() {
// to be overridden.
}
@CallSuper
override fun onResume() {
super.onResume()
attachViewModelEventConsumers()
viewModel?.onStart()
}
/**
* Gets called when it's the right time to register the [ObservableField]s of your [androidx.lifecycle.ViewModel].
*/
protected open fun onRegisterObservables() {
//
}
@CallSuper
override fun onPause() {
super.onPause()
viewModel?.onStop()
detachViewModelEventConsumers()
}
@CallSuper
override fun onBackPressed() {
val isConsumedByViewModel = (viewModel?.onBackPressed() ?: false)
if (!handleBackPressEvent() && !isConsumedByViewModel) {
super.onBackPressed()
}
}
private fun handleBackPressEvent(): Boolean {
return supportFragmentManager.fragments.handleBackPressEvent()
}
final override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
savedInstanceState.let(::onRestoreStateInternal)
}
private fun onRestoreStateInternal(stateBundle: Bundle) {
viewModel?.onRestoreState(stateBundle)
onRestoreState(stateBundle)
}
/**
* Gets called whenever it's the right time to restore the previously stored state.
*
* @param stateBundle the previously store state
*/
open fun onRestoreState(stateBundle: Bundle) {
//
}
final override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.let(::onSaveStateInternal)
}
private fun onSaveStateInternal(stateBundle: Bundle) {
viewModel?.onSaveState(stateBundle)
onSaveState(stateBundle)
}
/**
* Gets called whenever it's the right time to save the state.
*
* @param stateBundle the bundle the state is to be saved into
*/
open fun onSaveState(stateBundle: Bundle) {
//
}
final override fun onDestroy() {
unregisterFields()
onUnbind()
onRecycle()
super.onDestroy()
}
/**
* Override this method if you need to manually unbind the previously bound view-state specific observers.
*/
protected open fun onUnbind() {
// to be overridden.
}
/**
* Gets called right before the destruction of the [android.app.Activity] (see: [android.app.Activity.onDestroy]).
*/
protected open fun onRecycle() {
// to be overridden
}
/**
* Gets called whenever the new [Command] arrives from the [BaseViewModel].
*
* Override this method to provide the handling of the ViewModel-specific commands.
*
* @param command the newly arrived [Command]
*/
@CallSuper
protected open fun onHandleCommand(command: Command) {
// to be overridden
}
/**
* Gets called whenever the [ViewState] change event arrives from the [BaseViewModel].
*
* Override this method to provide the handling of the UI state changes
* based on the emitted [ViewState] change events.
*
* @param state the new [ViewState]
*/
protected open fun onViewStateChanged(state: ViewState) {
// to be overridden
}
/**
* Gets called whenever the new [Route] event arrives from the [BaseViewModel].
*
* Override this method to provide the handling of the navigation between the application screens
* based on the emitted [Route] events.
*
* @param route the newly arrived [Route]
*/
protected open fun onRoute(route: Route) {
// to be overridden
}
private fun subscribeViewStateObservers() {
viewModel?.viewStateHolder?.observe(this, Observer(::onViewStateChanged))
}
private fun attachViewModelEventConsumers() {
viewModel?.apply {
commandChannel.consumer = ::onHandleCommand
routeChannel.consumer = ::onRoute
}
}
private fun detachViewModelEventConsumers() {
viewModel?.apply {
commandChannel.consumer = null
routeChannel.consumer = null
}
}
private fun unregisterFields() {
registeredObservables.forEach { (callback, field) -> field.removeOnPropertyChangedCallback(callback) }
registeredObservables.clear()
}
/**
* Adds the specified [Observable.OnPropertyChangedCallback] to the registry of Lifecycle-aware Callbacks.
* <br>
* [Observable.OnPropertyChangedCallback]s are automatically disposed whenever the
* [android.app.Activity.onPause] method is called.
*
* @param observable the [Observable] the [Observable.OnPropertyChangedCallback] is registered to
*/
protected fun Observable.OnPropertyChangedCallback.manageLifecycle(observable: Observable) {
registeredObservables.add(Pair(this, observable))
}
/**
* Registers the value change callback to the specified [ObservableField].
* <br>
* The lifecycle of the registered callbacks is managed internally (see: [manageLifecycle]),
* so you don't have to do any manual unregistering yourself.
*
* @param callback value change callback
*/
protected inline fun <T : ObservableField<R>, R : Any> T.register(crossinline callback: (R) -> Unit) {
this.onPropertyChanged { it.get()?.let(callback) }.manageLifecycle(this)
}
}