From 7e92921f84905fefa46a9514c096593520c52076 Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 14 Oct 2022 15:43:27 -0400 Subject: [PATCH] Attempt to avoid duplicate update rows Related to #7713 --- .../browse/extension/ExtensionsPresenter.kt | 24 ++++++------ .../ui/recent/updates/UpdatesPresenter.kt | 38 ++++++++++--------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsPresenter.kt index 46b334e72..83818be2f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/ExtensionsPresenter.kt @@ -22,7 +22,6 @@ import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach -import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.update import rx.Observable import uy.kohesive.injekt.Injekt @@ -78,9 +77,9 @@ class ExtensionsPresenter( presenterScope.launchIO { combine( _query, - getExtensions.subscribe().stateIn(presenterScope), _currentDownloads, - ) { query, (_updates, _installed, _available, _untrusted), downloads -> + getExtensions.subscribe(), + ) { query, downloads, (_updates, _installed, _available, _untrusted) -> val searchQuery = query ?: "" val languagesWithExtensions = _available @@ -137,15 +136,16 @@ class ExtensionsPresenter( fun updateAllExtensions() { presenterScope.launchIO { if (state.isEmpty) return@launchIO - val items = state.items - items.mapNotNull { - if (it !is ExtensionUiModel.Item) return@mapNotNull null - if (it.extension !is Extension.Installed) return@mapNotNull null - if (it.extension.hasUpdate.not()) return@mapNotNull null - it.extension - }.forEach { - updateExtension(it) - } + state.items + .mapNotNull { + when { + it !is ExtensionUiModel.Item -> null + it.extension !is Extension.Installed -> null + !it.extension.hasUpdate -> null + else -> it.extension + } + } + .forEach { updateExtension(it) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt index 46f131c93..288c74d2c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt @@ -102,25 +102,27 @@ class UpdatesPresenter( } private fun List.toUpdateItems(): List { - return this.map { update -> - val activeDownload = downloadManager.queue.find { update.chapterId == it.chapter.id } - val downloaded = downloadManager.isChapterDownloaded( - update.chapterName, - update.scanlator, - update.mangaTitle, - update.sourceId, - ) - val downloadState = when { - activeDownload != null -> activeDownload.status - downloaded -> Download.State.DOWNLOADED - else -> Download.State.NOT_DOWNLOADED + return this + .distinctBy { it.chapterId } + .map { + val activeDownload = downloadManager.queue.find { download -> it.chapterId == download.chapter.id } + val downloaded = downloadManager.isChapterDownloaded( + it.chapterName, + it.scanlator, + it.mangaTitle, + it.sourceId, + ) + val downloadState = when { + activeDownload != null -> activeDownload.status + downloaded -> Download.State.DOWNLOADED + else -> Download.State.NOT_DOWNLOADED + } + UpdatesItem( + update = it, + downloadStateProvider = { downloadState }, + downloadProgressProvider = { activeDownload?.progress ?: 0 }, + ) } - UpdatesItem( - update = update, - downloadStateProvider = { downloadState }, - downloadProgressProvider = { activeDownload?.progress ?: 0 }, - ) - } } private suspend fun observeDownloads() {