From 7d978ebd6e135477a0cc31321674dd971a76588c Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Sun, 3 Mar 2024 23:14:39 +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 --- .../legado/app/ui/book/read/page/entities/TextChapter.kt | 2 ++ .../app/ui/book/read/page/provider/ChapterProvider.kt | 7 ++++++- .../app/ui/book/read/page/provider/TextChapterLayout.kt | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt index e4911c542..fecadc9eb 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt @@ -35,6 +35,8 @@ data class TextChapter( private var layout: TextChapterLayout? = null + val layoutChannel get() = layout!!.channel + fun getPage(index: Int): TextPage? { return pages.getOrNull(index) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index 2e656a72b..107cfb831 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -32,6 +32,7 @@ import io.legado.app.utils.postEvent import io.legado.app.utils.spToPx import io.legado.app.utils.splitNotBlank import io.legado.app.utils.textHeight +import kotlinx.coroutines.CoroutineScope import splitties.init.appCtx import java.util.LinkedList import java.util.Locale @@ -306,6 +307,8 @@ object ChapterProvider { } fun getTextChapterAsync( + scope: CoroutineScope, + book: Book, bookChapter: BookChapter, displayTitle: String, bookContent: BookContent, @@ -320,7 +323,9 @@ object ChapterProvider { bookChapter.isVip, bookChapter.isPay, bookContent.effectiveReplaceRules - ) + ).apply { + createLayout(scope, book, bookContent) + } return textChapter } 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 99c6b5b72..283038bcb 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 @@ -26,6 +26,7 @@ import io.legado.app.utils.fastSum import io.legado.app.utils.splitNotBlank import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.ensureActive import kotlinx.coroutines.launch import java.util.LinkedList @@ -78,6 +79,8 @@ class TextChapterLayout( var exception: Throwable? = null + var channel = Channel(Int.MAX_VALUE) + init { job = Coroutine.async(scope) { launch { @@ -124,6 +127,7 @@ class TextChapterLayout( textPage.isCompleted = true textPage.textChapter = textChapter textPage.upLinesPosition() + channel.trySend(textPage) try { listener?.onLayoutPageCompleted(textPages.lastIndex, textPage) } catch (e: Exception) { @@ -133,6 +137,7 @@ class TextChapterLayout( } private fun onCompleted() { + channel.close() try { listener?.onLayoutCompleted() } catch (e: Exception) { @@ -144,6 +149,7 @@ class TextChapterLayout( } private fun onException(e: Throwable) { + channel.close(e) if (e is CancellationException) { listener = null return