pull/32/head
kunfei 6 years ago
parent 04b471f7c1
commit 20fd00f165
  1. 8
      app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt
  2. 2
      app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt
  3. 20
      app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt

@ -42,7 +42,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
initAnimation() initAnimation()
initView() initView()
viewModel.chapterMaxIndex.observe(this, Observer { }) viewModel.chapterMaxIndex.observe(this, Observer { bookLoadFinish() })
viewModel.initData(intent) viewModel.initData(intent)
savedInstanceState?.let { savedInstanceState?.let {
changeSourceDialog = supportFragmentManager.findFragmentByTag(ChangeSourceDialog.tag) as? ChangeSourceDialog changeSourceDialog = supportFragmentManager.findFragmentByTag(ChangeSourceDialog.tag) as? ChangeSourceDialog
@ -222,6 +222,12 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
} }
} }
private fun bookLoadFinish() {
viewModel.book?.let {
viewModel.loadContent(it, it.durChapterIndex)
}
}
override fun changeTo(book: Book) { override fun changeTo(book: Book) {
} }

@ -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 -> App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
BookHelp.getContent(book, chapter)?.let { BookHelp.getContent(book, chapter)?.let {

@ -1,18 +1,22 @@
package io.legado.app.ui.widget.page package io.legado.app.ui.widget.page
import android.widget.TextView import io.legado.app.data.entities.BookChapter
class ChapterProvider { class ChapterProvider {
fun getTextChapter(textView: TextView, content: String) { fun getTextChapter(textView: ContentTextView, bookChapter: BookChapter, content: String): TextChapter {
val textPages = arrayListOf<TextPage>()
textView.text = content var surplusText = content
val layout = textView.layout var pageIndex = 0
val topOfLastLine = textView.height - textView.paddingTop - textView.paddingBottom - textView.lineHeight while (surplusText.isNotEmpty()) {
val lineNum = layout.getLineForVertical(topOfLastLine) textView.text = surplusText
val lastCharNum = layout.getLineEnd(lineNum) textPages.add(TextPage(pageIndex, surplusText.substring(0, textView.getCharNum())))
surplusText = surplusText.substring(textView.getCharNum())
pageIndex++
}
return TextChapter(bookChapter.index, textPages)
} }

Loading…
Cancel
Save