Skip to content
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