Hey everybody, What's the recommended approach fo...
# android
a
Hey everybody, What's the recommended approach for using Paging 3 and Room with Clean Architecture? Since the domain layer is Java or Kotlin only, my repository or usecases can't return a
PagingSource
and implementing it in the presentation layer is far from ideal.
It could be possible to create an
ItemsPager
interface in the domain layer that the
PagingSource
also implements and have a mapper that maps
ItemPager
to
PagingSource
, but I've never seen that done anywhere and it would also mean that I don't get to use Room's Paging support.
d
PagingSource comes from paging-common which does not have android dependency
✔️ 1
LimitOffsetDataSource from Room is in room-runtime though
👍 1
a
It is a coincidence that I am doing some r&d on it. I will also try to apply the suggestions here and update my finding
a
So I ended up exposing
Flow<PagingData<..>>
, because it doesn't seem to be possible to map the items of a
PagingSource
directly. However, this thing doesn't seem to be testable, whatsoever, if your
PagingSource
is created by Room. Is that the case?
d
What are you interested in testing?
in terms of transformations, you need to collect the state of PagingData into a presenter api like the differ
then use .snapshot(), we'll be adding that sample soon
a
This's a method of my `LocalDataSource`:
Copy code
override fun findPosts(pageSize: Int): Flow<PagingData<Post>> {
    return Pager(
        config = PagingConfig(pageSize = pageSize, enablePlaceholders = false),
        pagingSourceFactory = { postsDao.findPosts() }
    ).flow.map { pagingData ->
           pagingData.map(PostEntity::toPost)
    }
}
findPosts
is a Room method that returns
PagingSource<Int, PostEntity>
and I'm exposing
Flow<PagingData<Post>>
since I'm unable to map a
PagingSource<Int, PostEntity>
to a
PagingSource<Int, Post>
. I have a problem with both creating a fake of
PagingData
and with testing it. I need to create a fake of
LocalDataSource
where I replace the method mentioned above with a fake version of it, the way to do that seems to be to create a
PagingSource
just for testing, which is more code than the actual code being tested. The other problem is testing the
PagingData
flow, there seems to be no way to know what data
PagingData
contains, so even if I create a
PagingSource
, how do I know it contains the correct data and delivers them correctly?
I've seen the docs you mentioned but they assume I'm able to expose the PagingSource directly and that I own it, both of which are not the case.
d
As I mentioned earlier, you need to collect PagingData's state into a presenter API like a differ which you can make a simple no-op implementation of
✔️ 1
For your PagingSource from Room, I would just use Room's test utils and get an in-memory impl from room you can control rather than mocking it