5

I am using clean architecture with paging 3 and trying to populate a lazyColumn.

I have the following class in my data layer module and I don't want to pass the PagingData to the domain layer as I want to keep the domain free of any Android SDK.

class RepositoryImp @Inject constructor(
    private val foodService: FoodService,
    private val foodDatabase: FoodDatabase) : Repository {

    @OptIn(ExperimentalPagingApi::class)
    override fun fetchAllComplexSearch(): Flow<ResponseState<List<ComplexSearchEntity>>> {
        val pagingSourceFactory = { foodDatabase.foodDao().fetchAllComplexSearchPaging() }

        val pagingDataResult = Pager(
            config = PagingConfig(pageSize = ITEMS_PER_PAGE_DEFAULT),
            remoteMediator = ComplexSearchRemoteMediator(
                foodDatabase = foodDatabase, foodService = foodService
            ),
            pagingSourceFactory = pagingSourceFactory
        ).flow


        val data = pagingDataResult.map { pagingData ->
            pagingData.map { complexSearchModel ->
                ResponseState.Success(
                    listOf(
                        ComplexSearchEntity(
                            complexSearchModel.id,
                            complexSearchModel.title,
                            complexSearchModel.image,
                            complexSearchModel.imageType
                        )
                    )
                )
            }
        }
        
        return data
    }

I want to return this Flow<ResponseState<List<ComplexSearchEntity>>> But I get the following error:

Type mismatch.
Required:
Flow<ResponseState<List<ComplexSearchEntity>>>
Found:
Flow<PagingData<ResponseState.Success<List<ComplexSearchEntity>>>>

It seems like I am wrapping the ResponseState.Success(...) inside the PagingData

Then the presentation layer module will map the Response.Success in a PagingData to be used as a LazyPagingItems<ComplexSearchEntity> in a LazyColumn.

Jesse
  • 3,243
  • 1
  • 22
  • 29
ant2009
  • 27,094
  • 154
  • 411
  • 609
  • Maybe you could initialize your Pager in the viewmodel? – mrzbn Jul 20 '22 at 16:01
  • I don't want to move setting up the Pager in the viewModel. Because the mediator is in the data layer and the presentation layer should not know about the data layer. – ant2009 Jul 20 '22 at 16:20

1 Answers1

2

As of this answer you can use common artifact of paging library in your domain module.

mrzbn
  • 497
  • 1
  • 3
  • 15