Is there a way to convert a List<T> to a PagedData<T> while the backing paging source data doesn't use room db

999 Views Asked by At
  • I do have a paged data source that returns a PagedData<T> data that is displayed with the pagedAdapter after initial load from SQLite. After some server transactions,I receive a list of List<T> and after transforming and caching it need to display it.
  • I know this would be easy with Room db but that's off the table for now.How do I update the initial paged list and create a new pagedList and then push it to the adapter.
  • Tried using a map transformation on the new server list to PagingDataObject but it's not possible as far as I know. list.map { item -> {PagingData<item>}
1

There are 1 best solutions below

3
Farid On

If you want you can implement your own pagingSource, it's not necessary to use Room. The first half of paging3 codelab shows you exactly how you can achive that. Create your pager like this :

Pager(
      config = PagingConfig(
        pageSize = 10,
        enablePlaceholders = false
     ),
      pagingSourceFactory = { MyPaginSource() }
).flow

and implement MyPaginSource like this :

class MyPaginSource() : PagingSource<Int, MyItem>() {

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MyItem> {
    if(params.key == null){
        //load initial list from where ever you want, and create your own key 
        //to load next page of data
        val firstPage = //todo
        val nextKey  = //todo
        return LoadResult.Page(
                data = firstPage ,
                prevKey = null,
                nextKey = nextKey
        )
    } else{
        //load the other pages based on your key, for example from server 
        val newPage = //todo
        val nextKey  = //todo if you need to continue loading
        return LoadResult.Page(
                data = newPage ,
                prevKey = null,
                nextKey = nextKey
        )
    }
}
}