1
0
Fork 0
fenix/app/src/main/java/org/mozilla/fenix/mvi/UIComponent.kt

36 lines
1.2 KiB
Kotlin
Raw Normal View History

2019-01-28 17:46:39 +01:00
/* This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.mvi
import io.reactivex.Observable
2019-01-31 07:49:41 +01:00
import io.reactivex.Observer
2019-01-28 17:46:39 +01:00
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
2019-01-31 07:49:41 +01:00
abstract class UIComponent<S : ViewState, A : Action, C : Change>(
protected val actionEmitter: Observer<A>,
protected val changesObservable: Observable<C>
) {
2019-01-28 17:46:39 +01:00
abstract var initialState: S
abstract val reducer: Reducer<S, C>
2019-01-31 07:49:41 +01:00
val uiView: UIView<S, A, C> by lazy { initView() }
2019-01-28 17:46:39 +01:00
2019-01-31 07:49:41 +01:00
abstract fun initView(): UIView<S, A, C>
2019-01-28 17:46:39 +01:00
open fun getContainerId() = uiView.containerId
2019-01-30 17:36:14 +01:00
2019-01-28 17:46:39 +01:00
/**
* Render the ViewState to the View through the Reducer
*/
2019-01-31 07:49:41 +01:00
fun render(reducer: Reducer<S, C>): Disposable =
changesObservable
2019-01-28 17:46:39 +01:00
.scan(initialState, reducer)
.distinctUntilChanged()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(uiView.updateView())
}