Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.bornfight.demo.repository.news
import androidx.paging.ExperimentalPagingApi
import androidx.paging.LoadType
import androidx.paging.PagingState
import androidx.paging.RemoteMediator
import androidx.room.withTransaction
import com.bornfight.common.data.database.AppDatabase
import com.bornfight.common.data.retrofit.ApiInterface
import com.bornfight.demo.model.NewsIte
import com.bornfight.demo.model.NewsRemoteKeys
import retrofit2.HttpException
import java.io.IOException
import java.io.InvalidObjectException
/**
* Make sure to have the same sort from DB as it is from the backend side, otherwise items get mixed up and prevKey
* and nextKey are no longer valid (the scroll might get stuck or the load might loop one of the pages)
*/
@OptIn(ExperimentalPagingApi::class)
class NewsPageKeyedRemoteMediator(
private val initialPage: Int = 1,
private val db: AppDatabase,
private val api: ApiInterface
) : RemoteMediator<Int, NewsItem>() {
override suspend fun load(loadType: LoadType, state: PagingState<Int, NewsItem>): MediatorResult {
return try {
// calculate the current page to load depending on the state
val page = when (loadType) {
LoadType.REFRESH -> {
val remoteKeys = getRemoteKeyClosestToCurrentPosition(state)
remoteKeys?.nextKey?.minus(1) ?: initialPage
}
LoadType.PREPEND -> {
return MediatorResult.Success(true)
}
LoadType.APPEND -> {
val remoteKeys = getRemoteKeyForLastItem(state)
?: throw InvalidObjectException("Result is empty")
remoteKeys.nextKey ?: return MediatorResult.Success(true)
}
}
// load the list of items from API using calculated current page.
// make sure the sort of the remote data and local data is the same!
val response = api.getNews(
isPromoted = null,
showContest = null,
limit = state.config.pageSize,
page = page
).data.sortedByDescending { it.createdAt }
// add custom logic, if you have some API metadata, you can use it as well
val endOfPaginationReached = response.size < state.config.pageSize
db.withTransaction {
// if refreshing, clear table and start over
if (loadType == LoadType.REFRESH) {
db.newsRemoteKeysDao().clearRemoteKeys()
db.newsDao().deleteNewsItems()
}
val prevKey = if (page == initialPage) null else page - 1
val nextKey = if (endOfPaginationReached) null else page + 1
val keys = response.map {
NewsRemoteKeys(newsId = it.id, prevKey = prevKey, nextKey = nextKey)
}
db.newsRemoteKeysDao().insertAll(keys)
db.newsDao().insertNewsList(response)
}
MediatorResult.Success(endOfPaginationReached = endOfPaginationReached)
} catch (e: IOException) {
MediatorResult.Error(e)
} catch (e: HttpException) {
MediatorResult.Error(e)
}
}
private suspend fun getRemoteKeyForLastItem(state: PagingState<Int, NewsItem>): NewsRemoteKeys? {
return state.lastItemOrNull()?.let { news ->
db.withTransaction { db.newsRemoteKeysDao().remoteKeysByMovieId(news.id) }
}
}
private suspend fun getRemoteKeyForFirstItem(state: PagingState<Int, NewsItem>): NewsRemoteKeys? {
return state.firstItemOrNull()?.let { news ->
db.withTransaction { db.newsRemoteKeysDao().remoteKeysByMovieId(news.id) }
}
}
private suspend fun getRemoteKeyClosestToCurrentPosition(state: PagingState<Int, NewsItem>): NewsRemoteKeys? {
return state.anchorPosition?.let { position ->
state.closestItemToPosition(position)?.id?.let { id ->
db.withTransaction { db.newsRemoteKeysDao().remoteKeysByMovieId(id) }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.