Update manga from differernt sources in parallel

This commit is contained in:
Jobobby04 2021-02-04 19:05:35 -05:00
parent 9a3faad499
commit e26ab22e41

View File

@ -32,6 +32,7 @@ import eu.kanade.tachiyomi.ui.library.LibraryGroup
import eu.kanade.tachiyomi.ui.manga.track.TrackItem
import eu.kanade.tachiyomi.util.chapter.NoChaptersException
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
import eu.kanade.tachiyomi.util.lang.withIOContext
import eu.kanade.tachiyomi.util.prepUpdateCover
import eu.kanade.tachiyomi.util.shouldDownloadNewChapters
import eu.kanade.tachiyomi.util.storage.getUriCompat
@ -58,6 +59,8 @@ import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import timber.log.Timber
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
@ -327,13 +330,20 @@ class LibraryUpdateService(
* @return an observable delivering the progress of each update.
*/
suspend fun updateChapterList(mangaToUpdate: List<LibraryManga>) {
val semaphore = Semaphore(5)
val progressCount = AtomicInteger(0)
val newUpdates = mutableListOf<Pair<LibraryManga, Array<Chapter>>>()
val failedUpdates = mutableListOf<Pair<Manga, String?>>()
var hasDownloads = false
mangaToUpdate
.mapNotNull { manga ->
withIOContext {
mangaToUpdate.groupBy { it.source }
.filterNot { it.key in LIBRARY_UPDATE_EXCLUDED_SOURCES }
.values.map { mangaInSource ->
async {
semaphore.withPermit {
mangaInSource
.map { manga ->
if (updateJob?.isActive != true) {
throw CancellationException()
}
@ -341,9 +351,6 @@ class LibraryUpdateService(
// Notify manga that will update.
notifier.showProgressNotification(manga, progressCount.andIncrement, mangaToUpdate.size)
// SY -->
if (manga.source in LIBRARY_UPDATE_EXCLUDED_SOURCES) return@mapNotNull null
// SY <--
// Update the chapters of the manga
try {
val newChapters = updateManga(manga).first
@ -375,6 +382,10 @@ class LibraryUpdateService(
)
)
}
}
}
}.awaitAll()
}
// Notify result of the overall update.
notifier.cancelProgressNotification()