Perform metadata update in global scope

(cherry picked from commit e4dc35674d680b10df2f059a5a51b01f892f9861)
This commit is contained in:
arkon 2021-01-24 10:33:29 -05:00 committed by Jobobby04
parent bf7af0c099
commit 6fdff1b03b

View File

@ -48,11 +48,14 @@ import exh.source.mangaDexSourceIds
import exh.util.executeOnIO
import exh.util.nullIfBlank
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope
import timber.log.Timber
import uy.kohesive.injekt.Injekt
@ -79,7 +82,7 @@ class LibraryUpdateService(
private lateinit var wakeLock: PowerManager.WakeLock
private lateinit var notifier: LibraryUpdateNotifier
private lateinit var scope: CoroutineScope
private lateinit var ioScope: CoroutineScope
private var updateJob: Job? = null
@ -174,7 +177,7 @@ class LibraryUpdateService(
override fun onCreate() {
super.onCreate()
scope = MainScope()
ioScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
notifier = LibraryUpdateNotifier(this)
wakeLock = acquireWakeLock(javaClass.name)
@ -186,7 +189,7 @@ class LibraryUpdateService(
* lock.
*/
override fun onDestroy() {
scope?.cancel()
ioScope?.cancel()
updateJob?.cancel()
if (wakeLock.isHeld) {
wakeLock.release()
@ -222,7 +225,7 @@ class LibraryUpdateService(
val mangaList = getMangaToUpdate(intent, target)
.sortedWith(rankingScheme[selectedScheme])
updateJob = scope.launchIO {
updateJob = ioScope.launch {
try {
when (target) {
Target.CHAPTERS -> updateChapterList(mangaList)
@ -415,17 +418,19 @@ class LibraryUpdateService(
// Update manga details metadata in the background
if (preferences.autoUpdateMetadata()) {
val updatedManga = source.getMangaDetails(manga.toMangaInfo())
val sManga = updatedManga.toSManga()
// Avoid "losing" existing cover
if (!sManga.thumbnail_url.isNullOrEmpty()) {
manga.prepUpdateCover(coverCache, sManga, false)
} else {
sManga.thumbnail_url = manga.thumbnail_url
}
GlobalScope.launchIO {
val updatedManga = source.getMangaDetails(manga.toMangaInfo())
val sManga = updatedManga.toSManga()
// Avoid "losing" existing cover
if (!sManga.thumbnail_url.isNullOrEmpty()) {
manga.prepUpdateCover(coverCache, sManga, false)
} else {
sManga.thumbnail_url = manga.thumbnail_url
}
manga.copyFrom(sManga)
db.insertManga(manga).executeAsBlocking()
manga.copyFrom(sManga)
db.insertManga(manga).executeAsBlocking()
}
}
scope.launchIO {