简体   繁体   English

Android 分页库 - Map 房间 DataSource.Factory<*, DatabaseModel> 到 DataSource.Factory<*, PresenterModel>

[英]Android Paging Library - Map Room DataSource.Factory<*, DatabaseModel> to DataSource.Factory<*, PresenterModel>

I use two models in my app:我在我的应用程序中使用了两个模型:

  1. Database数据库
  2. Presenter (UI)演示者(用户界面)

Android Paging gives me a DataSource.Factory<*, DatabaseModel> Android 分页给了我一个DataSource.Factory<*, DatabaseModel>

@Dao
interface ProjectDao {
    @Query("SELECT * FROM project")
    fun getAllProjects(): DataSource.Factory<Int, DatabaseModel>
    ...
}

When I want to make the LiveData using LivePagedListBuilder(dataSourceFactory, config) I need to map:当我想使用LiveData LivePagedListBuilder(dataSourceFactory, config)制作 LiveData 时,我需要 map:

DataSource.Factory<*, DatabaseModel> -|----> DataSource.Factory<*, PresenterModel> DataSource.Factory<*, DatabaseModel> -|----> DataSource.Factory<*, PresenterModel>

Is there any way possible to achieve this.有没有办法实现这一点。 I'm also open to here any approach done using RxKotlin (RxJava).我也对使用 RxKotlin (RxJava) 完成的任何方法持开放态度。

Here is how i have done done it这是我的做法

Data Source数据源

fun getDataSource(): DataSource.Factory<Int, DBModel> {
     return database.dao.getAllData()
   }

variable at view model变量视图 model

val scannedCompleteList=App.getRepository().getDataSource().toLiveData(
                Config(
                    pageSize = 60,
                    enablePlaceholders = true,
                    maxSize = 200
                )
            )

Now i have a binding adapter where i convert the data from db model to domain model现在我有一个绑定适配器,我将数据从 db model 转换为域 model

@BindingAdapter("setData")
fun setImageScanned(recyclerView: RecyclerView, data: List<DBModel>?) {
        val adapter = recyclerView.adapter as MyAdapter
        adapter.submitList(it.asDomainModel())
    }
}

So you are observing data at fragment so you can convert the data to presenter inside observer.因此,您正在观察片段中的数据,因此您可以将数据转换为观察者内部的演示者。 asDomainModel is an extension function which do the conversion asDomainModel 是一个扩展 function 进行转换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android体系结构组件分页DataSource.Factory错误 - Android architecture components paging DataSource.Factory error 自定义构造函数PageKeyedDataSource()在分页库的datasource.factory()中崩溃应用程序 - Custom constructor PageKeyedDataSource() crashes app in datasource.factory() of paging library 具有多视图 RecyclerView 的 Room DataSource.Factory - Room DataSource.Factory with multiple view RecyclerView 用于多个数据源的分页库 DataSource.Factory - Paging library DataSource.Factory for multiple data sources DataSource.Factory 未使用正确的变量值 Android 分页 - DataSource.Factory is not using correct variable value Android Paging 无法从Android的分页中的DataSource.Factory获得toLiveData - toLiveData not available from DataSource.Factory in Android's Paging Android Room 在使用 DataSource.Factory 时如何按文本搜索? - Android Room how to search by text when using DataSource.Factory? 房间本地单元测试 - 从 DataSource.Factory 查询 PagedList - Room Local Unit Test - Query PagedList from DataSource.Factory Kotlin 协程挂起错误与 Room DataSource.Factory - Kotlin coroutine suspend error with Room DataSource.Factory 如何从 DataSource.Factory 获取数据 - How to get data from DataSource.Factory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM