Search code examples
androidandroid-recyclerviewclean-architectureandroid-mvvmandroid-paging-3

Android. How to use pagination with clean architecture?


I'm writing application with MVVM and clean architecture. On one of the screens I need to implement RecyclerView with pagination. I am going to use the library Paging3.

Android Developer recommends using PagingSource and RemoteMediator in repository layer. But at the same time, in many sources, I read that the data layer and the domain layer should not know anything about the android framework.

But now I have to use the android library in the data sources of my data layer. Is this correct in the context of a clean architecture?

Please help me figure it out, I don't understand how to implement pagination using clean architecture.


Solution

  • Paging library is split into multiple artifacts. There is paging-common, which is a pure kotlin library that contains PagingSource and RemoteMediator among others. Then, there is a paging-runtime which is an android library that wires paging sources with RecyclerView and paging-compose, another android library that does the same for Jetpack Compose UI.

    That means that you can use paging-common in a pure kotlin module and in your repositories just fine, there is no android specific code in there. You can even use that repository on different platform, if you write that platform specific wiring yourself.