How do we change PagingSource dynamically in Pager...
# android
s
How do we change PagingSource dynamically in Pager? I need to collect from one Flow and when searching/filtering select another flow
e
You can feed only one Flow to the Adapter right? IIRC PagingSource allows you to filter/map/transform it.
m
val filterStateFlow = MutableStateFlow<YourFilter>(...) val pagedData = filterStateFlow.flatMapLatest { createPagerFlow(filter = it) } This will switch pager when filterStateFlow changes
👍 1
s
I will try it. Thanks
Copy code
fun getAllOrdersByMobileNumber(number: String): Flow<PagingData<Order>> {
    return Pager(config = pagingConfig) {
        dao.getOrdersByNumber("%$number%")
    }.flow.cachedIn(viewModelScope)
}

fun getAllOrders(): Flow<PagingData<Order>> {
    return Pager(config = pagingConfig) {
        dao.getAllOrders()
    }.flow.cachedIn(viewModelScope)
}
I have this feeding the recyclerview paging adapter
number
changes by LiveData.
PagingData
comes from Room database
in the activity I observe the
number
LiveData change, then I have to cancel the previous
Job
and lauch another
Job
to observe the new
Flow.
Which seems very verbose to me.
alright it works creating to
PagingSource
inside the
Pager
I though I had to recreate everything
106 Views