Don't show update progress notifications if job isn't active anymore (closes #5844)

This commit is contained in:
arkon 2021-09-04 10:24:55 -04:00
parent 2d3a1b6a9e
commit 7083b3d912

View File

@ -294,48 +294,46 @@ class LibraryUpdateService(
return@async return@async
} }
currentlyUpdatingManga.add(manga) withUpdateNotification(
notifier.showProgressNotification(
currentlyUpdatingManga, currentlyUpdatingManga,
progressCount.get(), progressCount,
mangaToUpdate.size manga,
) ) { manga ->
try {
val (newChapters, _) = updateManga(manga)
try { if (newChapters.isNotEmpty()) {
val (newChapters, _) = updateManga(manga) if (manga.shouldDownloadNewChapters(db, preferences)) {
downloadChapters(manga, newChapters)
hasDownloads.set(true)
}
if (newChapters.isNotEmpty()) { // Convert to the manga that contains new chapters
if (manga.shouldDownloadNewChapters(db, preferences)) { newUpdates.add(
downloadChapters(manga, newChapters) manga to newChapters.sortedByDescending { ch -> ch.source_order }
hasDownloads.set(true) .toTypedArray()
)
} }
} catch (e: Throwable) {
// Convert to the manga that contains new chapters val errorMessage = when (e) {
newUpdates.add(manga to newChapters.sortedByDescending { ch -> ch.source_order }.toTypedArray()) 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) { if (preferences.autoUpdateTrackers()) {
getString(R.string.no_chapters_error) updateTrackings(manga, loggedServices)
} 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
} }
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 return@async
} }
currentlyUpdatingManga.add(manga) withUpdateNotification(
notifier.showProgressNotification(
currentlyUpdatingManga, currentlyUpdatingManga,
progressCount.get(), progressCount,
mangaToUpdate.size manga,
) ) { manga ->
sourceManager.get(manga.source)?.let { source ->
sourceManager.get(manga.source)?.let { source -> try {
try { val networkManga =
val networkManga = source.getMangaDetails(manga.toMangaInfo())
source.getMangaDetails(manga.toMangaInfo()) val sManga = networkManga.toSManga()
val sManga = networkManga.toSManga() manga.prepUpdateCover(coverCache, sManga, true)
manga.prepUpdateCover(coverCache, sManga, true) sManga.thumbnail_url?.let {
sManga.thumbnail_url?.let { manga.thumbnail_url = it
manga.thumbnail_url = it db.insertManga(manga).executeAsBlocking()
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) currentlyUpdatingManga.remove(manga)
progressCount.andIncrement progressCount.andIncrement
notifier.showProgressNotification( notifier.showProgressNotification(
currentlyUpdatingManga, currentlyUpdatingManga,
progressCount.get(), progressCount.get(),
mangaToUpdate.size mangaToUpdate.size
) )
}
} }
} }
} }
@ -506,6 +503,38 @@ class LibraryUpdateService(
.awaitAll() .awaitAll()
} }
private suspend fun withUpdateNotification(
updatingManga: CopyOnWriteArrayList<LibraryManga>,
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. * Writes basic file of update errors to cache dir.
*/ */