You can create your own PagingSource
which will accept multiple PagingSource
in a constructor and load them one after another.
class MultipleSourcesSource<T : Any>(
val sources: List<PagingSource<Int, T>>
) : PagingSource<Int, T>() {
private var currentIndex = 0
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, T> {
if (currentIndex == sources.size) {
return LoadResult.Page(
data = emptyList(),
prevKey = null,
nextKey = null
)
}
val result = sources[currentIndex].load(params)
currentIndex++
return LoadResult.Page(
data = result.data,
prevKey = if (currentIndex == 0) currentIndex else currentIndex - 1,
nextKey = currentIndex
)
return result
}
override fun getRefreshKey(state: PagingState<Int, T>) = null
}
You also will have to create PagingSource
's for your tables:
class Table1DataSource(
private val database: MyDatabase
) : PagingSource<Int, Item>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Item> {
val items = database.getData() // get data from your source
return LoadResult.Page(
data = items,
prevKey = null,
nextKey = null
)
}
override fun getRefreshKey(state: PagingState<Int, Table1Item>) = null
}
And finally you can just use MultipleSourcesSource
for load your data:
val mediator = MultipleSourcesSource(listOf(
// here are table sources
))
// Create a Pager with the mediator
val pager = Pager(
config = PagingConfig(pageSize = pageSize),
pagingSourceFactory = { mediator }
)
.flow
.collect { results ->
// TODO: collect results
}