本地数据的Android分页(无空间)

本地数据的Android分页(无空间),android,paging,Android,Paging,我正在尝试使用分页库,从服务器获取列表,并将其用于本地数据,但我不想使用它的空间(我的应用程序中没有db,也不想只为它添加它), 所以我有了mediator,我正在尝试实现PagingSource。列表应该是可流动的,所以当我删除一个项目时,它会自动更新 调解人 class EventMediator( private val id: String, private val remoteDataSource: EventRemote, private val eventLocalData: Ev

我正在尝试使用分页库,从服务器获取列表,并将其用于本地数据,但我不想使用它的空间(我的应用程序中没有db,也不想只为它添加它), 所以我有了mediator,我正在尝试实现PagingSource。列表应该是可流动的,所以当我删除一个项目时,它会自动更新

调解人

class EventMediator(
private val id: String,
private val remoteDataSource: EventRemote,
private val eventLocalData: EvrntsLocal

) : RemoteMediator<Int, EventItem>() {

var hasNextKey = true

override suspend fun load(
    loadType: LoadType,
    state: PagingState<Int, EventItem>
): MediatorResult {
    try {
        val loadKey = when (loadType) {
            LoadType.REFRESH -> STARTING_MEAL_INDEX
            LoadType.PREPEND -> return MediatorResult.Success(endOfPaginationReached = true)
            LoadType.APPEND -> {

                if (!eventLocalData.hasNextKey) {
                    return MediatorResult.Success(endOfPaginationReached = true)
                }
                eventLocalData.getNumOfMeals()
            }
        }
        val response = remoteDataSource.getEvents(loadKey)

        return if (response is Result.Success) {
            hasNextKey = !response.data.lastPage
            if (loadType == LoadType.REFRESH) {
                eventLocalData.clearMeals()
            }
            eventLocalData.saveMeals(response.data.items)
            MediatorResult.Success(endOfPaginationReached = !hasNextKey)
        } else {
            MediatorResult.Error(IOException("Failed to get Events"))
        }

    } catch (e: IOException) {
        return MediatorResult.Error(e)
    } catch (e: HttpException) {
        return MediatorResult.Error(e)
    }
}
){

private val\u eventChannel=confollatedbroadcastchannel(emptyList())
var hasNextKey:Boolean=true
有趣的观察():流{
返回_eventChannel.asFlow()
}
乐趣获取膳食():列表{
返回\u eventChannel.value
}
趣味套餐(列表:列表){
_eventChannel.offer(_eventChannel.value.plus(列表))
}
fun getnumofedins():Int{
return\u eventChannel.value.size
}
趣味餐{
_eventChannel.offer(emptyList())
}
}

class EventSource(
private val eventLocalData: EvrntsLocal

) : PagingSource<Int, EventItem>() {

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, EventItem> {

    val offset = (params.key ?: STARTING_MEAL_INDEX)
    return try {
        val response = eventLocalData.getMeals()
        LoadResult.Page(
            data = response,
            prevKey = if (offset - NUM_OF_EVENTS <= STARTING_MEAL_INDEX) null else offset - NUM_OF_EVENTS,
            nextKey = if (offset + NUM_OF_EVENTS >= response.size) null else offset + NUM_OF_EVENTS
        )
    } catch (exception: IOException) {
        return LoadResult.Error(exception)
    } catch (exception: HttpException) {
        return LoadResult.Error(exception)
    }
}
    fun getEvents(folderId: String): Flow<PagingData<EventItem>> {
    return Pager(
        config = PagingConfig(50),
        remoteMediator = EventMediator(folderId, remoteDataSource, localDataSource),
        pagingSourceFactory =  { EventSource(localDataSource) }
    ) .flow
}
class EvrntsLocal @Inject constructor(
private val _eventChannel = ConflatedBroadcastChannel<List<EventItem>>(emptyList())
var hasNextKey: Boolean = true

fun observeMeals(): Flow<List<EventItem>> {
    return _eventChannel.asFlow()
}

fun getMeals(): List<EventItem> {
    return _eventChannel.value
}

fun saveMeals(list: List<EventItem>) {
    _eventChannel.offer(_eventChannel.value.plus(list))
}

fun getNumOfMeals(): Int {
    return _eventChannel.value.size
}

fun clearMeals() {
    _eventChannel.offer(emptyList())
}