Skip to content

Fix paging source key #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: step12_separators_old
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ class GithubPagingSource(
private val query: String
) : PagingSource<Int, Repo>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Repo> {
val position = params.key ?: GITHUB_STARTING_PAGE_INDEX
val position = params.key?.let {
it * NETWORK_PAGE_SIZE / params.loadSize
} ?: GITHUB_STARTING_PAGE_INDEX
val apiQuery = query + IN_QUALIFIER
return try {
val response = service.searchRepos(apiQuery, position, params.loadSize)
val repos = response.items
val prevKey = if (position == GITHUB_STARTING_PAGE_INDEX) {
null
} else {
(position * params.loadSize / NETWORK_PAGE_SIZE) - 1
}
val nextKey = if (repos.isEmpty()) {
null
} else {
Expand All @@ -47,7 +54,7 @@ class GithubPagingSource(
}
LoadResult.Page(
data = repos,
prevKey = if (position == GITHUB_STARTING_PAGE_INDEX) null else position - 1,
prevKey = prevKey,
nextKey = nextKey
)
} catch (exception: IOException) {
Expand Down