Striker
06/18/2020, 8:51 AMokarm
06/19/2020, 8:26 PMTransformations.switchMap
will be your friend here.
https://developer.android.com/reference/androidx/lifecycle/Transformationsokarm
06/19/2020, 8:30 PMprivate val selection = MutableLiveData<DateSelection>(Today)
val pagedReceipts: LiveData<PagingData<ReceiptPostBody>> = selection.switchMap { dateSelection ->
Pager(config = ...,
initialKey = ...,
pagingSourceFactory = { MyPagingSource() }
).liveData
}
Striker
06/20/2020, 7:03 AMStriker
06/20/2020, 7:04 AMokarm
06/20/2020, 10:26 AMMediatorLiveData
is used for this.
https://github.com/Zhuinden/livedata-combinetuple-kt/blob/master/livedata-combinetuple-kt/src/main/java/com/zhuinden/livedatacombinetuplekt/LiveDataCombineTuple.kt#L22Striker
06/20/2020, 10:39 AMokarm
06/20/2020, 12:06 PMFlow
instead of LiveData
you can use the LiveData.asFlow()
extension function to convert your filterBy
and searchQuery
into `Flow`s and then use combine
and flatMapConcat
to produce the final Flow<PagingData>
It would look something like this:
val latestPagingData: Flow<PagingData<SomeModel>> = searchQuery.asFlow().combine(filterBy.asFlow()) { query, filter -> Pair(query, filter) }.flatMapConcat { params -> Pager(...).flow }
Striker
06/20/2020, 12:53 PM