[英]Android Paging Library - Map Room DataSource.Factory<*, DatabaseModel> to DataSource.Factory<*, PresenterModel>
I use two models in my app:我在我的应用程序中使用了两个模型:
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.