Clean up LibraryUpdateService a bit

This commit is contained in:
arkon 2021-02-09 17:44:22 -05:00
parent 545bc0e605
commit 5af68186d6

View File

@ -253,48 +253,35 @@ class LibraryUpdateService(
val failedUpdates = mutableListOf<Pair<Manga, String?>>() val failedUpdates = mutableListOf<Pair<Manga, String?>>()
var hasDownloads = false var hasDownloads = false
mangaToUpdate mangaToUpdate.forEach { manga ->
.map { manga -> if (updateJob?.isActive != true) {
if (updateJob?.isActive != true) { return
return }
}
// Notify manga that will update. notifier.showProgressNotification(manga, progressCount.andIncrement, mangaToUpdate.size)
notifier.showProgressNotification(manga, progressCount.andIncrement, mangaToUpdate.size)
// Update the chapters of the manga try {
try { val (newChapters, _) = updateManga(manga)
val newChapters = updateManga(manga).first
Pair(manga, newChapters) if (newChapters.isNotEmpty()) {
} catch (e: Throwable) { if (manga.shouldDownloadNewChapters(db, preferences)) {
// If there's any error, return empty update and continue. downloadChapters(manga, newChapters)
val errorMessage = if (e is NoChaptersException) { hasDownloads = true
getString(R.string.no_chapters_error)
} else {
e.message
} }
failedUpdates.add(Pair(manga, errorMessage))
Pair(manga, emptyList())
}
}
// Filter out mangas without new chapters (or failed).
.filter { (_, newChapters) -> newChapters.isNotEmpty() }
.forEach { (manga, newChapters) ->
if (manga.shouldDownloadNewChapters(db, preferences)) {
downloadChapters(manga, newChapters)
hasDownloads = true
}
// Convert to the manga that contains new chapters. // Convert to the manga that contains new chapters
newUpdates.add( newUpdates.add(manga to newChapters.sortedByDescending { ch -> ch.source_order }.toTypedArray())
Pair( }
manga, } catch (e: Throwable) {
newChapters.sortedByDescending { ch -> ch.source_order }.toTypedArray() val errorMessage = if (e is NoChaptersException) {
) getString(R.string.no_chapters_error)
) } else {
e.message
}
failedUpdates.add(manga to errorMessage)
} }
}
// Notify result of the overall update.
notifier.cancelProgressNotification() notifier.cancelProgressNotification()
if (newUpdates.isNotEmpty()) { if (newUpdates.isNotEmpty()) {