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. 23
      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 page_view.callback = this
viewModel.callBack = this viewModel.callBack = this
viewModel.bookData.observe(this, Observer { title_bar.title = it.name }) viewModel.bookData.observe(this, Observer { title_bar.title = it.name })
viewModel.chapterListFinish.observe(this, Observer { bookLoadFinish() })
viewModel.initData(intent) viewModel.initData(intent)
savedInstanceState?.let { savedInstanceState?.let {
changeSourceDialog = changeSourceDialog =
@ -225,17 +224,6 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
return super.onKeyDown(keyCode, event) 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) { override fun contentLoadFinish(bookChapter: BookChapter, content: String) {
when (bookChapter.index) { when (bookChapter.index) {
viewModel.durChapterIndex -> launch { viewModel.durChapterIndex -> launch {
viewModel.curTextChapter = viewModel.curTextChapter = ChapterProvider
ChapterProvider.getTextChapter(content_text_view, bookChapter, content) .getTextChapter(content_text_view, bookChapter, content, viewModel.chapterSize)
page_view.upContent() page_view.upContent()
curChapterChanged() curChapterChanged()
} }
viewModel.durChapterIndex - 1 -> launch { viewModel.durChapterIndex - 1 -> launch {
viewModel.prevTextChapter = viewModel.prevTextChapter = ChapterProvider
ChapterProvider.getTextChapter(content_text_view, bookChapter, content) .getTextChapter(content_text_view, bookChapter, content, viewModel.chapterSize)
page_view.upContent() page_view.upContent()
} }
viewModel.durChapterIndex + 1 -> launch { viewModel.durChapterIndex + 1 -> launch {
viewModel.nextTextChapter = viewModel.nextTextChapter = ChapterProvider
ChapterProvider.getTextChapter(content_text_view, bookChapter, content) .getTextChapter(content_text_view, bookChapter, content, viewModel.chapterSize)
page_view.upContent() page_view.upContent()
} }
} }

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

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

@ -1,5 +1,6 @@
package io.legado.app.ui.widget.page package io.legado.app.ui.widget.page
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.util.AttributeSet import android.util.AttributeSet
@ -79,9 +80,10 @@ class ContentView : FrameLayout {
tv_top_right.text = context.getString(R.string.battery_show, battery) tv_top_right.text = context.getString(R.string.battery_show, battery)
} }
@SuppressLint("SetTextI18n")
fun setContent(page: TextPage?) { fun setContent(page: TextPage?) {
content_text_view.text = page?.text content_text_view.text = page?.text
tv_bottom_left.text = page?.title 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( data class TextPage(
val index: Int, val index: Int,
val text: CharSequence, val text: CharSequence,
val title: String val title: String,
var pageSize: Int = 0,
var chapterSize: Int = 0,
var chapterIndex: Int = 0
) )

Loading…
Cancel
Save