Код: Выделить всё
override suspend fun load(
loadType: LoadType,
state: PagingState
): MediatorResult {
val page = when(loadType) {
LoadType.REFRESH -> 0
LoadType.PREPEND -> return MediatorResult.Success(
endOfPaginationReached = true
)
LoadType.APPEND -> {
val lastItem = state.lastItemOrNull()
if (lastItem == null) {
return MediatorResult.Success(endOfPaginationReached = true)
} else {
lastItem.id / state.config.pageSize
}
}
}
return try {
val pokemonList = pokemonApi.getPokemonList(
limit = state.config.pageSize,
offset = page * state.config.pageSize
)
val endOfPaginationReached = pokemonList.results.isEmpty()
pokemonDb.withTransaction {
if (loadType == LoadType.REFRESH) {
pokemonDb.dao.clearAll()
}
val pokemonEntities = pokemonList.results.map { it.toPokemonEntity() }
pokemonDb.dao.upsertAll(pokemonEntities)
}
return MediatorResult.Success(
endOfPaginationReached = endOfPaginationReached
)
} catch (e: Exception) {
MediatorResult.Error(e)
}
}
Код: Выделить всё
@OptIn(ExperimentalPagingApi::class)
@Provides
@Singleton
fun providePokemonPager(pokemonApi: PokemonApi, pokemonDatabase: PokemonDatabase): Pager {
return Pager(
config = PagingConfig(pageSize = PokemonApi.PAGE_SIZE),
remoteMediator = PokemonRemoteMediator(
pokemonDb = pokemonDatabase,
pokemonApi = pokemonApi
),
pagingSourceFactory = { pokemonDatabase.dao.pagingSource() }
)
}
Есть предложения? Tysm
Я уже пытался аннулировать pagingSource, но не знаю, что делать
Подробнее здесь: https://stackoverflow.com/questions/790 ... ving-items
Мобильная версия