From 4c347862f86bc19c257d1182b32f37c148c8c90f Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 25 Mar 2023 09:53:30 +0200 Subject: [PATCH] Remove pages duplicates #309 --- .../core/parser/RemoteMangaRepository.kt | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/koitharu/kotatsu/core/parser/RemoteMangaRepository.kt b/app/src/main/java/org/koitharu/kotatsu/core/parser/RemoteMangaRepository.kt index 12420b0ad..a99cd9acc 100644 --- a/app/src/main/java/org/koitharu/kotatsu/core/parser/RemoteMangaRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/core/parser/RemoteMangaRepository.kt @@ -1,5 +1,6 @@ package org.koitharu.kotatsu.core.parser +import android.util.Log import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -9,6 +10,7 @@ import kotlinx.coroutines.currentCoroutineContext import okhttp3.Headers import okhttp3.Interceptor import okhttp3.Response +import org.koitharu.kotatsu.BuildConfig import org.koitharu.kotatsu.core.cache.ContentCache import org.koitharu.kotatsu.core.cache.SafeDeferred import org.koitharu.kotatsu.core.prefs.SourceSettings @@ -80,7 +82,7 @@ class RemoteMangaRepository( override suspend fun getPages(chapter: MangaChapter): List { cache.getPages(source, chapter.url)?.let { return it } val pages = asyncSafe { - parser.getPages(chapter) + parser.getPages(chapter).distinctById() } cache.putPages(source, chapter.url, pages) return pages.await() @@ -115,4 +117,20 @@ class RemoteMangaRepository( }, ) } + + private fun List.distinctById(): List { + if (isEmpty()) { + return emptyList() + } + val result = ArrayList(size) + val set = HashSet(size) + for (page in this) { + if (set.add(page.id)) { + result.add(page) + } else if (BuildConfig.DEBUG) { + Log.w(null, "Duplicate page: $page") + } + } + return result + } }