pull/32/head
kunfei 5 years ago
parent a605cc4d69
commit 4d19aaedb3
  1. 36
      app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt
  2. 20
      app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt
  3. 2
      app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt
  4. 22
      app/src/main/java/io/legado/app/ui/widget/page/PageView.kt

@ -53,7 +53,6 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
initAnimation() initAnimation()
initView() initView()
viewModel.callBack = this viewModel.callBack = this
viewModel.chapterMaxIndex.observe(this, Observer { bookLoadFinish() })
viewModel.bookData.observe(this, Observer { title_bar.title = it.name }) viewModel.bookData.observe(this, Observer { title_bar.title = it.name })
viewModel.initData(intent) viewModel.initData(intent)
savedInstanceState?.let { savedInstanceState?.let {
@ -235,7 +234,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
} }
} }
private fun bookLoadFinish() { override fun bookLoadFinish() {
viewModel.bookData.value?.let { viewModel.bookData.value?.let {
viewModel.loadContent(it, viewModel.durChapterIndex) viewModel.loadContent(it, viewModel.durChapterIndex)
viewModel.loadContent(it, viewModel.durChapterIndex + 1) viewModel.loadContent(it, viewModel.durChapterIndex + 1)
@ -243,7 +242,13 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
} }
} }
override fun onLoadFinish(bookChapter: BookChapter, content: String) { override fun loadChapter(index: Int) {
viewModel.bookData.value?.let {
viewModel.loadContent(it, index)
}
}
override fun contentLoadFinish(bookChapter: BookChapter, content: String) {
launch { launch {
when (bookChapter.index) { when (bookChapter.index) {
viewModel.durChapterIndex -> { viewModel.durChapterIndex -> {
@ -268,12 +273,16 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
} }
} }
override fun chapterSize(): Int {
return viewModel.chapterSize
}
override fun oldBook(): Book? { override fun oldBook(): Book? {
return viewModel.bookData.value return viewModel.bookData.value
} }
override fun changeTo(book: Book) { override fun changeTo(book: Book) {
viewModel.changeTo(book)
} }
override fun durChapterIndex(): Int { override fun durChapterIndex(): Int {
@ -296,15 +305,6 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
} }
} }
fun upStyle() {
content_view.upStyle()
page_view.upStyle()
}
fun upBg() {
page_view.upBg()
}
private fun onClickReadAloud() { private fun onClickReadAloud() {
if (!ReadAloudService.isRun) { if (!ReadAloudService.isRun) {
readAloudStatus = Status.STOP readAloudStatus = Status.STOP
@ -332,10 +332,14 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
observeEventSticky<Int>(Bus.UP_CONFIG) { observeEventSticky<Int>(Bus.UP_CONFIG) {
when (it) { when (it) {
0 -> { 0 -> {
upBg() page_view.upBg()
upStyle() content_view.upStyle()
page_view.upStyle()
}
1 -> {
content_view.upStyle()
page_view.upStyle()
} }
1 -> upStyle()
} }
} }
} }

@ -2,7 +2,6 @@ package io.legado.app.ui.readbook
import android.app.Application import android.app.Application
import android.content.Intent import android.content.Intent
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
@ -20,7 +19,7 @@ import kotlinx.coroutines.Dispatchers.IO
class ReadBookViewModel(application: Application) : BaseViewModel(application) { class ReadBookViewModel(application: Application) : BaseViewModel(application) {
var bookData = MutableLiveData<Book>() var bookData = MutableLiveData<Book>()
var chapterMaxIndex = MediatorLiveData<Int>() var chapterSize = 0
var bookSource: BookSource? = null var bookSource: BookSource? = null
var webBook: WebBook? = null var webBook: WebBook? = null
var callBack: CallBack? = null var callBack: CallBack? = null
@ -52,7 +51,8 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
?.onSuccess(IO) { cList -> ?.onSuccess(IO) { cList ->
if (!cList.isNullOrEmpty()) { if (!cList.isNullOrEmpty()) {
App.db.bookChapterDao().insert(*cList.toTypedArray()) App.db.bookChapterDao().insert(*cList.toTypedArray())
chapterMaxIndex.postValue(cList.size) chapterSize = cList.size
callBack?.bookLoadFinish()
} else { } else {
toast(R.string.error_load_toc) toast(R.string.error_load_toc)
} }
@ -63,7 +63,8 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
if (durChapterIndex > count - 1) { if (durChapterIndex > count - 1) {
durChapterIndex = count - 1 durChapterIndex = count - 1
} }
chapterMaxIndex.postValue(count) chapterSize = count
callBack?.bookLoadFinish()
} }
} }
@ -80,7 +81,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
execute { execute {
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 {
callBack?.onLoadFinish(chapter, it) callBack?.contentLoadFinish(chapter, it)
synchronized(loadingLock) { synchronized(loadingLock) {
loadingChapters.remove(index) loadingChapters.remove(index)
} }
@ -118,7 +119,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
?.onSuccess(IO) { content -> ?.onSuccess(IO) { content ->
content?.let { content?.let {
BookHelp.saveContent(book, chapter, it) BookHelp.saveContent(book, chapter, it)
callBack?.onLoadFinish(chapter, it) callBack?.contentLoadFinish(chapter, it)
synchronized(loadingLock) { synchronized(loadingLock) {
loadingChapters.remove(chapter.index) loadingChapters.remove(chapter.index)
} }
@ -130,6 +131,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
} }
fun changeTo(book: Book) {
}
private fun autoChangeSource() { private fun autoChangeSource() {
} }
@ -140,6 +145,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
interface CallBack { interface CallBack {
fun onLoadFinish(bookChapter: BookChapter, content: String) fun bookLoadFinish()
fun contentLoadFinish(bookChapter: BookChapter, content: String)
} }
} }

@ -6,7 +6,7 @@ interface DataSource {
fun isPrepared(): Boolean fun isPrepared(): Boolean
fun getChapterPosition() fun getChapterPosition(): Int
fun getChapter(position: Int): TextChapter? fun getChapter(position: Int): TextChapter?

@ -37,8 +37,8 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
override fun getChapterPosition() { override fun getChapterPosition(): Int {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. return callback?.durChapterIndex() ?: 0
} }
override fun getChapter(position: Int): TextChapter? { override fun getChapter(position: Int): TextChapter? {
@ -46,23 +46,29 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att
} }
override fun getCurrentChapter(): TextChapter? { override fun getCurrentChapter(): TextChapter? {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. return callback?.textChapter(0)
} }
override fun getNextChapter(): TextChapter? { override fun getNextChapter(): TextChapter? {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. return callback?.textChapter(1)
} }
override fun getPreviousChapter(): TextChapter? { override fun getPreviousChapter(): TextChapter? {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. return callback?.textChapter(-1)
} }
override fun hasNextChapter(): Boolean { override fun hasNextChapter(): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. callback?.let {
return it.durChapterIndex() < it.chapterSize() - 1
}
return false
} }
override fun hasPrevChapter(): Boolean { override fun hasPrevChapter(): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. callback?.let {
return it.durChapterIndex() > 0
}
return false
} }
})) }))
} }
@ -190,8 +196,10 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att
} }
interface CallBack { interface CallBack {
fun chapterSize(): Int
fun durChapterIndex(): Int fun durChapterIndex(): Int
fun durChapterPos(pageSize: Int): Int fun durChapterPos(pageSize: Int): Int
fun textChapter(chapterOnDur: Int = 0): TextChapter? fun textChapter(chapterOnDur: Int = 0): TextChapter?
fun loadChapter(index: Int)
} }
} }

Loading…
Cancel
Save