Skip to content

Instantly share code, notes, and snippets.

@yongjhih
Created May 29, 2020 03:14
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 yongjhih/8a0468b7a6ea7633bab7d522a219aa66 to your computer and use it in GitHub Desktop.
Save yongjhih/8a0468b7a6ea7633bab7d522a219aa66 to your computer and use it in GitHub Desktop.
class UserDataSource(
private val query: String,
private val githubService: GitHubService,
private val compositeDisposable: CompositeDisposable
) : PageKeyedDataSource<String, User>() {
val lifecycle by lazy { MutableLiveData<DataSourceLifecycle>() }
override fun loadInitial(
params: LoadInitialParams<String>,
callback: LoadInitialCallback<String, User>
) {
compositeDisposable.add(
githubService.getUsersCall(query)
.single()
.lifecycleResponse(lifecycle)
.subscribe { response ->
val links = response.headers().get("Link")
?.let { PageLinks(it) }
val users = response.body()?.items ?: emptyList()
callback.onResult(users, links?.prev, links?.next)
}
)
}
override fun loadAfter(params: LoadParams<String>, callback: LoadCallback<String, User>) {
compositeDisposable.add(
githubService.getUsersCallByUrl(params.key)
.single()
.lifecycleResponse(lifecycle)
.subscribe { response ->
val links = response.headers().get("Link")
?.let { PageLinks(it) }
val users = response.body()?.items ?: emptyList()
callback.onResult(users, links?.next)
}
)
}
override fun loadBefore(params: LoadParams<String>, callback: LoadCallback<String, User>) {
compositeDisposable.add(
githubService.getUsersCallByUrl(params.key)
.single()
.lifecycleResponse(lifecycle)
.subscribe { response ->
val links = response.headers().get("Link")
?.let { PageLinks(it) }
val users = response.body()?.items ?: emptyList()
callback.onResult(users, links?.prev)
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment