diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt index 4d44842f0..be1a968ab 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt @@ -42,7 +42,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea setSupportActionBar(toolbar) initAnimation() initView() - viewModel.chapterMaxIndex.observe(this, Observer { }) + viewModel.chapterMaxIndex.observe(this, Observer { bookLoadFinish() }) viewModel.initData(intent) savedInstanceState?.let { changeSourceDialog = supportFragmentManager.findFragmentByTag(ChangeSourceDialog.tag) as? ChangeSourceDialog @@ -222,6 +222,12 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea } } + private fun bookLoadFinish() { + viewModel.book?.let { + viewModel.loadContent(it, it.durChapterIndex) + } + } + override fun changeTo(book: Book) { } diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt index da91edc8a..7e470f80a 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt @@ -48,7 +48,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } - fun getContent(book: Book, index: Int) { + fun loadContent(book: Book, index: Int) { App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> BookHelp.getContent(book, chapter)?.let { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt index 32e4dc66f..3832a42c6 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt @@ -1,18 +1,22 @@ package io.legado.app.ui.widget.page -import android.widget.TextView +import io.legado.app.data.entities.BookChapter class ChapterProvider { - fun getTextChapter(textView: TextView, content: String) { - - textView.text = content - val layout = textView.layout - val topOfLastLine = textView.height - textView.paddingTop - textView.paddingBottom - textView.lineHeight - val lineNum = layout.getLineForVertical(topOfLastLine) - val lastCharNum = layout.getLineEnd(lineNum) + fun getTextChapter(textView: ContentTextView, bookChapter: BookChapter, content: String): TextChapter { + val textPages = arrayListOf() + var surplusText = content + var pageIndex = 0 + while (surplusText.isNotEmpty()) { + textView.text = surplusText + textPages.add(TextPage(pageIndex, surplusText.substring(0, textView.getCharNum()))) + surplusText = surplusText.substring(textView.getCharNum()) + pageIndex++ + } + return TextChapter(bookChapter.index, textPages) }