I am using paging 3 and cannot find my use case in the available tutorials.
basically I paste is the example i find in the documentation and in all the tutorials on line, basically backend.searchUsers(query, nextPageNumber)
does not fit my case I have just to call backend.fetchAList(userId,startRecord,endRecord)
so basically I need to ask to the back end to giving to me only a specific range of pages, let's say I want 16 items per time, I guess I should ask startRecord:0 endRecord:15
then I do not know how if user scrolls at the end of the list I should ask startRecord:16 endRecord:31
and so on, till the end of the list. How can I achieve that with Paging3?
EDIT: I am assuming that my approach should be like I mention above my use case is: as user I want to retrieve a list with PaginatedData to fill my adapter, loading the list gradually so that the use scroll a certain number of pages and then new ones are retrieved from the back end, with the condition that the back end has these fields: startRecord: Int; endRecord:Int, hasNextItems: Boolean, List, totalItems: Int
class ExamplePagingSource(
val backend: ExampleBackendService,
val query: String
) : PagingSource<Int, User>() {
override suspend fun load(
params: LoadParams<Int>
): LoadResult<Int, User> {
try {
// Start refresh at page 1 if undefined.
val nextPageNumber = params.key ?: 1
val response = backend.searchUsers(query, nextPageNumber)
return LoadResult.Page(
data = response.users,
prevKey = null, // Only paging forward.
nextKey = response.nextPageNumber
)
} catch (e: Exception) {
// Handle errors in this block and return LoadResult.Error if it is an
// expected error (such as a network failure).
}
}
override fun getRefreshKey(state: PagingState<Int, User>): Int? {
// Try to find the page key of the closest page to anchorPosition, from
// either the prevKey or the nextKey, but you need to handle nullability
// here:
// * prevKey == null -> anchorPage is the first page.
// * nextKey == null -> anchorPage is the last page.
// * both prevKey and nextKey null -> anchorPage is the initial page, so
// just return null.
return state.anchorPosition?.let { anchorPosition ->
val anchorPage = state.closestPageToPosition(anchorPosition)
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
}
}
}