From 147db05c195aa25c191ede489b14710159be4f7e Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Wed, 5 Nov 2025 22:01:32 +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 | 46 ++++++++++++++----- .../manga/entities/EpaperTransformation.kt | 2 +- .../read/page/provider/TextChapterLayout.kt | 12 +++-- 3 files changed, 42 insertions(+), 18 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 70396d6e4..c4d0ef32a 100644 --- a/app/src/main/java/io/legado/app/model/ReadBook.kt +++ b/app/src/main/java/io/legado/app/model/ReadBook.kt @@ -33,8 +33,11 @@ import io.legado.app.ui.book.read.page.provider.LayoutProgressListener import io.legado.app.utils.postEvent import io.legado.app.utils.stackTraceStr import io.legado.app.utils.toastOnUi +import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Job import kotlinx.coroutines.MainScope import kotlinx.coroutines.SupervisorJob @@ -681,11 +684,11 @@ object ReadBook : CoroutineScope by MainScope() { canceled: Boolean = false, success: (() -> Unit)? = null ) { - removeLoading(chapter.index) if (canceled || chapter.index !in durChapterIndex - 1..durChapterIndex + 1) { + removeLoading(chapter.index) return } - Coroutine.async { + Coroutine.async(start = CoroutineStart.LAZY) { val contentProcessor = ContentProcessor.get(book.name, book.origin) val displayTitle = chapter.getDisplayTitle( contentProcessor.getTitleReplaceRules(), @@ -699,7 +702,9 @@ object ReadBook : CoroutineScope by MainScope() { when (val offset = chapter.index - durChapterIndex) { 0 -> { curTextChapter?.cancelLayout() - curTextChapter = textChapter + withContext(Main) { + curTextChapter = textChapter + } callBack?.upMenuView() var available = false for (page in textChapter.layoutChannel) { @@ -724,14 +729,18 @@ object ReadBook : CoroutineScope by MainScope() { -1 -> { prevTextChapter?.cancelLayout() - prevTextChapter = textChapter + withContext(Main) { + prevTextChapter = textChapter + } textChapter.layoutChannel.receiveAsFlow().collect() if (upContent) callBack?.upContent(offset, resetPageOffset) } 1 -> { nextTextChapter?.cancelLayout() - nextTextChapter = textChapter + withContext(Main) { + nextTextChapter = textChapter + } for (page in textChapter.layoutChannel) { if (page.index > 1) { continue @@ -743,11 +752,16 @@ object ReadBook : CoroutineScope by MainScope() { return@async }.onError { + if (it is CancellationException) { + return@onError + } AppLog.put("ChapterProvider ERROR", it) appCtx.toastOnUi("ChapterProvider ERROR:\n${it.stackTraceStr}") }.onSuccess { success?.invoke() - } + }.onFinally { + removeLoading(chapter.index) + }.start() } suspend fun contentLoadFinishAwait( @@ -757,8 +771,8 @@ object ReadBook : CoroutineScope by MainScope() { upContent: Boolean = true, resetPageOffset: Boolean ) { - removeLoading(chapter.index) if (chapter.index !in durChapterIndex - 1..durChapterIndex + 1) { + removeLoading(chapter.index) return } kotlin.runCatching { @@ -775,7 +789,9 @@ object ReadBook : CoroutineScope by MainScope() { when (val offset = chapter.index - durChapterIndex) { 0 -> { curTextChapter?.cancelLayout() - curTextChapter = textChapter + withContext(Main) { + curTextChapter = textChapter + } callBack?.upMenuView() var available = false for (page in textChapter.layoutChannel) { @@ -800,14 +816,18 @@ object ReadBook : CoroutineScope by MainScope() { -1 -> { prevTextChapter?.cancelLayout() - prevTextChapter = textChapter + withContext(Main) { + prevTextChapter = textChapter + } textChapter.layoutChannel.receiveAsFlow().collect() if (upContent) callBack?.upContent(offset, resetPageOffset) } 1 -> { nextTextChapter?.cancelLayout() - nextTextChapter = textChapter + withContext(Main) { + nextTextChapter = textChapter + } for (page in textChapter.layoutChannel) { if (page.index > 1) { continue @@ -816,12 +836,14 @@ object ReadBook : CoroutineScope by MainScope() { } } } - - return }.onFailure { + if (it is CancellationException) { + return@onFailure + } AppLog.put("ChapterProvider ERROR", it) appCtx.toastOnUi("ChapterProvider ERROR:\n${it.stackTraceStr}") } + removeLoading(chapter.index) } @Synchronized diff --git a/app/src/main/java/io/legado/app/ui/book/manga/entities/EpaperTransformation.kt b/app/src/main/java/io/legado/app/ui/book/manga/entities/EpaperTransformation.kt index b63c72ac2..dbba916d2 100644 --- a/app/src/main/java/io/legado/app/ui/book/manga/entities/EpaperTransformation.kt +++ b/app/src/main/java/io/legado/app/ui/book/manga/entities/EpaperTransformation.kt @@ -19,7 +19,7 @@ import java.security.MessageDigest * 仅当applyBinarization为true时有效。 */ class EpaperTransformation( - @IntRange(0, 255) private val threshold: Int = 128, + @param:IntRange(0, 255) private val threshold: Int = 128, ) : BitmapTransformation() { private val ID = diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/TextChapterLayout.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/TextChapterLayout.kt index 7376ddc37..ee4e8db3c 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/TextChapterLayout.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/TextChapterLayout.kt @@ -30,10 +30,10 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.ensureActive import kotlinx.coroutines.launch import java.util.LinkedList -import kotlin.coroutines.coroutineContext import kotlin.math.roundToInt class TextChapterLayout( @@ -109,6 +109,8 @@ class TextChapterLayout( }.onError { exception = it onException(it) + }.onCancel { + channel.cancel() }.onFinally { isCompleted = true } @@ -227,7 +229,7 @@ class TextChapterLayout( val sb = StringBuffer() var isSetTypedImage = false contents.forEach { content -> - coroutineContext.ensureActive() + currentCoroutineContext().ensureActive() if (isTextImageStyle) { //图片样式为文字嵌入类型 var text = content.replace(ChapterProvider.srcReplaceChar, "▣") @@ -260,7 +262,7 @@ class TextChapterLayout( if (content.contains("