Horis 2 anos atrás
pai
commit
383b3d4a33

+ 15 - 2
app/src/main/java/io/legado/app/model/ReadBook.kt

@@ -294,14 +294,27 @@ object ReadBook : CoroutineScope by MainScope() {
         curPageChanged(true)
     }
 
+    fun openChapter(index: Int, durChapterPos: Int = 0, success: (() -> Unit)? = null) {
+        if (index < chapterSize) {
+            clearTextChapter()
+            callBack?.upContent()
+            durChapterIndex = index
+            ReadBook.durChapterPos = durChapterPos
+            saveRead()
+            loadContent(resetPageOffset = true) {
+                success?.invoke()
+            }
+        }
+    }
+
     /**
      * 当前页面变化
      */
-    private fun curPageChanged(pauseReadAloud: Boolean = false) {
+    private fun curPageChanged(pageChanged: Boolean = false) {
         callBack?.pageChanged()
         if (BaseReadAloudService.isRun) {
             val scrollPageAnim = pageAnim() == 3
-            if (scrollPageAnim && pauseReadAloud) {
+            if (scrollPageAnim && pageChanged) {
                 ReadAloud.pause(appCtx)
             } else {
                 readAloud(!BaseReadAloudService.pause)

+ 1 - 4
app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt

@@ -1381,11 +1381,8 @@ class ReadBookActivity : BaseReadBookActivity(),
     override fun onLayoutPageCompleted(index: Int, page: TextPage) {
         upSeekBarThrottle.invoke()
         if (upContent) {
-            val line = page.lines.first()
             val durChapterPos = ReadBook.durChapterPos
-            val startPos = line.chapterPosition
-            val endPos = startPos + line.charSize
-            if (durChapterPos in startPos..<endPos) {
+            if (page.containPos(durChapterPos)) {
                 runOnUiThread {
                     binding.readView.upContent(0, resetPageOffset = false)
                 }

+ 1 - 10
app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@@ -317,16 +317,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
     }
 
     fun openChapter(index: Int, durChapterPos: Int = 0, success: (() -> Unit)? = null) {
-        if (index < ReadBook.chapterSize) {
-            ReadBook.clearTextChapter()
-            ReadBook.callBack?.upContent()
-            ReadBook.durChapterIndex = index
-            ReadBook.durChapterPos = durChapterPos
-            ReadBook.saveRead()
-            ReadBook.loadContent(resetPageOffset = true) {
-                success?.invoke()
-            }
-        }
+        ReadBook.openChapter(index, durChapterPos, success)
     }
 
     fun removeFromBookshelf(success: (() -> Unit)?) {

+ 14 - 0
app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt

@@ -47,6 +47,7 @@ data class TextPage(
     var doublePage = false
     var paddingTop = 0
     var isCompleted = false
+
     @JvmField
     var textChapter = emptyTextChapter
     val pageSize get() = textChapter.pageSize
@@ -256,6 +257,19 @@ data class TextPage(
         return textChapter
     }
 
+    /**
+     * 判断章节字符位置是否在这一页中
+     *
+     * @param chapterPos 章节字符位置
+     * @return
+     */
+    fun containPos(chapterPos: Int): Boolean {
+        val line = lines.first()
+        val startPos = line.chapterPosition
+        val endPos = startPos + charSize
+        return chapterPos in startPos..<endPos
+    }
+
     fun draw(view: ContentTextView, canvas: Canvas, relativeOffset: Float) {
         render(view)
         canvas.withTranslation(0f, relativeOffset + paddingTop) {