Horis 2 лет назад
Родитель
Сommit
3fb015c99f

+ 16 - 32
app/src/main/java/io/legado/app/model/ReadBook.kt

@@ -488,22 +488,14 @@ object ReadBook : CoroutineScope by MainScope() {
             )
             val contents = contentProcessor
                 .getContent(book, chapter, content, includeTitle = false)
-            val textChapter = ChapterProvider.getTextChapterAsync(
-                book,
-                chapter,
-                displayTitle,
-                contents,
-                chapterSize,
-                this@ReadBook
-            )
+            val textChapter =
+                ChapterProvider.getTextChapterAsync(chapter, displayTitle, contents, chapterSize)
             when (val offset = chapter.index - durChapterIndex) {
                 0 -> {
                     curTextChapter?.cancelLayout()
                     curTextChapter?.setProgressListener(null)
                     curTextChapter = textChapter
-                    if (textChapter.isCompleted) {
-                        if (upContent) callBack?.upContent(offset, resetPageOffset)
-                    } else if (resetPageOffset) {
+                    if (resetPageOffset) {
                         callBack?.resetPageOffset()
                     }
                     callBack?.upMenuView()
@@ -516,15 +508,11 @@ object ReadBook : CoroutineScope by MainScope() {
                     prevTextChapter?.cancelLayout()
                     prevTextChapter = textChapter
                     if (upContent) {
-                        if (textChapter.isCompleted) {
-                            callBack?.upContent(offset, resetPageOffset)
-                        } else {
-                            textChapter.setProgressListener(object : LayoutProgressListener {
-                                override fun onLayoutCompleted() {
-                                    callBack?.upContent(offset, resetPageOffset)
-                                }
-                            })
-                        }
+                        textChapter.setProgressListener(object : LayoutProgressListener {
+                            override fun onLayoutCompleted() {
+                                callBack?.upContent(offset, resetPageOffset)
+                            }
+                        })
                     }
                 }
 
@@ -532,22 +520,18 @@ object ReadBook : CoroutineScope by MainScope() {
                     nextTextChapter?.cancelLayout()
                     nextTextChapter = textChapter
                     if (upContent) {
-                        if (textChapter.isCompleted) {
-                            callBack?.upContent(offset, resetPageOffset)
-                        } else {
-                            textChapter.setProgressListener(object : LayoutProgressListener {
-                                override fun onLayoutPageCompleted(index: Int, page: TextPage) {
-                                    if (index > 1) {
-                                        return
-                                    }
-                                    callBack?.upContent(offset, resetPageOffset)
+                        textChapter.setProgressListener(object : LayoutProgressListener {
+                            override fun onLayoutPageCompleted(index: Int, page: TextPage) {
+                                if (index > 1) {
+                                    return
                                 }
-                            })
-                        }
+                                callBack?.upContent(offset, resetPageOffset)
+                            }
+                        })
                     }
                 }
             }
-            Unit
+            textChapter.createLayout(this@ReadBook, book, contents)
         }.onError {
             AppLog.put("ChapterProvider ERROR", it)
             appCtx.toastOnUi("ChapterProvider ERROR:\n${it.stackTraceStr}")

+ 4 - 0
app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt

@@ -288,6 +288,10 @@ class PageView(context: Context) : FrameLayout(context) {
         binding.contentTextView.setContent(textPage)
     }
 
+    fun invalidateContentView() {
+        binding.contentTextView.invalidate()
+    }
+
     /**
      * 设置无障碍文本
      */

+ 5 - 1
app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt

@@ -527,7 +527,11 @@ class ReadView(context: Context, attrs: AttributeSet) :
             curPage.setContentDescription(pageFactory.curPage.text)
         }
         if (isScroll && !isAutoPage) {
-            curPage.setContent(pageFactory.curPage, resetPageOffset)
+            if (relativePosition == 0) {
+                curPage.setContent(pageFactory.curPage, resetPageOffset)
+            } else {
+                curPage.invalidateContentView()
+            }
         } else {
             when (relativePosition) {
                 -1 -> prevPage.setContent(pageFactory.prevPage)

+ 1 - 6
app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt

@@ -32,7 +32,6 @@ 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
@@ -307,12 +306,10 @@ object ChapterProvider {
     }
 
     fun getTextChapterAsync(
-        book: Book,
         bookChapter: BookChapter,
         displayTitle: String,
         bookContent: BookContent,
         chapterSize: Int,
-        scope: CoroutineScope
     ): TextChapter {
 
         val textChapter = TextChapter(
@@ -323,9 +320,7 @@ object ChapterProvider {
             bookChapter.isVip,
             bookChapter.isPay,
             bookContent.effectiveReplaceRules
-        ).apply {
-            createLayout(scope, book, bookContent)
-        }
+        )
 
         return textChapter
     }

+ 12 - 15
app/src/main/java/io/legado/app/ui/book/read/page/provider/TextChapterLayout.kt

@@ -13,7 +13,6 @@ import io.legado.app.help.book.BookHelp
 import io.legado.app.help.config.AppConfig
 import io.legado.app.help.config.ReadBookConfig
 import io.legado.app.help.coroutine.Coroutine
-import io.legado.app.help.globalExecutor
 import io.legado.app.model.ImageProvider
 import io.legado.app.model.ReadBook
 import io.legado.app.ui.book.read.page.entities.TextChapter
@@ -69,7 +68,7 @@ class TextChapterLayout(
     private val stringBuilder = StringBuilder()
 
     private var isCompleted = false
-    private var job: Coroutine<*>? = null
+    private val job: Coroutine<*>
     private val bookChapter inline get() = textChapter.chapter
     private val displayTitle inline get() = textChapter.title
     private val chaptersSize inline get() = textChapter.chaptersSize
@@ -77,19 +76,17 @@ class TextChapterLayout(
     var exception: Throwable? = null
 
     init {
-        globalExecutor.submit {
-            job = Coroutine.async(scope) {
-                launch {
-                    val bookSource = book.getBookSource() ?: return@launch
-                    BookHelp.saveImages(bookSource, book, bookChapter, bookContent.toString())
-                }
-                getTextChapter(book, bookChapter, displayTitle, bookContent)
-            }.onError {
-                exception = it
-                onException(it)
-            }.onFinally {
-                isCompleted = true
+        job = Coroutine.async(scope) {
+            launch {
+                val bookSource = book.getBookSource() ?: return@launch
+                BookHelp.saveImages(bookSource, book, bookChapter, bookContent.toString())
             }
+            getTextChapter(book, bookChapter, displayTitle, bookContent)
+        }.onError {
+            exception = it
+            onException(it)
+        }.onFinally {
+            isCompleted = true
         }
     }
 
@@ -109,7 +106,7 @@ class TextChapterLayout(
     }
 
     fun cancel() {
-        job?.cancel()
+        job.cancel()
     }
 
     private fun onPageCompleted() {