Horis 2 лет назад
Родитель
Сommit
96b2014ee4

+ 2 - 0
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)
     }

+ 6 - 1
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
     }

+ 6 - 0
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<TextPage>(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