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

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() {
launch(IO) {
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 chapterSize = 0
var durChapterIndex = 0
var durPageIndex = 0
var durChapterPos = 0
var webBook: WebBook? = null
val loadingChapters = arrayListOf<Int>()
@ -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
}

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

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

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

@ -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<String>(EventBus.TIME_CHANGED) { binding.pageView.upTime() }
observeEvent<Int>(EventBus.BATTERY_CHANGED) { binding.pageView.upBattery(it) }
observeEvent<BookChapter>(EventBus.OPEN_CHAPTER) {
viewModel.openChapter(it.index, ReadBook.durPageIndex)
viewModel.openChapter(it.index, ReadBook.durChapterPos)
binding.pageView.upContent()
}
observeEvent<Boolean>(EventBus.MEDIA_BUTTON) {
@ -841,7 +840,7 @@ class ReadBookActivity : ReadBookBaseActivity(),
observeEvent<Int>(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()
}
}

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

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

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

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

@ -8,20 +8,25 @@ data class TextChapter(
val title: String,
val url: String,
val pages: List<TextPage>,
val pageLines: List<Int>,
val pageLengths: List<Int>,
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
}
}

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

@ -83,8 +83,6 @@ object ChapterProvider {
imageStyle: String?,
): TextChapter {
val textPages = arrayListOf<TextPage>()
val pageLines = arrayListOf<Int>()
val pageLengths = arrayListOf<Int>()
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<TextPage>,
pageLines: ArrayList<Int>,
pageLengths: ArrayList<Int>,
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())

Loading…
Cancel
Save