This commit is contained in:
Horis 2024-02-22 18:35:44 +08:00
parent 00a42a05ef
commit 2d7e3cfe04
4 changed files with 31 additions and 16 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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)?) {

View File

@ -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) {