From 2d0bf560e5095e7d5e31f5fab56ffe17d3f80994 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Tue, 28 Mar 2023 14:45:52 -0400 Subject: [PATCH] More updates to the library updates flow no longer using a suspend, instead holding a buffer for the flow --- .../data/library/LibraryUpdateJob.kt | 34 +++++++++---------- .../ui/manga/MangaDetailsPresenter.kt | 13 ++++--- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt index b68f96286492..96cb1acb4aaa 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateJob.kt @@ -61,6 +61,7 @@ import kotlinx.coroutines.MainScope import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.ensureActive import kotlinx.coroutines.flow.MutableSharedFlow @@ -171,7 +172,7 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet } catch (e: Exception) { if (e is CancellationException) { // Assume success although cancelled - finishUpdates() + finishUpdates(true) Result.success() } else { Timber.e(e) @@ -319,26 +320,22 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet notifier.cancelProgressNotification() } - private suspend fun finishUpdates() { - coroutineScope { - if (!isStopped) { - extraDeferredJobs.awaitAll() - } + private suspend fun finishUpdates(wasStopped: Boolean = false) { + if (!wasStopped && !isStopped) { + extraDeferredJobs.awaitAll() } if (newUpdates.isNotEmpty()) { notifier.showResultNotification(newUpdates) - coroutineScope { - if (preferences.refreshCoversToo().get() && !isStopped) { - updateDetails(newUpdates.keys.toList()) - notifier.cancelProgressNotification() - if (downloadNew && hasDownloads) { - DownloadJob.start(context, runExtensionUpdatesAfter) - runExtensionUpdatesAfter = false - } - } else if (downloadNew && hasDownloads) { - DownloadJob.start(this@LibraryUpdateJob.applicationContext, runExtensionUpdatesAfter) + if (!wasStopped && preferences.refreshCoversToo().get() && !isStopped) { + updateDetails(newUpdates.keys.toList()) + notifier.cancelProgressNotification() + if (downloadNew && hasDownloads) { + DownloadJob.start(context, runExtensionUpdatesAfter) runExtensionUpdatesAfter = false } + } else if (downloadNew && hasDownloads) { + DownloadJob.start(applicationContext, runExtensionUpdatesAfter) + runExtensionUpdatesAfter = false } } newUpdates.clear() @@ -627,7 +624,10 @@ class LibraryUpdateJob(private val context: Context, workerParams: WorkerParamet private var instance: WeakReference? = null - val updateMutableFlow = MutableSharedFlow() + val updateMutableFlow = MutableSharedFlow( + extraBufferCapacity = 10, + onBufferOverflow = BufferOverflow.DROP_OLDEST, + ) val updateFlow = updateMutableFlow.asSharedFlow() private var runExtensionUpdatesAfter = false diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt index 6920a6fa47eb..5987e0270d3d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt @@ -57,6 +57,7 @@ import eu.kanade.tachiyomi.widget.TriStateCheckBox import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch @@ -114,10 +115,12 @@ class MangaDetailsPresenter( tabletChapterHeaderItem = MangaHeaderItem(manga, false) tabletChapterHeaderItem?.isChapterHeader = true } - isLockedFromSearch = controller.shouldLockIfNeeded && SecureActivityDelegate.shouldBeLocked() + isLockedFromSearch = + controller.shouldLockIfNeeded && SecureActivityDelegate.shouldBeLocked() headerItem.isLocked = isLockedFromSearch downloadManager.addListener(this) - LibraryUpdateJob.updateFlow.onEach(::onUpdateManga).launchIn(presenterScope) + LibraryUpdateJob.updateFlow.filter { it == manga.id } + .onEach(::onUpdateManga).launchIn(presenterScope) tracks = db.getTracks(manga).executeAsBlocking() if (manga.isLocal()) { refreshAll() @@ -694,11 +697,7 @@ class MangaDetailsPresenter( } } - private fun onUpdateManga(mangaId: Long?) { - if (mangaId == manga.id) { - fetchChapters() - } - } + private fun onUpdateManga(mangaId: Long?) = fetchChapters() fun shareManga() { val context = Injekt.get()