This commit is contained in:
Horis 2024-03-03 23:15:48 +08:00
parent 7d978ebd6e
commit 2b761150c8

View File

@ -20,7 +20,6 @@ import io.legado.app.model.localBook.TextFile
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.ui.book.read.page.entities.TextChapter
import io.legado.app.ui.book.read.page.entities.TextPage
import io.legado.app.ui.book.read.page.provider.ChapterProvider import io.legado.app.ui.book.read.page.provider.ChapterProvider
import io.legado.app.ui.book.read.page.provider.LayoutProgressListener import io.legado.app.ui.book.read.page.provider.LayoutProgressListener
import io.legado.app.utils.stackTraceStr import io.legado.app.utils.stackTraceStr
@ -31,6 +30,8 @@ import kotlinx.coroutines.MainScope
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancelChildren import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import splitties.init.appCtx import splitties.init.appCtx
import kotlin.math.max import kotlin.math.max
@ -533,89 +534,58 @@ 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 = val textChapter = ChapterProvider.getTextChapterAsync(
ChapterProvider.getTextChapterAsync(chapter, displayTitle, contents, chapterSize) this@ReadBook, book, chapter, displayTitle, contents, chapterSize
)
when (val offset = chapter.index - durChapterIndex) { when (val offset = chapter.index - durChapterIndex) {
0 -> { 0 -> {
curTextChapter?.cancelLayout() curTextChapter?.cancelLayout()
curTextChapter = textChapter curTextChapter = textChapter
callBack?.upMenuView() callBack?.upMenuView()
textChapter.setProgressListener(object : LayoutProgressListener { var available = false
var available = false for (page in textChapter.layoutChannel) {
val index = page.index
override fun onLayoutPageCompleted(index: Int, page: TextPage) { if (!available && page.containPos(durChapterPos)) {
if (!available && page.containPos(durChapterPos)) { if (upContent) {
if (upContent) { callBack?.upContent(offset, resetPageOffset)
callBack?.upContent(offset, resetPageOffset)
}
curPageChanged()
callBack?.contentLoadFinish()
available = true
} }
if (upContent && isScroll) { curPageChanged()
if (max(index - 3, 0) < durPageIndex) { callBack?.contentLoadFinish()
callBack?.upContent(offset, resetPageOffset) available = true
} }
if (upContent && isScroll) {
if (max(index - 3, 0) < durPageIndex) {
callBack?.upContent(offset, false)
} }
callBack?.onLayoutPageCompleted(index, page)
} }
callBack?.onLayoutPageCompleted(index, page)
override fun onLayoutCompleted() { }
if (upContent) callBack?.upContent(offset, resetPageOffset) if (upContent) callBack?.upContent(offset, false)
success?.invoke()
callBack?.onLayoutCompleted()
}
override fun onLayoutException(e: Throwable) {
AppLog.put("ChapterProvider ERROR", e)
appCtx.toastOnUi("ChapterProvider ERROR:\n${e.stackTraceStr}")
callBack?.onLayoutException(e)
}
})
} }
-1 -> { -1 -> {
prevTextChapter?.cancelLayout() prevTextChapter?.cancelLayout()
prevTextChapter = textChapter prevTextChapter = textChapter
textChapter.setProgressListener(object : LayoutProgressListener { textChapter.layoutChannel.receiveAsFlow().collect()
override fun onLayoutCompleted() { if (upContent) callBack?.upContent(offset, resetPageOffset)
if (upContent) callBack?.upContent(offset, resetPageOffset)
success?.invoke()
}
override fun onLayoutException(e: Throwable) {
AppLog.put("ChapterProvider ERROR", e)
appCtx.toastOnUi("ChapterProvider ERROR:\n${e.stackTraceStr}")
}
})
} }
1 -> { 1 -> {
nextTextChapter?.cancelLayout() nextTextChapter?.cancelLayout()
nextTextChapter = textChapter nextTextChapter = textChapter
textChapter.setProgressListener(object : LayoutProgressListener { for (page in textChapter.layoutChannel) {
override fun onLayoutPageCompleted(index: Int, page: TextPage) { if (page.index > 1) {
if (index > 1) { continue
return
}
if (upContent) callBack?.upContent(offset, resetPageOffset)
} }
if (upContent) callBack?.upContent(offset, resetPageOffset)
override fun onLayoutCompleted() { }
success?.invoke()
}
override fun onLayoutException(e: Throwable) {
AppLog.put("ChapterProvider ERROR", e)
appCtx.toastOnUi("ChapterProvider ERROR:\n${e.stackTraceStr}")
}
})
} }
} }
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}")
}.onSuccess {
success?.invoke()
} }
} }