使用 paging3 清洁架构并组合将 PagingData 传递到域层

ant*_*009 6 android clean-architecture android-jetpack-compose android-paging-3

我正在使用分页 3 的干净架构并尝试填充lazyColumn。

我的数据层模块中有以下类,我不想将 PagingData 传递到域层,因为我想让域不受任何 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
    }
Run Code Online (Sandbox Code Playgroud)

我想退货Flow<ResponseState<List<ComplexSearchEntity>>>,但出现以下错误:

Type mismatch.
Required:
Flow<ResponseState<List<ComplexSearchEntity>>>
Found:
Flow<PagingData<ResponseState.Success<List<ComplexSearchEntity>>>>
Run Code Online (Sandbox Code Playgroud)

看起来我正在将 ResponseState.Success(...) 包装在PagingData

然后,表示层模块会将 PagingData 中的 Response.Success 映射为LazyPagingItems<ComplexSearchEntity>LazyColumn 中的 a。

mrz*_*zbn 2

从这个答案开始,您可以在域模块中使用分页库的常见工件。