Paging Library 3 LoadType.Append is never called

Viewed 18

I'm using paging library 3 , and it is working fine , but when it comes to the remote mediator , the refresh and prepend are called but LoadType.append is never called , it is only saving the first page , any reason why , Thank you

  • This is my remote mediator class
 package com.yassir.test.mediator

import androidx.paging.ExperimentalPagingApi
import androidx.paging.LoadType
import androidx.paging.PagingState
import androidx.paging.RemoteMediator
import androidx.room.withTransaction
import com.yassir.test.Utils
import com.yassir.test.auth.AuthService
import com.yassir.test.models.Result
import com.yassir.test.models.keys.RemoteKey
import com.yassir.test.room.MovieDatabase
import timber.log.Timber
import java.io.InvalidObjectException

@OptIn(ExperimentalPagingApi::class)
class MovieRemoteMediator(
    var authService: AuthService,
    var database: MovieDatabase
) : RemoteMediator<Int,Result>() {


    override suspend fun load(loadType: LoadType, state: PagingState<Int, Result>): MediatorResult {
        val page = when(loadType){
            LoadType.REFRESH -> {
                Timber.d("Refresh Called")
                val remoteKeys = getRemoteKeyClosestToCurrentPosition(state)
                remoteKeys?.nextKey?.minus(1) ?: 1
            }

            LoadType.APPEND -> {
                Timber.d("Append Called")
                val remoteKeys = getRemoteKeyForLastItem(state) ?: throw InvalidObjectException("Result is empty")
                remoteKeys.nextKey ?: return MediatorResult.Success(true)
            }

            LoadType.PREPEND -> {
                Timber.d("Prepend  Called")
                return MediatorResult.Success(true)
            }

        }
        val response = authService.getTrendingMovies(Utils.API_KEY,"en-US","popularity.desc",
            include_adult = false, include_video = false,page,"flatrate")

        val endOfPaginationReached = response.results.size < state.config.pageSize

        database.withTransaction {
            // if refreshing, clear table and start over
            if (loadType == LoadType.REFRESH) {
                database.getRemoteKeysDao().clearRemoteKeys()
                database.getMoviesDao().clearAll()
            }
            val prevKey = if (page == 1) null else page - 1
            val nextKey = if (endOfPaginationReached) null else page + 1

            val keys = response.results.map {
                RemoteKey(movieId = it.id.toLong(), prevKey = prevKey, nextKey = nextKey)
            }
            database.getRemoteKeysDao().insertAll(keys)
            database.getMoviesDao().insertAll(response.results)
        }
        return MediatorResult.Success(endOfPaginationReached = endOfPaginationReached)
    }

    private suspend fun getRemoteKeyForLastItem(state: PagingState<Int, Result>): RemoteKey? {
        return state.lastItemOrNull()?.let { movie ->
            database.getRemoteKeysDao().remoteKeysByMoviesId(movie.id.toLong())
        }
    }
    private suspend fun getRemoteKeyClosestToCurrentPosition(state: PagingState<Int, Result>): RemoteKey? {
        return state.anchorPosition?.let { position ->
            state.closestItemToPosition(position)?.id?.let { id ->
                database.getRemoteKeysDao().remoteKeysByMoviesId(id.toLong())
            }
        }
    }

}
0 Answers
Related