Created
May 29, 2020 03:14
-
-
Save yongjhih/8a0468b7a6ea7633bab7d522a219aa66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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