pull/32/head
kunfei 5 years ago
parent 3f43bdfb5c
commit 4fb7b6cd30
  1. 12
      app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt
  2. 23
      app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt

@ -61,6 +61,7 @@ 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 =
@ -224,6 +225,17 @@ 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)
}
}
/** /**
* 加载章节内容 * 加载章节内容
*/ */

@ -21,6 +21,7 @@ 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
@ -60,7 +61,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
durChapterIndex = count - 1 durChapterIndex = count - 1
} }
chapterSize = count chapterSize = count
loadChapterListFinish() chapterListFinish.postValue(true)
} }
} }
@ -84,7 +85,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
loadChapterListFinish() chapterListFinish.postValue(true)
} else { } else {
toast(R.string.error_load_toc) toast(R.string.error_load_toc)
} }
@ -94,14 +95,6 @@ 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
@ -125,7 +118,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
} }
fun download(book: Book, index: Int) { private fun download(book: Book, index: Int) {
synchronized(loadingLock) { synchronized(loadingLock) {
if (loadingChapters.contains(index)) return if (loadingChapters.contains(index)) return
loadingChapters.add(index) loadingChapters.add(index)
@ -200,19 +193,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
durPageIndex = 0 durPageIndex = 0
} }
saveRead() saveRead()
loadChapterListFinish() chapterListFinish.postValue(true)
} }
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
curTextChapter?.let { App.db.bookDao().update(book)
book.durChapterTitle = it.title
App.db.bookDao().update(book)
}
} }
} }
} }

Loading…
Cancel
Save