diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index 74799c8e5..f725beb75 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -336,7 +336,7 @@ class AudioPlayService : BaseService(), private fun saveProgress() { launch(IO) { AudioPlay.book?.let { - App.db.bookDao().upProgress(it.bookUrl, AudioPlay.durPageIndex) + App.db.bookDao().upProgress(it.bookUrl, AudioPlay.durChapterPos) } } } diff --git a/app/src/main/java/io/legado/app/service/help/AudioPlay.kt b/app/src/main/java/io/legado/app/service/help/AudioPlay.kt index e591343a8..87549c8eb 100644 --- a/app/src/main/java/io/legado/app/service/help/AudioPlay.kt +++ b/app/src/main/java/io/legado/app/service/help/AudioPlay.kt @@ -21,7 +21,7 @@ object AudioPlay { var inBookshelf = false var chapterSize = 0 var durChapterIndex = 0 - var durPageIndex = 0 + var durChapterPos = 0 var webBook: WebBook? = null val loadingChapters = arrayListOf() @@ -81,7 +81,7 @@ object AudioPlay { Coroutine.async { book?.let { book -> durChapterIndex = index - durPageIndex = 0 + durChapterPos = 0 book.durChapterIndex = durChapterIndex book.durChapterPos = 0 saveRead() @@ -100,7 +100,7 @@ object AudioPlay { return@let } durChapterIndex-- - durPageIndex = 0 + durChapterPos = 0 book.durChapterIndex = durChapterIndex book.durChapterPos = 0 saveRead() @@ -119,7 +119,7 @@ object AudioPlay { return@let } durChapterIndex++ - durPageIndex = 0 + durChapterPos = 0 book.durChapterIndex = durChapterIndex book.durChapterPos = 0 saveRead() @@ -137,7 +137,7 @@ object AudioPlay { book.lastCheckCount = 0 book.durChapterTime = System.currentTimeMillis() book.durChapterIndex = durChapterIndex - book.durChapterPos = durPageIndex + book.durChapterPos = durChapterPos App.db.bookChapterDao().getChapter(book.bookUrl, book.durChapterIndex)?.let { book.durChapterTitle = it.title } diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index 4c022c653..500118037 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -31,7 +31,7 @@ object ReadBook { var inBookshelf = false var chapterSize = 0 var durChapterIndex = 0 - var durPageIndex = 0 + var durChapterPos = 0 var isLocalBook = true var callBack: CallBack? = null var prevTextChapter: TextChapter? = null @@ -50,7 +50,7 @@ object ReadBook { readRecord.bookName = book.name readRecord.readTime = App.db.readRecordDao().getReadTime(book.name) ?: 0 durChapterIndex = book.durChapterIndex - durPageIndex = book.durChapterPos + durChapterPos = book.durChapterPos isLocalBook = book.origin == BookType.local chapterSize = 0 prevTextChapter = null @@ -96,14 +96,14 @@ object ReadBook { } fun moveToNextPage() { - durPageIndex++ + durChapterPos = curTextChapter?.getNextPageLength(durChapterPos) ?: durChapterPos callBack?.upContent() saveRead() } fun moveToNextChapter(upContent: Boolean): Boolean { if (durChapterIndex < chapterSize - 1) { - durPageIndex = 0 + durChapterPos = 0 durChapterIndex++ prevTextChapter = curTextChapter curTextChapter = nextTextChapter @@ -133,7 +133,7 @@ object ReadBook { fun moveToPrevChapter(upContent: Boolean, toLast: Boolean = true): Boolean { if (durChapterIndex > 0) { - durPageIndex = if (toLast) prevTextChapter?.lastIndex ?: 0 else 0 + durChapterPos = if (toLast) prevTextChapter?.lastReadLength ?: 0 else 0 durChapterIndex-- nextTextChapter = curTextChapter curTextChapter = prevTextChapter @@ -161,15 +161,15 @@ object ReadBook { } } - fun skipToPage(page: Int) { - durPageIndex = page + fun skipToPage(index: Int) { + durChapterPos = curTextChapter?.getReadLength(index) ?: index callBack?.upContent() curPageChanged() saveRead() } - fun setPageIndex(pageIndex: Int) { - durPageIndex = pageIndex + fun setPageIndex(index: Int) { + durChapterPos = curTextChapter?.getReadLength(index) ?: index saveRead() curPageChanged() } @@ -191,19 +191,16 @@ object ReadBook { if (book != null && textChapter != null) { val key = IntentDataHelp.putData(textChapter) ReadAloud.play( - App.INSTANCE, book.name, textChapter.title, durPageIndex, key, play + App.INSTANCE, book.name, textChapter.title, durPageIndex(), key, play ) } } - fun durChapterPos(): Int { + fun durPageIndex(): Int { curTextChapter?.let { - if (durPageIndex < it.pageSize) { - return durPageIndex - } - return it.pageSize - 1 + return it.getPageIndexByCharIndex(durChapterPos) } - return durPageIndex + return durChapterPos } /** @@ -430,7 +427,7 @@ object ReadBook { book.lastCheckCount = 0 book.durChapterTime = System.currentTimeMillis() book.durChapterIndex = durChapterIndex - book.durChapterPos = durPageIndex + book.durChapterPos = durChapterPos App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { book.durChapterTitle = it.title } diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt index ff731e66f..93727fb8d 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt @@ -208,7 +208,7 @@ class AudioPlayActivity : binding.tvAllTime.text = DateFormatUtils.format(it.toLong(), "mm:ss") } observeEventSticky(EventBus.AUDIO_PROGRESS) { - AudioPlay.durPageIndex = it + AudioPlay.durChapterPos = it if (!adjustProgress) binding.playerProgress.progress = it binding.tvDurTime.text = DateFormatUtils.format(it.toLong(), "mm:ss") } diff --git a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt index 9d73eed6b..a1879a1d1 100644 --- a/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt @@ -31,7 +31,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application) titleData.postValue(book.name) coverData.postValue(book.getDisplayCover()) durChapterIndex = book.durChapterIndex - durPageIndex = book.durChapterPos + durChapterPos = book.durChapterPos App.db.bookChapterDao().getChapter(book.bookUrl, book.durChapterIndex)?.let { postEvent(EventBus.AUDIO_SUB_TITLE, it.title) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 56c16674d..e57eab1eb 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -500,7 +500,7 @@ class ReadBookActivity : ReadBookBaseActivity(), autoPageProgress = 0 launch { binding.pageView.upContent(relativePosition, resetPageOffset) - binding.readMenu.setSeekPage(ReadBook.durPageIndex) + binding.readMenu.setSeekPage(ReadBook.durPageIndex()) } loadStates = false } @@ -526,7 +526,7 @@ class ReadBookActivity : ReadBookBaseActivity(), override fun pageChanged() { autoPageProgress = 0 launch { - binding.readMenu.setSeekPage(ReadBook.durPageIndex) + binding.readMenu.setSeekPage(ReadBook.durPageIndex()) } } @@ -741,13 +741,12 @@ class ReadBookActivity : ReadBookBaseActivity(), delay(100L) pages = ReadBook.curTextChapter?.pages } - val positions = - ReadBook.searchResultPositions( - pages, - indexWithinChapter, - viewModel.searchContentQuery - ) - while (ReadBook.durPageIndex != positions[0]) { + val positions = ReadBook.searchResultPositions( + pages, + indexWithinChapter, + viewModel.searchContentQuery + ) + while (ReadBook.durPageIndex() != positions[0]) { delay(100L) ReadBook.skipToPage(positions[0]) } @@ -817,7 +816,7 @@ class ReadBookActivity : ReadBookBaseActivity(), observeEvent(EventBus.TIME_CHANGED) { binding.pageView.upTime() } observeEvent(EventBus.BATTERY_CHANGED) { binding.pageView.upBattery(it) } observeEvent(EventBus.OPEN_CHAPTER) { - viewModel.openChapter(it.index, ReadBook.durPageIndex) + viewModel.openChapter(it.index, ReadBook.durChapterPos) binding.pageView.upContent() } observeEvent(EventBus.MEDIA_BUTTON) { @@ -841,7 +840,7 @@ class ReadBookActivity : ReadBookBaseActivity(), observeEvent(EventBus.ALOUD_STATE) { if (it == Status.STOP || it == Status.PAUSE) { ReadBook.curTextChapter?.let { textChapter -> - val page = textChapter.page(ReadBook.durPageIndex) + val page = textChapter.getPageByReadPos(ReadBook.durChapterPos) if (page != null) { page.removePageAloudSpan() binding.pageView.upContent(resetPageOffset = false) @@ -853,9 +852,9 @@ class ReadBookActivity : ReadBookBaseActivity(), launch(IO) { if (BaseReadAloudService.isPlay()) { ReadBook.curTextChapter?.let { textChapter -> - val pageStart = - chapterStart - textChapter.getReadLength(ReadBook.durPageIndex) - textChapter.page(ReadBook.durPageIndex)?.upPageAloudSpan(pageStart) + val pageStart = chapterStart - ReadBook.durChapterPos + textChapter.getPageByReadPos(ReadBook.durChapterPos) + ?.upPageAloudSpan(pageStart) upContent() } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt index 9e9f7deab..1b019cbaf 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt @@ -214,7 +214,7 @@ abstract class ReadBookBaseActivity : bookUrl = book.bookUrl, bookName = book.name, chapterIndex = ReadBook.durChapterIndex, - pageIndex = ReadBook.durPageIndex, + pageIndex = ReadBook.durChapterPos, chapterName = textChapter.title, content = editContent ) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 66245bc6e..a590564ad 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -69,7 +69,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { ReadBook.book = book if (ReadBook.durChapterIndex != book.durChapterIndex) { ReadBook.durChapterIndex = book.durChapterIndex - ReadBook.durPageIndex = book.durChapterPos + ReadBook.durChapterPos = book.durChapterPos ReadBook.prevTextChapter = null ReadBook.curTextChapter = null ReadBook.nextTextChapter = null @@ -230,14 +230,14 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } - fun openChapter(index: Int, pageIndex: Int = 0) { + fun openChapter(index: Int, durChapterPos: Int = 0) { ReadBook.prevTextChapter = null ReadBook.curTextChapter = null ReadBook.nextTextChapter = null ReadBook.callBack?.upContent() if (index != ReadBook.durChapterIndex) { ReadBook.durChapterIndex = index - ReadBook.durPageIndex = pageIndex + ReadBook.durChapterPos = durChapterPos } ReadBook.saveRead() ReadBook.loadContent(resetPageOffset = true) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt index e598bce9d..7a06fe21a 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt @@ -287,7 +287,7 @@ class ReadMenu @JvmOverloads constructor( binding.tvChapterUrl.gone() } binding.seekReadPage.max = it.pageSize.minus(1) - binding.seekReadPage.progress = ReadBook.durPageIndex + binding.seekReadPage.progress = ReadBook.durPageIndex() binding.tvPre.isEnabled = ReadBook.durChapterIndex != 0 binding.tvNext.isEnabled = ReadBook.durChapterIndex != ReadBook.chapterSize - 1 } ?: let { diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/DataSource.kt b/app/src/main/java/io/legado/app/ui/book/read/page/DataSource.kt index 3a128f969..6883a28a3 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/DataSource.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/DataSource.kt @@ -5,7 +5,7 @@ import io.legado.app.ui.book.read.page.entities.TextChapter interface DataSource { - val pageIndex: Int get() = ReadBook.durChapterPos() + val pageIndex: Int get() = ReadBook.durPageIndex() val currentChapter: TextChapter? diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt index 1cd9177c9..bb79a2e67 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt @@ -8,20 +8,25 @@ data class TextChapter( val title: String, val url: String, val pages: List, - val pageLines: List, - val pageLengths: List, val chaptersSize: Int, var titlePaint: TextPaint? = null, var contentPaint: TextPaint? = null ) { + fun page(index: Int): TextPage? { return pages.getOrNull(index) } + fun getPageByReadPos(readPos: Int): TextPage? { + return page(getPageIndexByCharIndex(readPos)) + } + val lastPage: TextPage? get() = pages.lastOrNull() val lastIndex: Int get() = pages.lastIndex + val lastReadLength: Int get() = getReadLength(lastIndex) + val pageSize: Int get() = pages.size fun isLastIndex(index: Int): Boolean { @@ -32,11 +37,15 @@ data class TextChapter( var length = 0 val maxIndex = min(pageIndex, pages.size) for (index in 0 until maxIndex) { - length += pageLengths[index] + length += pages[index].charSize } return length } + fun getNextPageLength(length: Int): Int { + return getReadLength(getPageIndexByCharIndex(length) + 1) + } + fun getUnRead(pageIndex: Int): String { val stringBuilder = StringBuilder() if (pages.isNotEmpty()) { @@ -54,4 +63,15 @@ data class TextChapter( } return stringBuilder.toString() } + + fun getPageIndexByCharIndex(charIndex: Int): Int { + var length = 0 + pages.forEach { + length += it.charSize + if (length > charIndex) { + return it.index + } + } + return pages.lastIndex + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt index beb9e3673..df3de1447 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt @@ -8,6 +8,7 @@ import io.legado.app.help.ReadBookConfig import io.legado.app.ui.book.read.page.provider.ChapterProvider import java.text.DecimalFormat +@Suppress("unused") data class TextPage( var index: Int = 0, var text: String = App.INSTANCE.getString(R.string.data_loading), @@ -16,9 +17,12 @@ data class TextPage( var pageSize: Int = 0, var chapterSize: Int = 0, var chapterIndex: Int = 0, - var height: Float = 0f + var height: Float = 0f, ) { + val lineSize = textLines.size + val charSize = text.length + fun upLinesPosition() = ChapterProvider.apply { if (!ReadBookConfig.textBottomJustify) return@apply if (textLines.size <= 1) return@apply diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index c656162ef..0dd420e38 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -83,8 +83,6 @@ object ChapterProvider { imageStyle: String?, ): TextChapter { val textPages = arrayListOf() - val pageLines = arrayListOf() - val pageLengths = arrayListOf() val stringBuilder = StringBuilder() var durY = 0f var paint = Pair(titlePaint, contentPaint) @@ -109,21 +107,12 @@ object ChapterProvider { val isTitle = index == 0 val textPaint = if (isTitle) paint.first else paint.second if (!(isTitle && ReadBookConfig.titleMode == 2)) { - durY = setTypeText( - text, durY, textPages, pageLines, - pageLengths, stringBuilder, isTitle, textPaint - ) + durY = setTypeText(text, durY, textPages, stringBuilder, isTitle, textPaint) } } } textPages.last().height = durY + 20.dp textPages.last().text = stringBuilder.toString() - if (pageLines.size < textPages.size) { - pageLines.add(textPages.last().textLines.size) - } - if (pageLengths.size < textPages.size) { - pageLengths.add(textPages.last().text.length) - } textPages.forEachIndexed { index, item -> item.index = index item.pageSize = textPages.size @@ -136,7 +125,7 @@ object ChapterProvider { return TextChapter( bookChapter.index, bookChapter.title, bookChapter.getAbsoluteURL().substringBefore(","), - textPages, pageLines, pageLengths, chapterSize, paint.first, paint.second + textPages, chapterSize, paint.first, paint.second ) } @@ -211,8 +200,6 @@ object ChapterProvider { text: String, y: Float, textPages: ArrayList, - pageLines: ArrayList, - pageLengths: ArrayList, stringBuilder: StringBuilder, isTitle: Boolean, textPaint: TextPaint @@ -247,8 +234,6 @@ object ChapterProvider { if (durY + textPaint.textHeight > visibleHeight) { //当前页面结束,设置各种值 textPages.last().text = stringBuilder.toString() - pageLines.add(textPages.last().textLines.size) - pageLengths.add(textPages.last().text.length) textPages.last().height = durY //新建页面 textPages.add(TextPage())