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) 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() callBack?.pageChanged()
if (BaseReadAloudService.isRun) { if (BaseReadAloudService.isRun) {
val scrollPageAnim = pageAnim() == 3 val scrollPageAnim = pageAnim() == 3
if (scrollPageAnim && pauseReadAloud) { if (scrollPageAnim && pageChanged) {
ReadAloud.pause(appCtx) ReadAloud.pause(appCtx)
} else { } else {
readAloud(!BaseReadAloudService.pause) readAloud(!BaseReadAloudService.pause)

View File

@ -1381,11 +1381,8 @@ class ReadBookActivity : BaseReadBookActivity(),
override fun onLayoutPageCompleted(index: Int, page: TextPage) { override fun onLayoutPageCompleted(index: Int, page: TextPage) {
upSeekBarThrottle.invoke() upSeekBarThrottle.invoke()
if (upContent) { if (upContent) {
val line = page.lines.first()
val durChapterPos = ReadBook.durChapterPos val durChapterPos = ReadBook.durChapterPos
val startPos = line.chapterPosition if (page.containPos(durChapterPos)) {
val endPos = startPos + line.charSize
if (durChapterPos in startPos..<endPos) {
runOnUiThread { runOnUiThread {
binding.readView.upContent(0, resetPageOffset = false) 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) { fun openChapter(index: Int, durChapterPos: Int = 0, success: (() -> Unit)? = null) {
if (index < ReadBook.chapterSize) { ReadBook.openChapter(index, durChapterPos, success)
ReadBook.clearTextChapter()
ReadBook.callBack?.upContent()
ReadBook.durChapterIndex = index
ReadBook.durChapterPos = durChapterPos
ReadBook.saveRead()
ReadBook.loadContent(resetPageOffset = true) {
success?.invoke()
}
}
} }
fun removeFromBookshelf(success: (() -> Unit)?) { fun removeFromBookshelf(success: (() -> Unit)?) {

View File

@ -47,6 +47,7 @@ data class TextPage(
var doublePage = false var doublePage = false
var paddingTop = 0 var paddingTop = 0
var isCompleted = false var isCompleted = false
@JvmField @JvmField
var textChapter = emptyTextChapter var textChapter = emptyTextChapter
val pageSize get() = textChapter.pageSize val pageSize get() = textChapter.pageSize
@ -256,6 +257,19 @@ data class TextPage(
return textChapter 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) { fun draw(view: ContentTextView, canvas: Canvas, relativeOffset: Float) {
render(view) render(view)
canvas.withTranslation(0f, relativeOffset + paddingTop) { canvas.withTranslation(0f, relativeOffset + paddingTop) {