Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map LiveData<PagedList<X>> to LiveData<PagedList<Y>>

I need to map domain objects to UI objects and display using a live paged list.

I have tried to map LiveData<PagedList<X>> to LiveData<PagedList<Y>>, and map PositionalDataSource<X> to PositionalDataSource<Y>, but due to package private and private restrictions these both appear to be impossible without placing my code in android.arch.paging package and using reflection, or using a modified version of the paging lib.

Does anyone know of a way to do this without resorting to such undesirable methods?

(Note that this would not be an issue if the paging library API used interfaces instead of abstract base classes - that would allow wrapping any paged list/data source and add appropriate mappings.)

like image 462
Chris Avatar asked Feb 19 '18 18:02

Chris


2 Answers

DataSource and DataSource.Factory have mapBy() and mapPageBy(), which could help you in this case. Just be careful cause these two will restrict the size of the "Y"-result-list.

If the size of the result differs from the original list's size then DataSource will throw an Exception.

like image 106
edy Avatar answered Nov 17 '22 11:11

edy


For me following worked :

  val dataSourceFactory = 
         cache.getDataSourceFactory(params)
              .map {
                   convertXToY(it)
                  }

like image 31
Prashant Priyadarshi Avatar answered Nov 17 '22 12:11

Prashant Priyadarshi