Mantas Varnagiris
12/18/2020, 7:36 PMRoom
with Paging 3
. I'll explain my setup first so the problem is a bit clearer
• database (android module)
has Room
. It has a Dao
with fun getItems(): PagingSource<Int, ItemEntity>
• ItemEntity
is internal only to database
module - I don't want to use it in domain. It is converted to Item
• core (kotlin module)
has Paging 3
. It wants to do paging using PagingSource<Int, Item>
(not ItemEntity
as it has no knowledge of it)
• database
module implements interface that gives PagingSource<Int, Item>
Now my problem is that I cannot convert from PagingSource<Int, ItemEntity>
to PagingSource<Int, Item>
. At least I could not find a way to do it. I don't want to expose ItemEntity
but I want to do paging with converted model Item
. How can I do that?eMantas Varnagiris
12/19/2020, 12:24 PMDao
exposes DataSource.Factory<Int, ItemEntity>
and then use .map { it.toItem() }..asPagingSourceFactory().invoke()
Mantas Varnagiris
12/19/2020, 12:25 PMDataSource.Factory
from Paging 2
to map valuesJorge R
12/21/2020, 12:15 PMMantas Varnagiris
12/21/2020, 12:18 PMPagingSource
but it does seem like this is the best approach now. Although my "hack" of using Paging 2 DataSource.Factory
that supports mapping and conversion to PagingSource
kind of works as well 🙂Jorge R
12/21/2020, 12:21 PMDustin Lam
01/09/2021, 6:27 PMclass MappedPagingSource<Key : Any, R : Any, T :Any>(
val originalPagingSource: PagingSource<Key, T>
val block: suspend (T) -> R
): PagingSource<Key, R> {
override suspend fun load(params) {
val page = originalPagingSource.load(params)
return LoadResult.Page(
...
data = page.data.map(block),
...
}
}
Dustin Lam
01/09/2021, 6:29 PMDustin Lam
01/09/2021, 6:31 PMMantas Varnagiris
01/09/2021, 6:44 PMDustin Lam
01/09/2021, 7:12 PMMantas Varnagiris
01/09/2021, 7:13 PMJorge R
01/10/2021, 12:11 AMDustin Lam
01/10/2021, 12:17 AMDustin Lam
01/10/2021, 12:18 AM