From 43a89baf0017f2b16f9e9deb219f2033fb9a8625 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Thu, 13 Nov 2025 08:16:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/model/ReadBook.kt | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/ReadBook.kt b/app/src/main/java/io/legado/app/model/ReadBook.kt index 1a4dda472..4d81bbb19 100644 --- a/app/src/main/java/io/legado/app/model/ReadBook.kt +++ b/app/src/main/java/io/legado/app/model/ReadBook.kt @@ -52,6 +52,7 @@ import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext import splitties.init.appCtx +import java.util.concurrent.ConcurrentHashMap import kotlin.math.max import kotlin.math.min @@ -70,16 +71,14 @@ object ReadBook : CoroutineScope by MainScope() { var prevTextChapter: TextChapter? = null var curTextChapter: TextChapter? = null var nextTextChapter: TextChapter? = null - var prevChapterLoadingJob: Coroutine<*>? = null - var curChapterLoadingJob: Coroutine<*>? = null - var nextChapterLoadingJob: Coroutine<*>? = null - var prevChapterLoadingLock = Mutex() - var curChapterLoadingLock = Mutex() - var nextChapterLoadingLock = Mutex() var bookSource: BookSource? = null var msg: String? = null private val loadingChapters = arrayListOf() private val readRecord = ReadRecord() + private val chapterLoadingJobs = ConcurrentHashMap>() + private val prevChapterLoadingLock = Mutex() + private val curChapterLoadingLock = Mutex() + private val nextChapterLoadingLock = Mutex() var readStartTime: Long = System.currentTimeMillis() /* 跳转进度前进度记录 */ @@ -221,9 +220,7 @@ object ReadBook : CoroutineScope by MainScope() { } fun clearTextChapter() { - prevChapterLoadingJob?.cancel() - curChapterLoadingJob?.cancel() - nextChapterLoadingJob?.cancel() + clearExpiredChapterLoadingJob(true) prevTextChapter = null curTextChapter = null nextTextChapter = null @@ -336,7 +333,7 @@ object ReadBook : CoroutineScope by MainScope() { if (durChapterIndex < simulatedChapterSize - 1) { durChapterPos = 0 durChapterIndex++ - prevChapterLoadingJob?.cancel() + clearExpiredChapterLoadingJob() prevTextChapter = curTextChapter curTextChapter = nextTextChapter nextTextChapter = null @@ -367,7 +364,7 @@ object ReadBook : CoroutineScope by MainScope() { if (durChapterIndex < simulatedChapterSize - 1) { durChapterPos = 0 durChapterIndex++ - prevChapterLoadingJob?.cancel() + clearExpiredChapterLoadingJob() prevTextChapter = curTextChapter curTextChapter = nextTextChapter nextTextChapter = null @@ -399,7 +396,7 @@ object ReadBook : CoroutineScope by MainScope() { if (durChapterIndex > 0) { durChapterPos = if (toLast) prevTextChapter?.lastReadLength ?: Int.MAX_VALUE else 0 durChapterIndex-- - nextChapterLoadingJob?.cancel() + clearExpiredChapterLoadingJob() nextTextChapter = curTextChapter curTextChapter = prevTextChapter prevTextChapter = null @@ -697,12 +694,7 @@ object ReadBook : CoroutineScope by MainScope() { if (canceled || chapter.index !in durChapterIndex - 1..durChapterIndex + 1) { return } - val offset = chapter.index - durChapterIndex - when (offset) { - 0 -> curChapterLoadingJob?.cancel() - -1 -> prevChapterLoadingJob?.cancel() - 1 -> nextChapterLoadingJob?.cancel() - } + chapterLoadingJobs[chapter.index]?.cancel() val job = Coroutine.async(this, start = CoroutineStart.LAZY) { val contentProcessor = ContentProcessor.get(book.name, book.origin) val displayTitle = chapter.getDisplayTitle( @@ -715,7 +707,7 @@ object ReadBook : CoroutineScope by MainScope() { val textChapter = ChapterProvider.getTextChapterAsync( this, book, chapter, displayTitle, contents, simulatedChapterSize ) - when (offset) { + when (val offset = chapter.index - durChapterIndex) { 0 -> curChapterLoadingLock.withLock { withContext(Main) { ensureActive() @@ -776,11 +768,7 @@ object ReadBook : CoroutineScope by MainScope() { }.onSuccess { success?.invoke() } - when (offset) { - 0 -> curChapterLoadingJob = job - -1 -> prevChapterLoadingJob = job - 1 -> nextChapterLoadingJob = job - } + chapterLoadingJobs[chapter.index] = job job.start() } @@ -978,6 +966,17 @@ object ReadBook : CoroutineScope by MainScope() { } } + private fun clearExpiredChapterLoadingJob(clearAll: Boolean = false) { + val iterator = chapterLoadingJobs.iterator() + while (iterator.hasNext()) { + val (index, job) = iterator.next() + if (clearAll || index !in durChapterIndex - 1..durChapterIndex + 1) { + job.cancel() + iterator.remove() + } + } + } + /** * 注册回调 */ @@ -998,6 +997,7 @@ object ReadBook : CoroutineScope by MainScope() { downloadScope.coroutineContext.cancelChildren() coroutineContext.cancelChildren() ImageProvider.clear() + clearExpiredChapterLoadingJob(true) if (!CacheBookService.isRun) { CacheBook.close() }