Skip to content
Merged
Show file tree
Hide file tree
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 @@ -16,7 +16,6 @@

package com.example.android.codelabs.paging.data

import android.util.Log
import androidx.paging.LoadType
import androidx.paging.PagingState
import androidx.paging.RemoteMediator
Expand Down Expand Up @@ -63,30 +62,29 @@ class GithubRemoteMediator(
val remoteKeys = getRemoteKeyClosestToCurrentPosition(state)
remoteKeys?.nextKey?.minus(1) ?: GITHUB_STARTING_PAGE_INDEX
}
LoadType.START -> {
LoadType.PREPEND -> {
val remoteKeys = getRemoteKeyForFirstItem(state)
if (remoteKeys == null) {
// The LoadType is START so some data was loaded before,
// so we should have been able to get remote keys
// If the remoteKeys are null, then we're an invalid state and we have a bug
throw InvalidObjectException("Remote key and the prevKey should not be null")
}

// If the previous key is null, then we can't request more data
val prevKey = remoteKeys.prevKey
if (prevKey == null) {
return MediatorResult.Success(endOfPaginationReached = false)
}
remoteKeys.prevKey
remoteKeys.prevKey ?: return MediatorResult.Success(endOfPaginationReached = true)
}
LoadType.END -> {
LoadType.APPEND -> {
val remoteKeys = getRemoteKeyForLastItem(state)
if (remoteKeys?.nextKey == null) {
if (remoteKeys == null) {
// The LoadType is END, so some data was loaded before,
// so we should have been able to get remote keys
// If the remoteKeys are null, then we're an invalid state and we have a bug
throw InvalidObjectException("Remote key should not be null for $loadType")
}
remoteKeys.nextKey

// If the next key is null, then we can't request more data
remoteKeys.nextKey ?: return MediatorResult.Success(endOfPaginationReached = true)
}
}

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ allprojects {
repositories {
google()
jcenter()
maven { url 'https://androidx-dev-prod.appspot.com/snapshots/builds/6433189/artifacts/repository' }
maven { url 'https://androidx-dev-prod.appspot.com/snapshots/builds/6445242/artifacts/repository' }
}
}

Expand Down