Skip to content

Instantly share code, notes, and snippets.

@Marchuck
Created June 14, 2018 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Marchuck/dad2be3ef5fbd87614c313e5a8732cdd to your computer and use it in GitHub Desktop.
Save Marchuck/dad2be3ef5fbd87614c313e5a8732cdd to your computer and use it in GitHub Desktop.
class SwapiPeopleDataSource(val useCase: GetPeopleUseCase) : PageKeyedDataSource<Int, Person?>() {
override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, Person?>) {
async {
val items = useCase.execute(page = 1).await()
callback.onResult(items.orEmpty(), null, 2)
}
}
override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, Person?>) {
async {
val items = useCase.execute(page = params.key).await()
callback.onResult(items.orEmpty(), params.key.inc())
}
}
override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, Person?>) {
async {
val items = useCase.execute(page = params.key).await()
callback.onResult(items.orEmpty(), params.key.dec())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment