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

37 lines
1.4 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
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
2019-01-30 17:36:14 +01:00
abstract class UIComponent<S : ViewState, A : Action, C : Change>(open val bus: ActionBusFactory) {
2019-01-28 17:46:39 +01:00
abstract var initialState: S
abstract val reducer: Reducer<S, C>
val uiView: UIView<S> by lazy { initView() }
abstract fun initView(): UIView<S>
open fun getContainerId() = uiView.containerId
2019-01-30 17:36:14 +01:00
inline fun <reified A : Action> getUserInteractionEvents():
Observable<A> = bus.getSafeManagedObservable(A::class.java)
inline fun <reified C : Change> getModelChangeEvents():
Observable<C> = bus.getSafeManagedObservable(C::class.java)
2019-01-28 17:46:39 +01:00
/**
* Render the ViewState to the View through the Reducer
*/
inline fun <reified C : Change> render(noinline reducer: Reducer<S, C>): Disposable =
getModelChangeEvents<C>()
2019-01-28 17:46:39 +01:00
.scan(initialState, reducer)
.distinctUntilChanged()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(uiView.updateView())
}