From 7083b3d912a93b774f80adf760fdc32540c09b8d Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 4 Sep 2021 10:24:55 -0400 Subject: [PATCH] Don't show update progress notifications if job isn't active anymore (closes #5844) --- .../data/library/LibraryUpdateService.kt | 151 +++++++++++------- 1 file changed, 90 insertions(+), 61 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index 0cc8148f72..d23a5b5e26 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -294,48 +294,46 @@ class LibraryUpdateService( return@async } - currentlyUpdatingManga.add(manga) - notifier.showProgressNotification( + withUpdateNotification( currentlyUpdatingManga, - progressCount.get(), - mangaToUpdate.size - ) + progressCount, + manga, + ) { manga -> + try { + val (newChapters, _) = updateManga(manga) - try { - val (newChapters, _) = updateManga(manga) + if (newChapters.isNotEmpty()) { + if (manga.shouldDownloadNewChapters(db, preferences)) { + downloadChapters(manga, newChapters) + hasDownloads.set(true) + } - if (newChapters.isNotEmpty()) { - if (manga.shouldDownloadNewChapters(db, preferences)) { - downloadChapters(manga, newChapters) - hasDownloads.set(true) + // Convert to the manga that contains new chapters + newUpdates.add( + manga to newChapters.sortedByDescending { ch -> ch.source_order } + .toTypedArray() + ) } - - // Convert to the manga that contains new chapters - newUpdates.add(manga to newChapters.sortedByDescending { ch -> ch.source_order }.toTypedArray()) + } catch (e: Throwable) { + val errorMessage = when (e) { + is NoChaptersException -> { + getString(R.string.no_chapters_error) + } + is SourceManager.SourceNotInstalledException -> { + // failedUpdates will already have the source, don't need to copy it into the message + getString(R.string.loader_not_implemented_error) + } + else -> { + e.message + } + } + failedUpdates.add(manga to errorMessage) } - } catch (e: Throwable) { - val errorMessage = if (e is NoChaptersException) { - getString(R.string.no_chapters_error) - } else if (e is SourceManager.SourceNotInstalledException) { - // failedUpdates will already have the source, don't need to copy it into the message - getString(R.string.loader_not_implemented_error) - } else { - e.message + + if (preferences.autoUpdateTrackers()) { + updateTrackings(manga, loggedServices) } - failedUpdates.add(manga to errorMessage) } - - if (preferences.autoUpdateTrackers()) { - updateTrackings(manga, loggedServices) - } - - currentlyUpdatingManga.remove(manga) - progressCount.andIncrement - notifier.showProgressNotification( - currentlyUpdatingManga, - progressCount.get(), - mangaToUpdate.size - ) } } } @@ -418,36 +416,35 @@ class LibraryUpdateService( return@async } - currentlyUpdatingManga.add(manga) - notifier.showProgressNotification( + withUpdateNotification( currentlyUpdatingManga, - progressCount.get(), - mangaToUpdate.size - ) - - sourceManager.get(manga.source)?.let { source -> - try { - val networkManga = - source.getMangaDetails(manga.toMangaInfo()) - val sManga = networkManga.toSManga() - manga.prepUpdateCover(coverCache, sManga, true) - sManga.thumbnail_url?.let { - manga.thumbnail_url = it - db.insertManga(manga).executeAsBlocking() + progressCount, + manga, + ) { manga -> + sourceManager.get(manga.source)?.let { source -> + try { + val networkManga = + source.getMangaDetails(manga.toMangaInfo()) + val sManga = networkManga.toSManga() + manga.prepUpdateCover(coverCache, sManga, true) + sManga.thumbnail_url?.let { + manga.thumbnail_url = it + db.insertManga(manga).executeAsBlocking() + } + } catch (e: Throwable) { + // Ignore errors and continue + Timber.e(e) } - } catch (e: Throwable) { - // Ignore errors and continue - Timber.e(e) } - } - currentlyUpdatingManga.remove(manga) - progressCount.andIncrement - notifier.showProgressNotification( - currentlyUpdatingManga, - progressCount.get(), - mangaToUpdate.size - ) + currentlyUpdatingManga.remove(manga) + progressCount.andIncrement + notifier.showProgressNotification( + currentlyUpdatingManga, + progressCount.get(), + mangaToUpdate.size + ) + } } } } @@ -506,6 +503,38 @@ class LibraryUpdateService( .awaitAll() } + private suspend fun withUpdateNotification( + updatingManga: CopyOnWriteArrayList, + completed: AtomicInteger, + manga: LibraryManga, + block: suspend (LibraryManga) -> Unit, + ) { + if (updateJob?.isActive != true) { + return + } + + updatingManga.add(manga) + notifier.showProgressNotification( + updatingManga, + completed.get(), + mangaToUpdate.size + ) + + block(manga) + + if (updateJob?.isActive != true) { + return + } + + updatingManga.remove(manga) + completed.andIncrement + notifier.showProgressNotification( + updatingManga, + completed.get(), + mangaToUpdate.size + ) + } + /** * Writes basic file of update errors to cache dir. */