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

Viewed 859

I use two models in my app:

  1. Database
  2. Presenter (UI)

Android Paging gives me a 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:

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).

1 Answers

Here is how i have done done it

Data Source

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

variable at view 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

@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

Related