Use app scoped CoroutineScope (#3403)

This commit is contained in:
AntsyLich
2026-06-12 17:14:29 +06:00
committed by GitHub
parent 63a71fa447
commit 509eee5dfb
17 changed files with 91 additions and 80 deletions
@@ -286,7 +286,7 @@ class ReaderViewModel @JvmOverloads constructor(
val context = Injekt.get<Application>()
val source = sourceManager.getOrStub(manga.source)
loader = ChapterLoader(context, downloadManager, downloadProvider, manga, source)
loader = ChapterLoader(context, viewModelScope, downloadManager, downloadProvider, manga, source)
loadChapter(loader!!, chapterList.first { chapterId == it.chapter.id })
Result.success(true)
@@ -6,6 +6,7 @@ import eu.kanade.tachiyomi.data.download.DownloadProvider
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
import kotlinx.coroutines.CoroutineScope
import mihon.core.archive.archiveReader
import mihon.core.archive.epubReader
import tachiyomi.core.common.i18n.stringResource
@@ -22,6 +23,7 @@ import tachiyomi.source.local.io.Format
*/
class ChapterLoader(
private val context: Context,
private val scope: CoroutineScope,
private val downloadManager: DownloadManager,
private val downloadProvider: DownloadProvider,
private val manga: Manga,
@@ -100,7 +102,7 @@ class ChapterLoader(
is Format.Epub -> EpubPageLoader(format.file.epubReader(context))
}
}
source is HttpSource -> HttpPageLoader(chapter, source)
source is HttpSource -> HttpPageLoader(chapter, source, scope)
source is StubSource -> error(context.stringResource(MR.strings.source_not_installed, source.toString()))
else -> error(context.stringResource(MR.strings.loader_not_implemented_error))
}
@@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.ui.reader.loader
import android.provider.Settings
import eu.kanade.tachiyomi.data.cache.ChapterCache
import eu.kanade.tachiyomi.data.database.models.toDomainChapter
import eu.kanade.tachiyomi.source.model.Page
@@ -8,7 +9,9 @@ import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.filter
@@ -31,11 +34,10 @@ import kotlin.math.min
internal class HttpPageLoader(
private val chapter: ReaderChapter,
private val source: HttpSource,
scope: CoroutineScope,
private val chapterCache: ChapterCache = Injekt.get(),
) : PageLoader() {
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
/**
* A queue used to manage requests one by one while allowing priorities.
*/
@@ -124,14 +126,14 @@ internal class HttpPageLoader(
queue.offer(PriorityPage(page, PriorityPage.RETRY))
}
@OptIn(DelicateCoroutinesApi::class)
override fun recycle() {
super.recycle()
scope.cancel()
queue.clear()
// Cache current page list progress for online chapters to allow a faster reopen
chapter.pages?.let { pages ->
launchIO {
GlobalScope.launchIO {
try {
// Convert to pages without reader information
val pagesToSave = pages.map { Page(it.index, it.url, it.imageUrl) }