Update trackers in parallel, update manga metadata asynchronously

This commit is contained in:
arkon 2021-01-23 14:55:00 -05:00
parent 7cae3095c4
commit 04a993c997

View File

@ -36,7 +36,10 @@ import eu.kanade.tachiyomi.util.system.isServiceRunning
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope import kotlinx.coroutines.MainScope
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.supervisorScope
import timber.log.Timber import timber.log.Timber
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
@ -245,7 +248,7 @@ class LibraryUpdateService(
*/ */
suspend fun updateChapterList(mangaToUpdate: List<LibraryManga>) { suspend fun updateChapterList(mangaToUpdate: List<LibraryManga>) {
// Initialize the variables holding the progress of the updates. // Initialize the variables holding the progress of the updates.
val count = AtomicInteger(0) val progressCount = AtomicInteger(0)
// List containing new updates // List containing new updates
val newUpdates = mutableListOf<Pair<LibraryManga, Array<Chapter>>>() val newUpdates = mutableListOf<Pair<LibraryManga, Array<Chapter>>>()
// List containing failed updates // List containing failed updates
@ -256,7 +259,7 @@ class LibraryUpdateService(
mangaToUpdate mangaToUpdate
.map { manga -> .map { manga ->
// Notify manga that will update. // Notify manga that will update.
notifier.showProgressNotification(manga, count.andIncrement, mangaToUpdate.size) notifier.showProgressNotification(manga, progressCount.andIncrement, mangaToUpdate.size)
// Update the chapters of the manga // Update the chapters of the manga
try { try {
@ -326,17 +329,19 @@ class LibraryUpdateService(
// Update manga details metadata in the background // Update manga details metadata in the background
if (preferences.autoUpdateMetadata()) { if (preferences.autoUpdateMetadata()) {
val updatedManga = source.getMangaDetails(manga.toMangaInfo()) scope.async {
val sManga = updatedManga.toSManga() val updatedManga = source.getMangaDetails(manga.toMangaInfo())
// Avoid "losing" existing cover val sManga = updatedManga.toSManga()
if (!sManga.thumbnail_url.isNullOrEmpty()) { // Avoid "losing" existing cover
manga.prepUpdateCover(coverCache, sManga, false) if (!sManga.thumbnail_url.isNullOrEmpty()) {
} else { manga.prepUpdateCover(coverCache, sManga, false)
sManga.thumbnail_url = manga.thumbnail_url } else {
} sManga.thumbnail_url = manga.thumbnail_url
}
manga.copyFrom(sManga) manga.copyFrom(sManga)
db.insertManga(manga).executeAsBlocking() db.insertManga(manga).executeAsBlocking()
}
} }
val chapters = source.getChapterList(manga.toMangaInfo()) val chapters = source.getChapterList(manga.toMangaInfo())
@ -346,10 +351,10 @@ class LibraryUpdateService(
} }
private suspend fun updateCovers(mangaToUpdate: List<LibraryManga>) { private suspend fun updateCovers(mangaToUpdate: List<LibraryManga>) {
var count = 0 var progressCount = 0
mangaToUpdate.forEach { manga -> mangaToUpdate.forEach { manga ->
notifier.showProgressNotification(manga, count++, mangaToUpdate.size) notifier.showProgressNotification(manga, progressCount++, mangaToUpdate.size)
sourceManager.get(manga.source)?.let { source -> sourceManager.get(manga.source)?.let { source ->
try { try {
@ -375,28 +380,32 @@ class LibraryUpdateService(
* background thread, so it's safe to do heavy operations or network calls here. * background thread, so it's safe to do heavy operations or network calls here.
*/ */
private suspend fun updateTrackings(mangaToUpdate: List<LibraryManga>) { private suspend fun updateTrackings(mangaToUpdate: List<LibraryManga>) {
// Initialize the variables holding the progress of the updates. var progressCount = 0
var count = 0
val loggedServices = trackManager.services.filter { it.isLogged } val loggedServices = trackManager.services.filter { it.isLogged }
mangaToUpdate.forEach { manga -> mangaToUpdate.forEach { manga ->
// Notify manga that will update. // Notify manga that will update.
notifier.showProgressNotification(manga, count++, mangaToUpdate.size) notifier.showProgressNotification(manga, progressCount++, mangaToUpdate.size)
// Update the tracking details. // Update the tracking details.
db.getTracks(manga).executeAsBlocking().forEach { track -> db.getTracks(manga).executeAsBlocking()
val service = trackManager.getService(track.sync_id) .map { track ->
if (service != null && service in loggedServices) { supervisorScope {
try { async {
val updatedTrack = service.refresh(track) val service = trackManager.getService(track.sync_id)
db.insertTrack(updatedTrack).executeAsBlocking() if (service != null && service in loggedServices) {
} catch (e: Throwable) { try {
// Ignore errors and continue val updatedTrack = service.refresh(track)
Timber.e(e) db.insertTrack(updatedTrack).executeAsBlocking()
} catch (e: Throwable) {
// Ignore errors and continue
Timber.e(e)
}
}
}
} }
} }
} .awaitAll()
} }
notifier.cancelProgressNotification() notifier.cancelProgressNotification()