From 87bd8dbbc63b197b088452dd7d1e6b40fc029328 Mon Sep 17 00:00:00 2001 From: Dustin Lam Date: Wed, 29 Apr 2020 19:42:41 -0700 Subject: [PATCH] Fix RemoteMediator endOfPagination logic --- .../paging/data/GithubRemoteMediator.kt | 18 ++++++++---------- build.gradle | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/example/android/codelabs/paging/data/GithubRemoteMediator.kt b/app/src/main/java/com/example/android/codelabs/paging/data/GithubRemoteMediator.kt index 684c40df..8674226c 100644 --- a/app/src/main/java/com/example/android/codelabs/paging/data/GithubRemoteMediator.kt +++ b/app/src/main/java/com/example/android/codelabs/paging/data/GithubRemoteMediator.kt @@ -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 @@ -63,7 +62,7 @@ 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, @@ -71,22 +70,21 @@ class GithubRemoteMediator( // 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) } } diff --git a/build.gradle b/build.gradle index 0471a6da..ed701b4d 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } } }