0
  • 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>}
cap_muho
  • 63
  • 7

1 Answers1

0

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
        )
    }
}
}
Farid
  • 1,024
  • 9
  • 16
  • This is what I currently have and been using but I run into issues where the load method never gets called even after calling invalidate after getting a new list. – cap_muho Apr 29 '21 at 17:37
  • Other case I observed is , for each invalidate of the paging source I have to define a new a new pager source as I kept getting an exception cannot pass same paging source. – cap_muho Apr 29 '21 at 17:42
  • I had the same problem as you mentioned in your second comment, the problem was that i declared my PagingSource as an object and i was trying to pass it again to pager instead of creating a new instance of it. – Farid Apr 30 '21 at 12:24