Add RxStore and StoryViewerPage forward navigation.

This commit is contained in:
Alex Hart
2022-04-01 09:27:09 -03:00
committed by Cody Henthorne
parent 11c3ea769e
commit 3e42c044b8
8 changed files with 300 additions and 37 deletions

View File

@@ -0,0 +1,30 @@
package org.thoughtcrime.securesms.util.rx
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.processors.BehaviorProcessor
import io.reactivex.rxjava3.schedulers.Schedulers
import io.reactivex.rxjava3.subjects.PublishSubject
/**
* Rx replacement for Store.
* Actions are run on the computation thread.
*/
class RxStore<T : Any>(defaultValue: T) {
private val behaviorProcessor = BehaviorProcessor.createDefault(defaultValue)
private val actionSubject = PublishSubject.create<(T) -> T>().toSerialized()
val state: T get() = behaviorProcessor.value!!
val stateFlowable: Flowable<T> = behaviorProcessor
init {
actionSubject
.observeOn(Schedulers.computation())
.scan(defaultValue) { v, f -> f(v) }
.subscribe { behaviorProcessor.onNext(it) }
}
fun update(transformer: (T) -> T) {
actionSubject.onNext(transformer)
}
}