From b29c970865c72e08abfa9a471a6674003b8aacc8 Mon Sep 17 00:00:00 2001 From: Christine Coenen Date: Sun, 14 Apr 2024 11:48:20 +0200 Subject: [PATCH] Removed duplicate entries when scrolling the mediathek list #279 --- .../mediathek/api/MediathekPagingSource.kt | 21 +++++-------------- app/src/main/res/raw/changelog.md | 3 +++ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/de/christinecoenen/code/zapp/app/mediathek/api/MediathekPagingSource.kt b/app/src/main/java/de/christinecoenen/code/zapp/app/mediathek/api/MediathekPagingSource.kt index 7ef2a8a5..e066ca21 100644 --- a/app/src/main/java/de/christinecoenen/code/zapp/app/mediathek/api/MediathekPagingSource.kt +++ b/app/src/main/java/de/christinecoenen/code/zapp/app/mediathek/api/MediathekPagingSource.kt @@ -17,17 +17,8 @@ class MediathekPagingSource( ) : PagingSource() { override fun getRefreshKey(state: PagingState): Int? { - // Try to find the page key of the closest page to anchorPosition, from - // either the prevKey or the nextKey, but you need to handle nullability - // here: - // * prevKey == null -> anchorPage is the first page. - // * nextKey == null -> anchorPage is the last page. - // * both prevKey and nextKey null -> anchorPage is the initial page, so - // just return null. - return state.anchorPosition?.let { anchorPosition -> - val anchorPage = state.closestPageToPosition(anchorPosition) - anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1) - } + return ((state.anchorPosition ?: 0) - state.config.initialLoadSize / 2) + .coerceAtLeast(0) } override suspend fun load(params: LoadParams): LoadResult { @@ -35,22 +26,20 @@ class MediathekPagingSource( queryInfoResultPublisher.emit(null) return try { - // Start refresh at page 1 if undefined. - val nextPageNumber = params.key ?: 1 query.size = params.loadSize - query.offset = nextPageNumber.minus(1) * params.loadSize + query.offset = params.key ?: 0 val response = mediathekApi.listShows(query) val showList = response.result?.results ?: throw Error(response.err) - val nextKey = if (showList.isEmpty()) null else nextPageNumber.plus(1) + val nextOffset = if (showList.size < query.size) null else query.offset + query.size queryInfoResultPublisher.emit(response.result.queryInfo) LoadResult.Page( data = showList, prevKey = null, // Only paging forward. - nextKey = nextKey + nextKey = nextOffset ) } catch (e: IOException) { // IOException for network failures. diff --git a/app/src/main/res/raw/changelog.md b/app/src/main/res/raw/changelog.md index a275528d..85828ea7 100644 --- a/app/src/main/res/raw/changelog.md +++ b/app/src/main/res/raw/changelog.md @@ -1,3 +1,6 @@ +# v-next +* Duplikate beim Scrollen in der Mediathek-Liste entfernt + # 8.5.1 * Dynamische Farben wurden auf manchen Geräten nicht übernommen