I am developing an app that shows a list of items it fetches from the internet. I have 2 buttons loadMore and refresh, loadMore - loads the next batch of items, refresh - load the items from the beginning.
I am using MVI (Model View Intent) pattern. Just to make it simple i created an example using a list of numbers each number will represent a batch of items:
val loadSubject = BehaviorSubject.create<Unit>()
val refreshSubject = PublishSubject.create<Unit>()
val list = loadSubject.scanWith(
{ Observable.just(emptyList<Int>()) },
{ listObservable, _ ->
listObservable
.map { it + ++count }
.replay().autoConnect()
}
)
.flatMap { it }
.filter { it.isNotEmpty() }
val listSubscription = {
list.subscribe {
//do whatever with the list
}
}
refreshSubject.scanWith(
listSubscription,
{ disposable, _ ->
disposable.dispose()
listSubscription()
}
).subscribe()
So now it would work perfectly but the subscription is in my Intent, I need a method with Rx that would do exact same thing but letting my View subscribe.
What I am trying to get is:
let say my list is [1,2,3]
on loadMore press ill get [1,2,3,4]
on refresh press ill get [5]