Search code examples
androidkotlinandroid-jetpackclean-architectureandroid-paging

Jetpack Paging 3 inside java/kotlin module


I'm developing an app with clean architecture principles. I've a domain module which is a Java/Kotlin module and it hasn't android dependencies and a domainImpl module which is an Android module and has dependencies to local, remote and domain module. this is a Repository example inside domain module:

interface MovieRepository {
    fun getMovie(id: Long): Flow<Movie>
}

and below code is it's implementation inside domainImpl module:

class MovieRepositoryImpl(
    private val movieApi: MovieApi
) : MovieRepository {

    override fun getMovie(id: Long): Flow<Movie> = flow {
        emit(movieApi.getMovie(id))
    }

}

Everything works fine in this case. But Now I'm trying to add Android Paging 3 for my pagination. So I have to add a method to the MovieRepository interface like:

fun getMovies(): Flow<PagingData<Movie>>

But before this I've to add Paging library to my domain module but unfortunately it is an Android library and I couldn't find a core dependency for it. So What can I do about it? Do I have to change my domain module to an android module because of this? Or is there any other workaround?


Solution

  • The non android components of Paging are in androidx.paging:paging-common - any dependencies on presenter APIs are Android specific by definition so any code related to RecyclerView, LazyColumn, etc will need to be Android specific.

    However PagingSource, PagingData, Pager, RemoteMediator are all provided by paging-common, so your implementation of those classes, including your transformation chain and overall output of Flow<PagingData> can be completely android-free.