Skip to content

Instantly share code, notes, and snippets.

@chetdeva
Last active November 2, 2018 14:01
Show Gist options
  • Save chetdeva/ce1598c0456c5e7c468ef0c2c8f09a3d to your computer and use it in GitHub Desktop.
Save chetdeva/ce1598c0456c5e7c468ef0c2c8f09a3d to your computer and use it in GitHub Desktop.
/**
* A data source that uses the before/after keys returned in page requests.
*/
class GithubPageKeyedDataSource : PageKeyedDataSource<Int, Item>() {
override fun loadInitial(params: LoadInitialParams<Int>,
callback: LoadInitialCallback<Int, Item>) {
val currentPage = 1
val nextPage = currentPage + 1
val request = githubApi.searchUsers(
query = searchQuery,
page = currentPage,
perPage = params.requestedLoadSize)
// Retrofit Call onResponse omitted
callback.onResult(items, null, nextPage)
}
override fun loadAfter(params: LoadParams<Int>,
callback: LoadCallback<Int, Item>) {
val currentPage = params.key
val nextPage = currentPage + 1
val request = githubApi.searchUsersAsync(
query = searchQuery,
page = currentPage,
perPage = params.requestedLoadSize)
// Retrofit Call onResponse omitted
callback.onResult(items, nextPage)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment