pull/32/head
kunfei 5 years ago
parent 8ab491d573
commit 3f43bdfb5c
  1. 24
      app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt
  2. 21
      app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt
  3. 23
      app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt
  4. 4
      app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt
  5. 5
      app/src/main/java/io/legado/app/ui/widget/page/TextPage.kt

@ -61,7 +61,6 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
page_view.callback = this
viewModel.callBack = this
viewModel.bookData.observe(this, Observer { title_bar.title = it.name })
viewModel.chapterListFinish.observe(this, Observer { bookLoadFinish() })
viewModel.initData(intent)
savedInstanceState?.let {
changeSourceDialog =
@ -225,17 +224,6 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
return super.onKeyDown(keyCode, event)
}
/**
* 书籍加载完成,开始加载章节内容
*/
private fun bookLoadFinish() {
viewModel.bookData.value?.let {
viewModel.loadContent(it, viewModel.durChapterIndex)
viewModel.loadContent(it, viewModel.durChapterIndex + 1)
viewModel.loadContent(it, viewModel.durChapterIndex - 1)
}
}
/**
* 加载章节内容
*/
@ -251,19 +239,19 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
override fun contentLoadFinish(bookChapter: BookChapter, content: String) {
when (bookChapter.index) {
viewModel.durChapterIndex -> launch {
viewModel.curTextChapter =
ChapterProvider.getTextChapter(content_text_view, bookChapter, content)
viewModel.curTextChapter = ChapterProvider
.getTextChapter(content_text_view, bookChapter, content, viewModel.chapterSize)
page_view.upContent()
curChapterChanged()
}
viewModel.durChapterIndex - 1 -> launch {
viewModel.prevTextChapter =
ChapterProvider.getTextChapter(content_text_view, bookChapter, content)
viewModel.prevTextChapter = ChapterProvider
.getTextChapter(content_text_view, bookChapter, content, viewModel.chapterSize)
page_view.upContent()
}
viewModel.durChapterIndex + 1 -> launch {
viewModel.nextTextChapter =
ChapterProvider.getTextChapter(content_text_view, bookChapter, content)
viewModel.nextTextChapter = ChapterProvider
.getTextChapter(content_text_view, bookChapter, content, viewModel.chapterSize)
page_view.upContent()
}
}

@ -21,7 +21,6 @@ import kotlinx.coroutines.launch
class ReadBookViewModel(application: Application) : BaseViewModel(application) {
var inBookshelf = false
var bookData = MutableLiveData<Book>()
val chapterListFinish = MutableLiveData<Boolean>()
var chapterSize = 0
var bookSource: BookSource? = null
var callBack: CallBack? = null
@ -61,7 +60,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
durChapterIndex = count - 1
}
chapterSize = count
chapterListFinish.postValue(true)
loadChapterListFinish()
}
}
@ -85,7 +84,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
if (!cList.isNullOrEmpty()) {
App.db.bookChapterDao().insert(*cList.toTypedArray())
chapterSize = cList.size
chapterListFinish.postValue(true)
loadChapterListFinish()
} else {
toast(R.string.error_load_toc)
}
@ -95,6 +94,14 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
}
}
private fun loadChapterListFinish() {
bookData.value?.let {
loadContent(it, durChapterIndex)
loadContent(it, durChapterIndex + 1)
loadContent(it, durChapterIndex - 1)
}
}
fun loadContent(book: Book, index: Int) {
synchronized(loadingLock) {
if (loadingChapters.contains(index)) return
@ -193,18 +200,22 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
durPageIndex = 0
}
saveRead()
chapterListFinish.postValue(true)
loadChapterListFinish()
}
fun saveRead() {
execute {
bookData.value?.let { book ->
book.durChapterTime = System.currentTimeMillis()
book.durChapterIndex = durChapterIndex
book.durChapterPos = durPageIndex
curTextChapter?.let {
book.durChapterTitle = it.title
App.db.bookDao().update(book)
}
}
}
}
fun removeFromBookshelf(success: (() -> Unit)?) {
execute {
@ -233,7 +244,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
loadContent(it, durChapterIndex.plus(1))
launch(IO) {
for (i in 2..10) {
delay(5000L)
delay(1000L)
bookData.value?.let { book ->
download(book, durChapterIndex + i)
}

@ -13,7 +13,10 @@ object ChapterProvider {
val readAloudSpan = ForegroundColorSpan(App.INSTANCE.accentColor)
private val titleSpan = RelativeSizeSpan(1.3f)
fun getTextChapter(textView: ContentTextView, bookChapter: BookChapter, content: String): TextChapter {
fun getTextChapter(
textView: ContentTextView, bookChapter: BookChapter,
content: String, chapterSize: Int
): TextChapter {
val textPages = arrayListOf<TextPage>()
val pageLengths = arrayListOf<Int>()
var surplusText = content
@ -32,24 +35,26 @@ object ChapterProvider {
pageLengths.add(textView.getCharNum())
textPages.add(
TextPage(
pageIndex,
spannableStringBuilder.delete(
index = pageIndex,
text = spannableStringBuilder.delete(
pageLengths[pageIndex],
spannableStringBuilder.length
),
bookChapter.title
title = bookChapter.title,
chapterSize = chapterSize,
chapterIndex = bookChapter.index
)
)
surplusText = surplusText.substring(pageLengths[pageIndex])
pageIndex++
}
for (item in textPages) {
item.pageSize = textPages.size
}
return TextChapter(
bookChapter.index,
bookChapter.title,
bookChapter.url,
textPages,
pageLengths
bookChapter.index, bookChapter.title, bookChapter.url,
textPages, pageLengths
)
}

@ -1,5 +1,6 @@
package io.legado.app.ui.widget.page
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
@ -79,9 +80,10 @@ class ContentView : FrameLayout {
tv_top_right.text = context.getString(R.string.battery_show, battery)
}
@SuppressLint("SetTextI18n")
fun setContent(page: TextPage?) {
content_text_view.text = page?.text
tv_bottom_left.text = page?.title
tv_bottom_right.text = page?.index?.plus(1)?.toString()
tv_bottom_right.text = "${page?.index?.plus(1)}/${page?.pageSize}"
}
}

@ -3,5 +3,8 @@ package io.legado.app.ui.widget.page
data class TextPage(
val index: Int,
val text: CharSequence,
val title: String
val title: String,
var pageSize: Int = 0,
var chapterSize: Int = 0,
var chapterIndex: Int = 0
)

Loading…
Cancel
Save