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.executeOnIO
import exh.util.nullIfBlank import exh.util.nullIfBlank
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope import kotlinx.coroutines.supervisorScope
import timber.log.Timber import timber.log.Timber
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
@ -79,7 +82,7 @@ class LibraryUpdateService(
private lateinit var wakeLock: PowerManager.WakeLock private lateinit var wakeLock: PowerManager.WakeLock
private lateinit var notifier: LibraryUpdateNotifier private lateinit var notifier: LibraryUpdateNotifier
private lateinit var scope: CoroutineScope private lateinit var ioScope: CoroutineScope
private var updateJob: Job? = null private var updateJob: Job? = null
@ -174,7 +177,7 @@ class LibraryUpdateService(
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
scope = MainScope() ioScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
notifier = LibraryUpdateNotifier(this) notifier = LibraryUpdateNotifier(this)
wakeLock = acquireWakeLock(javaClass.name) wakeLock = acquireWakeLock(javaClass.name)
@ -186,7 +189,7 @@ class LibraryUpdateService(
* lock. * lock.
*/ */
override fun onDestroy() { override fun onDestroy() {
scope?.cancel() ioScope?.cancel()
updateJob?.cancel() updateJob?.cancel()
if (wakeLock.isHeld) { if (wakeLock.isHeld) {
wakeLock.release() wakeLock.release()
@ -222,7 +225,7 @@ class LibraryUpdateService(
val mangaList = getMangaToUpdate(intent, target) val mangaList = getMangaToUpdate(intent, target)
.sortedWith(rankingScheme[selectedScheme]) .sortedWith(rankingScheme[selectedScheme])
updateJob = scope.launchIO { updateJob = ioScope.launch {
try { try {
when (target) { when (target) {
Target.CHAPTERS -> updateChapterList(mangaList) Target.CHAPTERS -> updateChapterList(mangaList)
@ -415,17 +418,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()) GlobalScope.launchIO {
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()
}
} }
scope.launchIO { scope.launchIO {