Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9ea613bf authored by Moez Bhatti's avatar Moez Bhatti
Browse files

Perform state reduction on main thread

parent 0e981da6
Loading
Loading
Loading
Loading
+18 −9
Original line number Diff line number Diff line
@@ -23,13 +23,26 @@ import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject

abstract class QkPresenter<View : QkViewContract<State>, State>(initialState: State) {

    val disposables = CompositeDisposable()
    protected val disposables = CompositeDisposable()
    protected val state: Subject<State> = BehaviorSubject.createDefault(initialState)

    protected val state: BehaviorSubject<State> = BehaviorSubject.createDefault(initialState)
    private val stateReducer: Subject<State.() -> State> = PublishSubject.create()

    init {
        // If we accidentally push a realm object into the state on the wrong thread, switching
        // to mainThread right here should immediately alert us of the issue
        disposables += stateReducer
                .observeOn(AndroidSchedulers.mainThread())
                .scan(initialState) { state, reducer -> reducer(state) }
                .subscribe(state::onNext)
    }

    @CallSuper
    open fun bindIntents(view: View) {
@@ -39,12 +52,8 @@ abstract class QkPresenter<View : QkViewContract<State>, State>(initialState: St
                .subscribe(view::render)
    }

    protected fun newState(reducer: State.() -> State) {
        state.value?.let { state.onNext(reducer(it)) }
    }
    protected fun newState(reducer: State.() -> State) = stateReducer.onNext(reducer)

    open fun onCleared() {
        disposables.dispose()
    }
    open fun onCleared() = disposables.dispose()

}
+18 −10
Original line number Diff line number Diff line
@@ -24,29 +24,37 @@ import com.uber.autodispose.android.lifecycle.scope
import com.uber.autodispose.autoDisposable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject

abstract class QkViewModel<in View : QkView<State>, State>(initialState: State) : ViewModel() {

    protected val state: BehaviorSubject<State> = BehaviorSubject.createDefault(initialState)

    protected val disposables = CompositeDisposable()
    protected val state: Subject<State> = BehaviorSubject.createDefault(initialState)

    private val stateReducer: Subject<State.() -> State> = PublishSubject.create()

    init {
        // If we accidentally push a realm object into the state on the wrong thread, switching
        // to mainThread right here should immediately alert us of the issue
        disposables += stateReducer
                .observeOn(AndroidSchedulers.mainThread())
                .scan(initialState) { state, reducer -> reducer(state) }
                .subscribe(state::onNext)
    }

    @CallSuper
    open fun bindView(view: View) {
        state
                .observeOn(AndroidSchedulers.mainThread())
                .autoDisposable(view.scope())
                .subscribe { view.render(it) }
                .subscribe(view::render)
    }

    protected fun newState(reducer: State.() -> State) {
        state.value?.let { state.onNext(reducer(it)) }
    }
    protected fun newState(reducer: State.() -> State) = stateReducer.onNext(reducer)

    override fun onCleared() {
        super.onCleared()
        disposables.dispose()
    }
    override fun onCleared() = disposables.dispose()

}
 No newline at end of file