阅读进度记录从页数改成字数

pull/517/head
gedoor 4 years ago
parent 755bc62632
commit d20ec0a97c
  1. 2
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  2. 10
      app/src/main/java/io/legado/app/service/help/AudioPlay.kt
  3. 31
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  4. 2
      app/src/main/java/io/legado/app/ui/audio/AudioPlayActivity.kt
  5. 2
      app/src/main/java/io/legado/app/ui/audio/AudioPlayViewModel.kt
  6. 27
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  7. 2
      app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt
  8. 6
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  9. 2
      app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt
  10. 2
      app/src/main/java/io/legado/app/ui/book/read/page/DataSource.kt
  11. 26
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt
  12. 6
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt
  13. 19
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt

@ -336,7 +336,7 @@ class AudioPlayService : BaseService(),
private fun saveProgress() { private fun saveProgress() {
launch(IO) { launch(IO) {
AudioPlay.book?.let { AudioPlay.book?.let {
App.db.bookDao().upProgress(it.bookUrl, AudioPlay.durPageIndex) App.db.bookDao().upProgress(it.bookUrl, AudioPlay.durChapterPos)
} }
} }
} }

@ -21,7 +21,7 @@ object AudioPlay {
var inBookshelf = false var inBookshelf = false
var chapterSize = 0 var chapterSize = 0
var durChapterIndex = 0 var durChapterIndex = 0
var durPageIndex = 0 var durChapterPos = 0
var webBook: WebBook? = null var webBook: WebBook? = null
val loadingChapters = arrayListOf<Int>() val loadingChapters = arrayListOf<Int>()
@ -81,7 +81,7 @@ object AudioPlay {
Coroutine.async { Coroutine.async {
book?.let { book -> book?.let { book ->
durChapterIndex = index durChapterIndex = index
durPageIndex = 0 durChapterPos = 0
book.durChapterIndex = durChapterIndex book.durChapterIndex = durChapterIndex
book.durChapterPos = 0 book.durChapterPos = 0
saveRead() saveRead()
@ -100,7 +100,7 @@ object AudioPlay {
return@let return@let
} }
durChapterIndex-- durChapterIndex--
durPageIndex = 0 durChapterPos = 0
book.durChapterIndex = durChapterIndex book.durChapterIndex = durChapterIndex
book.durChapterPos = 0 book.durChapterPos = 0
saveRead() saveRead()
@ -119,7 +119,7 @@ object AudioPlay {
return@let return@let
} }
durChapterIndex++ durChapterIndex++
durPageIndex = 0 durChapterPos = 0
book.durChapterIndex = durChapterIndex book.durChapterIndex = durChapterIndex
book.durChapterPos = 0 book.durChapterPos = 0
saveRead() saveRead()
@ -137,7 +137,7 @@ object AudioPlay {
book.lastCheckCount = 0 book.lastCheckCount = 0
book.durChapterTime = System.currentTimeMillis() book.durChapterTime = System.currentTimeMillis()
book.durChapterIndex = durChapterIndex book.durChapterIndex = durChapterIndex
book.durChapterPos = durPageIndex book.durChapterPos = durChapterPos
App.db.bookChapterDao().getChapter(book.bookUrl, book.durChapterIndex)?.let { App.db.bookChapterDao().getChapter(book.bookUrl, book.durChapterIndex)?.let {
book.durChapterTitle = it.title book.durChapterTitle = it.title
} }

@ -31,7 +31,7 @@ object ReadBook {
var inBookshelf = false var inBookshelf = false
var chapterSize = 0 var chapterSize = 0
var durChapterIndex = 0 var durChapterIndex = 0
var durPageIndex = 0 var durChapterPos = 0
var isLocalBook = true var isLocalBook = true
var callBack: CallBack? = null var callBack: CallBack? = null
var prevTextChapter: TextChapter? = null var prevTextChapter: TextChapter? = null
@ -50,7 +50,7 @@ object ReadBook {
readRecord.bookName = book.name readRecord.bookName = book.name
readRecord.readTime = App.db.readRecordDao().getReadTime(book.name) ?: 0 readRecord.readTime = App.db.readRecordDao().getReadTime(book.name) ?: 0
durChapterIndex = book.durChapterIndex durChapterIndex = book.durChapterIndex
durPageIndex = book.durChapterPos durChapterPos = book.durChapterPos
isLocalBook = book.origin == BookType.local isLocalBook = book.origin == BookType.local
chapterSize = 0 chapterSize = 0
prevTextChapter = null prevTextChapter = null
@ -96,14 +96,14 @@ object ReadBook {
} }
fun moveToNextPage() { fun moveToNextPage() {
durPageIndex++ durChapterPos = curTextChapter?.getNextPageLength(durChapterPos) ?: durChapterPos
callBack?.upContent() callBack?.upContent()
saveRead() saveRead()
} }
fun moveToNextChapter(upContent: Boolean): Boolean { fun moveToNextChapter(upContent: Boolean): Boolean {
if (durChapterIndex < chapterSize - 1) { if (durChapterIndex < chapterSize - 1) {
durPageIndex = 0 durChapterPos = 0
durChapterIndex++ durChapterIndex++
prevTextChapter = curTextChapter prevTextChapter = curTextChapter
curTextChapter = nextTextChapter curTextChapter = nextTextChapter
@ -133,7 +133,7 @@ object ReadBook {
fun moveToPrevChapter(upContent: Boolean, toLast: Boolean = true): Boolean { fun moveToPrevChapter(upContent: Boolean, toLast: Boolean = true): Boolean {
if (durChapterIndex > 0) { if (durChapterIndex > 0) {
durPageIndex = if (toLast) prevTextChapter?.lastIndex ?: 0 else 0 durChapterPos = if (toLast) prevTextChapter?.lastReadLength ?: 0 else 0
durChapterIndex-- durChapterIndex--
nextTextChapter = curTextChapter nextTextChapter = curTextChapter
curTextChapter = prevTextChapter curTextChapter = prevTextChapter
@ -161,15 +161,15 @@ object ReadBook {
} }
} }
fun skipToPage(page: Int) { fun skipToPage(index: Int) {
durPageIndex = page durChapterPos = curTextChapter?.getReadLength(index) ?: index
callBack?.upContent() callBack?.upContent()
curPageChanged() curPageChanged()
saveRead() saveRead()
} }
fun setPageIndex(pageIndex: Int) { fun setPageIndex(index: Int) {
durPageIndex = pageIndex durChapterPos = curTextChapter?.getReadLength(index) ?: index
saveRead() saveRead()
curPageChanged() curPageChanged()
} }
@ -191,19 +191,16 @@ object ReadBook {
if (book != null && textChapter != null) { if (book != null && textChapter != null) {
val key = IntentDataHelp.putData(textChapter) val key = IntentDataHelp.putData(textChapter)
ReadAloud.play( 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 { curTextChapter?.let {
if (durPageIndex < it.pageSize) { return it.getPageIndexByCharIndex(durChapterPos)
return durPageIndex
}
return it.pageSize - 1
} }
return durPageIndex return durChapterPos
} }
/** /**
@ -430,7 +427,7 @@ object ReadBook {
book.lastCheckCount = 0 book.lastCheckCount = 0
book.durChapterTime = System.currentTimeMillis() book.durChapterTime = System.currentTimeMillis()
book.durChapterIndex = durChapterIndex book.durChapterIndex = durChapterIndex
book.durChapterPos = durPageIndex book.durChapterPos = durChapterPos
App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let {
book.durChapterTitle = it.title book.durChapterTitle = it.title
} }

@ -208,7 +208,7 @@ class AudioPlayActivity :
binding.tvAllTime.text = DateFormatUtils.format(it.toLong(), "mm:ss") binding.tvAllTime.text = DateFormatUtils.format(it.toLong(), "mm:ss")
} }
observeEventSticky<Int>(EventBus.AUDIO_PROGRESS) { observeEventSticky<Int>(EventBus.AUDIO_PROGRESS) {
AudioPlay.durPageIndex = it AudioPlay.durChapterPos = it
if (!adjustProgress) binding.playerProgress.progress = it if (!adjustProgress) binding.playerProgress.progress = it
binding.tvDurTime.text = DateFormatUtils.format(it.toLong(), "mm:ss") binding.tvDurTime.text = DateFormatUtils.format(it.toLong(), "mm:ss")
} }

@ -31,7 +31,7 @@ class AudioPlayViewModel(application: Application) : BaseViewModel(application)
titleData.postValue(book.name) titleData.postValue(book.name)
coverData.postValue(book.getDisplayCover()) coverData.postValue(book.getDisplayCover())
durChapterIndex = book.durChapterIndex durChapterIndex = book.durChapterIndex
durPageIndex = book.durChapterPos durChapterPos = book.durChapterPos
App.db.bookChapterDao().getChapter(book.bookUrl, book.durChapterIndex)?.let { App.db.bookChapterDao().getChapter(book.bookUrl, book.durChapterIndex)?.let {
postEvent(EventBus.AUDIO_SUB_TITLE, it.title) postEvent(EventBus.AUDIO_SUB_TITLE, it.title)
} }

@ -500,7 +500,7 @@ class ReadBookActivity : ReadBookBaseActivity(),
autoPageProgress = 0 autoPageProgress = 0
launch { launch {
binding.pageView.upContent(relativePosition, resetPageOffset) binding.pageView.upContent(relativePosition, resetPageOffset)
binding.readMenu.setSeekPage(ReadBook.durPageIndex) binding.readMenu.setSeekPage(ReadBook.durPageIndex())
} }
loadStates = false loadStates = false
} }
@ -526,7 +526,7 @@ class ReadBookActivity : ReadBookBaseActivity(),
override fun pageChanged() { override fun pageChanged() {
autoPageProgress = 0 autoPageProgress = 0
launch { launch {
binding.readMenu.setSeekPage(ReadBook.durPageIndex) binding.readMenu.setSeekPage(ReadBook.durPageIndex())
} }
} }
@ -741,13 +741,12 @@ class ReadBookActivity : ReadBookBaseActivity(),
delay(100L) delay(100L)
pages = ReadBook.curTextChapter?.pages pages = ReadBook.curTextChapter?.pages
} }
val positions = val positions = ReadBook.searchResultPositions(
ReadBook.searchResultPositions( pages,
pages, indexWithinChapter,
indexWithinChapter, viewModel.searchContentQuery
viewModel.searchContentQuery )
) while (ReadBook.durPageIndex() != positions[0]) {
while (ReadBook.durPageIndex != positions[0]) {
delay(100L) delay(100L)
ReadBook.skipToPage(positions[0]) ReadBook.skipToPage(positions[0])
} }
@ -817,7 +816,7 @@ class ReadBookActivity : ReadBookBaseActivity(),
observeEvent<String>(EventBus.TIME_CHANGED) { binding.pageView.upTime() } observeEvent<String>(EventBus.TIME_CHANGED) { binding.pageView.upTime() }
observeEvent<Int>(EventBus.BATTERY_CHANGED) { binding.pageView.upBattery(it) } observeEvent<Int>(EventBus.BATTERY_CHANGED) { binding.pageView.upBattery(it) }
observeEvent<BookChapter>(EventBus.OPEN_CHAPTER) { observeEvent<BookChapter>(EventBus.OPEN_CHAPTER) {
viewModel.openChapter(it.index, ReadBook.durPageIndex) viewModel.openChapter(it.index, ReadBook.durChapterPos)
binding.pageView.upContent() binding.pageView.upContent()
} }
observeEvent<Boolean>(EventBus.MEDIA_BUTTON) { observeEvent<Boolean>(EventBus.MEDIA_BUTTON) {
@ -841,7 +840,7 @@ class ReadBookActivity : ReadBookBaseActivity(),
observeEvent<Int>(EventBus.ALOUD_STATE) { observeEvent<Int>(EventBus.ALOUD_STATE) {
if (it == Status.STOP || it == Status.PAUSE) { if (it == Status.STOP || it == Status.PAUSE) {
ReadBook.curTextChapter?.let { textChapter -> ReadBook.curTextChapter?.let { textChapter ->
val page = textChapter.page(ReadBook.durPageIndex) val page = textChapter.getPageByReadPos(ReadBook.durChapterPos)
if (page != null) { if (page != null) {
page.removePageAloudSpan() page.removePageAloudSpan()
binding.pageView.upContent(resetPageOffset = false) binding.pageView.upContent(resetPageOffset = false)
@ -853,9 +852,9 @@ class ReadBookActivity : ReadBookBaseActivity(),
launch(IO) { launch(IO) {
if (BaseReadAloudService.isPlay()) { if (BaseReadAloudService.isPlay()) {
ReadBook.curTextChapter?.let { textChapter -> ReadBook.curTextChapter?.let { textChapter ->
val pageStart = val pageStart = chapterStart - ReadBook.durChapterPos
chapterStart - textChapter.getReadLength(ReadBook.durPageIndex) textChapter.getPageByReadPos(ReadBook.durChapterPos)
textChapter.page(ReadBook.durPageIndex)?.upPageAloudSpan(pageStart) ?.upPageAloudSpan(pageStart)
upContent() upContent()
} }
} }

@ -214,7 +214,7 @@ abstract class ReadBookBaseActivity :
bookUrl = book.bookUrl, bookUrl = book.bookUrl,
bookName = book.name, bookName = book.name,
chapterIndex = ReadBook.durChapterIndex, chapterIndex = ReadBook.durChapterIndex,
pageIndex = ReadBook.durPageIndex, pageIndex = ReadBook.durChapterPos,
chapterName = textChapter.title, chapterName = textChapter.title,
content = editContent content = editContent
) )

@ -69,7 +69,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
ReadBook.book = book ReadBook.book = book
if (ReadBook.durChapterIndex != book.durChapterIndex) { if (ReadBook.durChapterIndex != book.durChapterIndex) {
ReadBook.durChapterIndex = book.durChapterIndex ReadBook.durChapterIndex = book.durChapterIndex
ReadBook.durPageIndex = book.durChapterPos ReadBook.durChapterPos = book.durChapterPos
ReadBook.prevTextChapter = null ReadBook.prevTextChapter = null
ReadBook.curTextChapter = null ReadBook.curTextChapter = null
ReadBook.nextTextChapter = 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.prevTextChapter = null
ReadBook.curTextChapter = null ReadBook.curTextChapter = null
ReadBook.nextTextChapter = null ReadBook.nextTextChapter = null
ReadBook.callBack?.upContent() ReadBook.callBack?.upContent()
if (index != ReadBook.durChapterIndex) { if (index != ReadBook.durChapterIndex) {
ReadBook.durChapterIndex = index ReadBook.durChapterIndex = index
ReadBook.durPageIndex = pageIndex ReadBook.durChapterPos = durChapterPos
} }
ReadBook.saveRead() ReadBook.saveRead()
ReadBook.loadContent(resetPageOffset = true) ReadBook.loadContent(resetPageOffset = true)

@ -287,7 +287,7 @@ class ReadMenu @JvmOverloads constructor(
binding.tvChapterUrl.gone() binding.tvChapterUrl.gone()
} }
binding.seekReadPage.max = it.pageSize.minus(1) binding.seekReadPage.max = it.pageSize.minus(1)
binding.seekReadPage.progress = ReadBook.durPageIndex binding.seekReadPage.progress = ReadBook.durPageIndex()
binding.tvPre.isEnabled = ReadBook.durChapterIndex != 0 binding.tvPre.isEnabled = ReadBook.durChapterIndex != 0
binding.tvNext.isEnabled = ReadBook.durChapterIndex != ReadBook.chapterSize - 1 binding.tvNext.isEnabled = ReadBook.durChapterIndex != ReadBook.chapterSize - 1
} ?: let { } ?: let {

@ -5,7 +5,7 @@ import io.legado.app.ui.book.read.page.entities.TextChapter
interface DataSource { interface DataSource {
val pageIndex: Int get() = ReadBook.durChapterPos() val pageIndex: Int get() = ReadBook.durPageIndex()
val currentChapter: TextChapter? val currentChapter: TextChapter?

@ -8,20 +8,25 @@ data class TextChapter(
val title: String, val title: String,
val url: String, val url: String,
val pages: List<TextPage>, val pages: List<TextPage>,
val pageLines: List<Int>,
val pageLengths: List<Int>,
val chaptersSize: Int, val chaptersSize: Int,
var titlePaint: TextPaint? = null, var titlePaint: TextPaint? = null,
var contentPaint: TextPaint? = null var contentPaint: TextPaint? = null
) { ) {
fun page(index: Int): TextPage? { fun page(index: Int): TextPage? {
return pages.getOrNull(index) return pages.getOrNull(index)
} }
fun getPageByReadPos(readPos: Int): TextPage? {
return page(getPageIndexByCharIndex(readPos))
}
val lastPage: TextPage? get() = pages.lastOrNull() val lastPage: TextPage? get() = pages.lastOrNull()
val lastIndex: Int get() = pages.lastIndex val lastIndex: Int get() = pages.lastIndex
val lastReadLength: Int get() = getReadLength(lastIndex)
val pageSize: Int get() = pages.size val pageSize: Int get() = pages.size
fun isLastIndex(index: Int): Boolean { fun isLastIndex(index: Int): Boolean {
@ -32,11 +37,15 @@ data class TextChapter(
var length = 0 var length = 0
val maxIndex = min(pageIndex, pages.size) val maxIndex = min(pageIndex, pages.size)
for (index in 0 until maxIndex) { for (index in 0 until maxIndex) {
length += pageLengths[index] length += pages[index].charSize
} }
return length return length
} }
fun getNextPageLength(length: Int): Int {
return getReadLength(getPageIndexByCharIndex(length) + 1)
}
fun getUnRead(pageIndex: Int): String { fun getUnRead(pageIndex: Int): String {
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
if (pages.isNotEmpty()) { if (pages.isNotEmpty()) {
@ -54,4 +63,15 @@ data class TextChapter(
} }
return stringBuilder.toString() 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
}
} }

@ -8,6 +8,7 @@ import io.legado.app.help.ReadBookConfig
import io.legado.app.ui.book.read.page.provider.ChapterProvider import io.legado.app.ui.book.read.page.provider.ChapterProvider
import java.text.DecimalFormat import java.text.DecimalFormat
@Suppress("unused")
data class TextPage( data class TextPage(
var index: Int = 0, var index: Int = 0,
var text: String = App.INSTANCE.getString(R.string.data_loading), var text: String = App.INSTANCE.getString(R.string.data_loading),
@ -16,9 +17,12 @@ data class TextPage(
var pageSize: Int = 0, var pageSize: Int = 0,
var chapterSize: Int = 0, var chapterSize: Int = 0,
var chapterIndex: 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 { fun upLinesPosition() = ChapterProvider.apply {
if (!ReadBookConfig.textBottomJustify) return@apply if (!ReadBookConfig.textBottomJustify) return@apply
if (textLines.size <= 1) return@apply if (textLines.size <= 1) return@apply

@ -83,8 +83,6 @@ object ChapterProvider {
imageStyle: String?, imageStyle: String?,
): TextChapter { ): TextChapter {
val textPages = arrayListOf<TextPage>() val textPages = arrayListOf<TextPage>()
val pageLines = arrayListOf<Int>()
val pageLengths = arrayListOf<Int>()
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
var durY = 0f var durY = 0f
var paint = Pair(titlePaint, contentPaint) var paint = Pair(titlePaint, contentPaint)
@ -109,21 +107,12 @@ object ChapterProvider {
val isTitle = index == 0 val isTitle = index == 0
val textPaint = if (isTitle) paint.first else paint.second val textPaint = if (isTitle) paint.first else paint.second
if (!(isTitle && ReadBookConfig.titleMode == 2)) { if (!(isTitle && ReadBookConfig.titleMode == 2)) {
durY = setTypeText( durY = setTypeText(text, durY, textPages, stringBuilder, isTitle, textPaint)
text, durY, textPages, pageLines,
pageLengths, stringBuilder, isTitle, textPaint
)
} }
} }
} }
textPages.last().height = durY + 20.dp textPages.last().height = durY + 20.dp
textPages.last().text = stringBuilder.toString() 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 -> textPages.forEachIndexed { index, item ->
item.index = index item.index = index
item.pageSize = textPages.size item.pageSize = textPages.size
@ -136,7 +125,7 @@ object ChapterProvider {
return TextChapter( return TextChapter(
bookChapter.index, bookChapter.title, bookChapter.index, bookChapter.title,
bookChapter.getAbsoluteURL().substringBefore(","), 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, text: String,
y: Float, y: Float,
textPages: ArrayList<TextPage>, textPages: ArrayList<TextPage>,
pageLines: ArrayList<Int>,
pageLengths: ArrayList<Int>,
stringBuilder: StringBuilder, stringBuilder: StringBuilder,
isTitle: Boolean, isTitle: Boolean,
textPaint: TextPaint textPaint: TextPaint
@ -247,8 +234,6 @@ object ChapterProvider {
if (durY + textPaint.textHeight > visibleHeight) { if (durY + textPaint.textHeight > visibleHeight) {
//当前页面结束,设置各种值 //当前页面结束,设置各种值
textPages.last().text = stringBuilder.toString() textPages.last().text = stringBuilder.toString()
pageLines.add(textPages.last().textLines.size)
pageLengths.add(textPages.last().text.length)
textPages.last().height = durY textPages.last().height = durY
//新建页面 //新建页面
textPages.add(TextPage()) textPages.add(TextPage())

Loading…
Cancel
Save