This commit is contained in:
Horis
2024-02-22 15:36:13 +08:00
parent aabeef4e27
commit 3fb015c99f
5 changed files with 38 additions and 54 deletions
@@ -488,22 +488,14 @@ object ReadBook : CoroutineScope by MainScope() {
) )
val contents = contentProcessor val contents = contentProcessor
.getContent(book, chapter, content, includeTitle = false) .getContent(book, chapter, content, includeTitle = false)
val textChapter = ChapterProvider.getTextChapterAsync( val textChapter =
book, ChapterProvider.getTextChapterAsync(chapter, displayTitle, contents, chapterSize)
chapter,
displayTitle,
contents,
chapterSize,
this@ReadBook
)
when (val offset = chapter.index - durChapterIndex) { when (val offset = chapter.index - durChapterIndex) {
0 -> { 0 -> {
curTextChapter?.cancelLayout() curTextChapter?.cancelLayout()
curTextChapter?.setProgressListener(null) curTextChapter?.setProgressListener(null)
curTextChapter = textChapter curTextChapter = textChapter
if (textChapter.isCompleted) { if (resetPageOffset) {
if (upContent) callBack?.upContent(offset, resetPageOffset)
} else if (resetPageOffset) {
callBack?.resetPageOffset() callBack?.resetPageOffset()
} }
callBack?.upMenuView() callBack?.upMenuView()
@@ -516,15 +508,11 @@ object ReadBook : CoroutineScope by MainScope() {
prevTextChapter?.cancelLayout() prevTextChapter?.cancelLayout()
prevTextChapter = textChapter prevTextChapter = textChapter
if (upContent) { if (upContent) {
if (textChapter.isCompleted) { textChapter.setProgressListener(object : LayoutProgressListener {
callBack?.upContent(offset, resetPageOffset) override fun onLayoutCompleted() {
} else { 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?.cancelLayout()
nextTextChapter = textChapter nextTextChapter = textChapter
if (upContent) { if (upContent) {
if (textChapter.isCompleted) { textChapter.setProgressListener(object : LayoutProgressListener {
callBack?.upContent(offset, resetPageOffset) override fun onLayoutPageCompleted(index: Int, page: TextPage) {
} else { if (index > 1) {
textChapter.setProgressListener(object : LayoutProgressListener { return
override fun onLayoutPageCompleted(index: Int, page: TextPage) {
if (index > 1) {
return
}
callBack?.upContent(offset, resetPageOffset)
} }
}) callBack?.upContent(offset, resetPageOffset)
} }
})
} }
} }
} }
Unit textChapter.createLayout(this@ReadBook, book, contents)
}.onError { }.onError {
AppLog.put("ChapterProvider ERROR", it) AppLog.put("ChapterProvider ERROR", it)
appCtx.toastOnUi("ChapterProvider ERROR:\n${it.stackTraceStr}") appCtx.toastOnUi("ChapterProvider ERROR:\n${it.stackTraceStr}")
@@ -288,6 +288,10 @@ class PageView(context: Context) : FrameLayout(context) {
binding.contentTextView.setContent(textPage) binding.contentTextView.setContent(textPage)
} }
fun invalidateContentView() {
binding.contentTextView.invalidate()
}
/** /**
* 设置无障碍文本 * 设置无障碍文本
*/ */
@@ -527,7 +527,11 @@ class ReadView(context: Context, attrs: AttributeSet) :
curPage.setContentDescription(pageFactory.curPage.text) curPage.setContentDescription(pageFactory.curPage.text)
} }
if (isScroll && !isAutoPage) { if (isScroll && !isAutoPage) {
curPage.setContent(pageFactory.curPage, resetPageOffset) if (relativePosition == 0) {
curPage.setContent(pageFactory.curPage, resetPageOffset)
} else {
curPage.invalidateContentView()
}
} else { } else {
when (relativePosition) { when (relativePosition) {
-1 -> prevPage.setContent(pageFactory.prevPage) -1 -> prevPage.setContent(pageFactory.prevPage)
@@ -32,7 +32,6 @@ import io.legado.app.utils.postEvent
import io.legado.app.utils.spToPx import io.legado.app.utils.spToPx
import io.legado.app.utils.splitNotBlank import io.legado.app.utils.splitNotBlank
import io.legado.app.utils.textHeight import io.legado.app.utils.textHeight
import kotlinx.coroutines.CoroutineScope
import splitties.init.appCtx import splitties.init.appCtx
import java.util.LinkedList import java.util.LinkedList
import java.util.Locale import java.util.Locale
@@ -307,12 +306,10 @@ object ChapterProvider {
} }
fun getTextChapterAsync( fun getTextChapterAsync(
book: Book,
bookChapter: BookChapter, bookChapter: BookChapter,
displayTitle: String, displayTitle: String,
bookContent: BookContent, bookContent: BookContent,
chapterSize: Int, chapterSize: Int,
scope: CoroutineScope
): TextChapter { ): TextChapter {
val textChapter = TextChapter( val textChapter = TextChapter(
@@ -323,9 +320,7 @@ object ChapterProvider {
bookChapter.isVip, bookChapter.isVip,
bookChapter.isPay, bookChapter.isPay,
bookContent.effectiveReplaceRules bookContent.effectiveReplaceRules
).apply { )
createLayout(scope, book, bookContent)
}
return textChapter return textChapter
} }
@@ -13,7 +13,6 @@ import io.legado.app.help.book.BookHelp
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadBookConfig
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.globalExecutor
import io.legado.app.model.ImageProvider import io.legado.app.model.ImageProvider
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.ui.book.read.page.entities.TextChapter
@@ -69,7 +68,7 @@ class TextChapterLayout(
private val stringBuilder = StringBuilder() private val stringBuilder = StringBuilder()
private var isCompleted = false private var isCompleted = false
private var job: Coroutine<*>? = null private val job: Coroutine<*>
private val bookChapter inline get() = textChapter.chapter private val bookChapter inline get() = textChapter.chapter
private val displayTitle inline get() = textChapter.title private val displayTitle inline get() = textChapter.title
private val chaptersSize inline get() = textChapter.chaptersSize private val chaptersSize inline get() = textChapter.chaptersSize
@@ -77,19 +76,17 @@ class TextChapterLayout(
var exception: Throwable? = null var exception: Throwable? = null
init { init {
globalExecutor.submit { job = Coroutine.async(scope) {
job = Coroutine.async(scope) { launch {
launch { val bookSource = book.getBookSource() ?: return@launch
val bookSource = book.getBookSource() ?: return@launch BookHelp.saveImages(bookSource, book, bookChapter, bookContent.toString())
BookHelp.saveImages(bookSource, book, bookChapter, bookContent.toString())
}
getTextChapter(book, bookChapter, displayTitle, bookContent)
}.onError {
exception = it
onException(it)
}.onFinally {
isCompleted = true
} }
getTextChapter(book, bookChapter, displayTitle, bookContent)
}.onError {
exception = it
onException(it)
}.onFinally {
isCompleted = true
} }
} }
@@ -109,7 +106,7 @@ class TextChapterLayout(
} }
fun cancel() { fun cancel() {
job?.cancel() job.cancel()
} }
private fun onPageCompleted() { private fun onPageCompleted() {