pull/49/head
kunfei 6 years ago
parent 9742b57980
commit 085b1012ee
  1. 27
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  2. 113
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  3. 171
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -0,0 +1,27 @@
package io.legado.app.service.help
import androidx.lifecycle.MutableLiveData
import io.legado.app.data.entities.Book
import io.legado.app.model.WebBook
import io.legado.app.ui.book.read.ReadBookViewModel
import io.legado.app.ui.widget.page.TextChapter
object ReadBook {
var titleDate = MutableLiveData<String>()
var book: Book? = null
var inBookshelf = false
var chapterSize = 0
var callBack: ReadBookViewModel.CallBack? = null
var durChapterIndex = 0
var durPageIndex = 0
var isLocalBook = true
var prevTextChapter: TextChapter? = null
var curTextChapter: TextChapter? = null
var nextTextChapter: TextChapter? = null
var webBook: WebBook? = null
val loadingChapters = arrayListOf<Int>()
}

@ -25,6 +25,7 @@ import io.legado.app.lib.dialogs.okButton
import io.legado.app.receiver.TimeElectricityReceiver import io.legado.app.receiver.TimeElectricityReceiver
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
import io.legado.app.service.help.ReadAloud import io.legado.app.service.help.ReadAloud
import io.legado.app.service.help.ReadBook
import io.legado.app.ui.book.read.config.* import io.legado.app.ui.book.read.config.*
import io.legado.app.ui.book.read.config.BgTextConfigDialog.Companion.BG_COLOR import io.legado.app.ui.book.read.config.BgTextConfigDialog.Companion.BG_COLOR
import io.legado.app.ui.book.read.config.BgTextConfigDialog.Companion.TEXT_COLOR import io.legado.app.ui.book.read.config.BgTextConfigDialog.Companion.TEXT_COLOR
@ -70,8 +71,8 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
Help.upLayoutInDisplayCutoutMode(window) Help.upLayoutInDisplayCutoutMode(window)
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
initView() initView()
viewModel.callBack = this ReadBook.callBack = this
viewModel.titleDate.observe(this, Observer { title_bar.title = it }) ReadBook.titleDate.observe(this, Observer { title_bar.title = it })
viewModel.initData(intent) viewModel.initData(intent)
} }
@ -101,7 +102,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
*/ */
private fun initView() { private fun initView() {
tv_chapter_name.onClick { tv_chapter_name.onClick {
viewModel.webBook?.let { ReadBook.webBook?.let {
startActivityForResult<BookSourceEditActivity>( startActivityForResult<BookSourceEditActivity>(
requestCodeEditSource, requestCodeEditSource,
Pair("data", it.bookSource.bookSourceUrl) Pair("data", it.bookSource.bookSourceUrl)
@ -138,13 +139,13 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
when (item.itemId) { when (item.itemId) {
R.id.menu_change_source -> { R.id.menu_change_source -> {
read_menu.runMenuOut() read_menu.runMenuOut()
viewModel.book?.let { ReadBook.book?.let {
ChangeSourceDialog.show(supportFragmentManager, it.name, it.author) ChangeSourceDialog.show(supportFragmentManager, it.name, it.author)
} }
} }
R.id.menu_refresh -> { R.id.menu_refresh -> {
viewModel.book?.let { ReadBook.book?.let {
viewModel.curTextChapter = null ReadBook.curTextChapter = null
page_view.upContent() page_view.upContent()
viewModel.refreshContent(it) viewModel.refreshContent(it)
} }
@ -252,9 +253,9 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
* 加载章节内容 * 加载章节内容
*/ */
override fun loadContent() { override fun loadContent() {
viewModel.loadContent(viewModel.durChapterIndex) viewModel.loadContent(ReadBook.durChapterIndex)
viewModel.loadContent(viewModel.durChapterIndex + 1) viewModel.loadContent(ReadBook.durChapterIndex + 1)
viewModel.loadContent(viewModel.durChapterIndex - 1) viewModel.loadContent(ReadBook.durChapterIndex - 1)
} }
/** /**
@ -269,9 +270,9 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
*/ */
override fun contentLoadFinish(bookChapter: BookChapter, content: String) { override fun contentLoadFinish(bookChapter: BookChapter, content: String) {
when (bookChapter.index) { when (bookChapter.index) {
viewModel.durChapterIndex -> launch { ReadBook.durChapterIndex -> launch {
viewModel.curTextChapter = ChapterProvider ReadBook.curTextChapter = ChapterProvider
.getTextChapter(bookChapter, content, viewModel.chapterSize) .getTextChapter(bookChapter, content, ReadBook.chapterSize)
page_view.upContent() page_view.upContent()
curChapterChanged() curChapterChanged()
if (intent.getBooleanExtra("readAloud", false)) { if (intent.getBooleanExtra("readAloud", false)) {
@ -279,14 +280,14 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
readAloud() readAloud()
} }
} }
viewModel.durChapterIndex - 1 -> launch { ReadBook.durChapterIndex - 1 -> launch {
viewModel.prevTextChapter = ChapterProvider ReadBook.prevTextChapter = ChapterProvider
.getTextChapter(bookChapter, content, viewModel.chapterSize) .getTextChapter(bookChapter, content, ReadBook.chapterSize)
page_view.upContent(-1) page_view.upContent(-1)
} }
viewModel.durChapterIndex + 1 -> launch { ReadBook.durChapterIndex + 1 -> launch {
viewModel.nextTextChapter = ChapterProvider ReadBook.nextTextChapter = ChapterProvider
.getTextChapter(bookChapter, content, viewModel.chapterSize) .getTextChapter(bookChapter, content, ReadBook.chapterSize)
page_view.upContent(1) page_view.upContent(1)
} }
} }
@ -297,22 +298,22 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} }
private fun curChapterChanged() { private fun curChapterChanged() {
viewModel.curTextChapter?.let { ReadBook.curTextChapter?.let {
tv_chapter_name.text = it.title tv_chapter_name.text = it.title
tv_chapter_name.visible() tv_chapter_name.visible()
if (!viewModel.isLocalBook) { if (!ReadBook.isLocalBook) {
tv_chapter_url.text = it.url tv_chapter_url.text = it.url
tv_chapter_url.visible() tv_chapter_url.visible()
} }
seek_read_page.max = it.pageSize().minus(1) seek_read_page.max = it.pageSize().minus(1)
tv_pre.isEnabled = viewModel.durChapterIndex != 0 tv_pre.isEnabled = ReadBook.durChapterIndex != 0
tv_next.isEnabled = viewModel.durChapterIndex != viewModel.chapterSize - 1 tv_next.isEnabled = ReadBook.durChapterIndex != ReadBook.chapterSize - 1
curPageChanged() curPageChanged()
} }
} }
private fun curPageChanged() { private fun curPageChanged() {
seek_read_page.progress = viewModel.durPageIndex seek_read_page.progress = ReadBook.durPageIndex
when (readAloudStatus) { when (readAloudStatus) {
Status.PLAY -> readAloud() Status.PLAY -> readAloud()
Status.PAUSE -> { Status.PAUSE -> {
@ -326,35 +327,35 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} }
override fun chapterSize(): Int { override fun chapterSize(): Int {
return viewModel.chapterSize return ReadBook.chapterSize
} }
override val curOrigin: String? override val curOrigin: String?
get() = viewModel.book?.origin get() = ReadBook.book?.origin
override val oldBook: Book? override val oldBook: Book?
get() = viewModel.book get() = ReadBook.book
override fun changeTo(book: Book) { override fun changeTo(book: Book) {
viewModel.changeTo(book) viewModel.changeTo(book)
} }
override fun durChapterIndex(): Int { override fun durChapterIndex(): Int {
return viewModel.durChapterIndex return ReadBook.durChapterIndex
} }
override fun durChapterPos(): Int { override fun durChapterPos(): Int {
viewModel.curTextChapter?.let { ReadBook.curTextChapter?.let {
if (viewModel.durPageIndex < it.pageSize()) { if (ReadBook.durPageIndex < it.pageSize()) {
return viewModel.durPageIndex return ReadBook.durPageIndex
} }
return it.pageSize() - 1 return it.pageSize() - 1
} }
return viewModel.durPageIndex return ReadBook.durPageIndex
} }
override fun setPageIndex(pageIndex: Int) { override fun setPageIndex(pageIndex: Int) {
viewModel.durPageIndex = pageIndex ReadBook.durPageIndex = pageIndex
viewModel.saveRead() viewModel.saveRead()
curPageChanged() curPageChanged()
} }
@ -364,9 +365,9 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
*/ */
override fun textChapter(chapterOnDur: Int): TextChapter? { override fun textChapter(chapterOnDur: Int): TextChapter? {
return when (chapterOnDur) { return when (chapterOnDur) {
0 -> viewModel.curTextChapter 0 -> ReadBook.curTextChapter
1 -> viewModel.nextTextChapter 1 -> ReadBook.nextTextChapter
-1 -> viewModel.prevTextChapter -1 -> ReadBook.prevTextChapter
else -> null else -> null
} }
} }
@ -375,8 +376,8 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
* 下一页 * 下一页
*/ */
override fun moveToNextChapter(upContent: Boolean): Boolean { override fun moveToNextChapter(upContent: Boolean): Boolean {
return if (viewModel.durChapterIndex < viewModel.chapterSize - 1) { return if (ReadBook.durChapterIndex < ReadBook.chapterSize - 1) {
viewModel.durPageIndex = 0 ReadBook.durPageIndex = 0
viewModel.moveToNextChapter(upContent) viewModel.moveToNextChapter(upContent)
viewModel.saveRead() viewModel.saveRead()
curChapterChanged() curChapterChanged()
@ -390,8 +391,8 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
* 上一页 * 上一页
*/ */
override fun moveToPrevChapter(upContent: Boolean, last: Boolean): Boolean { override fun moveToPrevChapter(upContent: Boolean, last: Boolean): Boolean {
return if (viewModel.durChapterIndex > 0) { return if (ReadBook.durChapterIndex > 0) {
viewModel.durPageIndex = if (last) viewModel.prevTextChapter?.lastIndex() ?: 0 else 0 ReadBook.durPageIndex = if (last) ReadBook.prevTextChapter?.lastIndex() ?: 0 else 0
viewModel.moveToPrevChapter(upContent) viewModel.moveToPrevChapter(upContent)
viewModel.saveRead() viewModel.saveRead()
curChapterChanged() curChapterChanged()
@ -418,7 +419,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} }
override fun skipToPage(page: Int) { override fun skipToPage(page: Int) {
viewModel.durPageIndex = page ReadBook.durPageIndex = page
page_view.upContent() page_view.upContent()
curPageChanged() curPageChanged()
viewModel.saveRead() viewModel.saveRead()
@ -429,7 +430,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} }
override fun openChapterList() { override fun openChapterList() {
viewModel.book?.let { ReadBook.book?.let {
startActivityForResult<ChapterListActivity>( startActivityForResult<ChapterListActivity>(
requestCodeChapterList, requestCodeChapterList,
Pair("bookUrl", it.bookUrl) Pair("bookUrl", it.bookUrl)
@ -468,15 +469,15 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
* 朗读 * 朗读
*/ */
private fun readAloud(play: Boolean = true) { private fun readAloud(play: Boolean = true) {
val book = viewModel.book val book = ReadBook.book
val textChapter = viewModel.curTextChapter val textChapter = ReadBook.curTextChapter
if (book != null && textChapter != null) { if (book != null && textChapter != null) {
val key = IntentDataHelp.putData(textChapter) val key = IntentDataHelp.putData(textChapter)
ReadAloud.play( ReadAloud.play(
this, this,
book.name, book.name,
textChapter.title, textChapter.title,
viewModel.durPageIndex, ReadBook.durPageIndex,
key, key,
play play
) )
@ -508,7 +509,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
when (requestCode) { when (requestCode) {
requestCodeEditSource -> viewModel.upBookSource() requestCodeEditSource -> viewModel.upBookSource()
requestCodeChapterList -> requestCodeChapterList ->
data?.getIntExtra("index", viewModel.durChapterIndex)?.let { data?.getIntExtra("index", ReadBook.durChapterIndex)?.let {
viewModel.openChapter(it) viewModel.openChapter(it)
} }
} }
@ -516,11 +517,11 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} }
override fun finish() { override fun finish() {
viewModel.book?.let { ReadBook.book?.let {
if (!viewModel.inBookshelf) { if (!ReadBook.inBookshelf) {
this.alert(title = getString(R.string.add_to_shelf)) { this.alert(title = getString(R.string.add_to_shelf)) {
message = getString(R.string.check_add_bookshelf, it.name) message = getString(R.string.check_add_bookshelf, it.name)
okButton { viewModel.inBookshelf = true } okButton { ReadBook.inBookshelf = true }
noButton { viewModel.removeFromBookshelf { super.finish() } } noButton { viewModel.removeFromBookshelf { super.finish() } }
}.show().applyTint() }.show().applyTint()
} else { } else {
@ -534,8 +535,8 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
observeEvent<Int>(Bus.ALOUD_STATE) { observeEvent<Int>(Bus.ALOUD_STATE) {
readAloudStatus = it readAloudStatus = it
if (it == Status.STOP || it == Status.PAUSE) { if (it == Status.STOP || it == Status.PAUSE) {
viewModel.curTextChapter?.let { textChapter -> ReadBook.curTextChapter?.let { textChapter ->
val page = textChapter.page(viewModel.durPageIndex) val page = textChapter.page(ReadBook.durPageIndex)
if (page != null && page.text is SpannableStringBuilder) { if (page != null && page.text is SpannableStringBuilder) {
page.text.removeSpan(ChapterProvider.readAloudSpan) page.text.removeSpan(ChapterProvider.readAloudSpan)
page_view.upContent() page_view.upContent()
@ -568,9 +569,9 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} }
observeEvent<Int>(Bus.TTS_START) { chapterStart -> observeEvent<Int>(Bus.TTS_START) { chapterStart ->
launch(IO) { launch(IO) {
viewModel.curTextChapter?.let { ReadBook.curTextChapter?.let {
val pageStart = chapterStart - it.getReadLength(viewModel.durPageIndex) val pageStart = chapterStart - it.getReadLength(ReadBook.durPageIndex)
it.page(viewModel.durPageIndex)?.upPageAloudSpan(pageStart) it.page(ReadBook.durPageIndex)?.upPageAloudSpan(pageStart)
withContext(Main) { withContext(Main) {
page_view.upContent() page_view.upContent()
} }
@ -583,18 +584,18 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
if (page_view.isScrollDelegate) { if (page_view.isScrollDelegate) {
page_view.moveToNextPage() page_view.moveToNextPage()
} else { } else {
viewModel.durPageIndex = viewModel.durPageIndex + 1 ReadBook.durPageIndex = ReadBook.durPageIndex + 1
page_view.upContent() page_view.upContent()
viewModel.saveRead() viewModel.saveRead()
} }
} }
2 -> if (!moveToNextChapter(true)) ReadAloud.stop(this) 2 -> if (!moveToNextChapter(true)) ReadAloud.stop(this)
-1 -> { -1 -> {
if (viewModel.durPageIndex > 0) { if (ReadBook.durPageIndex > 0) {
if (page_view.isScrollDelegate) { if (page_view.isScrollDelegate) {
page_view.moveToPrevPage() page_view.moveToPrevPage()
} else { } else {
viewModel.durPageIndex = viewModel.durPageIndex - 1 ReadBook.durPageIndex = ReadBook.durPageIndex - 1
page_view.upContent() page_view.upContent()
viewModel.saveRead() viewModel.saveRead()
} }

@ -2,7 +2,6 @@ package io.legado.app.ui.book.read
import android.app.Application import android.app.Application
import android.content.Intent import android.content.Intent
import androidx.lifecycle.MutableLiveData
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
@ -13,7 +12,7 @@ import io.legado.app.help.BookHelp
import io.legado.app.help.IntentDataHelp import io.legado.app.help.IntentDataHelp
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.service.help.ReadAloud import io.legado.app.service.help.ReadAloud
import io.legado.app.ui.widget.page.TextChapter import io.legado.app.service.help.ReadBook
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
@ -21,23 +20,10 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class ReadBookViewModel(application: Application) : BaseViewModel(application) { class ReadBookViewModel(application: Application) : BaseViewModel(application) {
var titleDate = MutableLiveData<String>()
var book: Book? = null
var inBookshelf = false
var chapterSize = 0
var callBack: CallBack? = null
var durChapterIndex = 0
var durPageIndex = 0
var isLocalBook = true
var prevTextChapter: TextChapter? = null
var curTextChapter: TextChapter? = null
var nextTextChapter: TextChapter? = null
var webBook: WebBook? = null
private val loadingChapters = arrayListOf<Int>()
fun initData(intent: Intent) { fun initData(intent: Intent) {
execute { execute {
inBookshelf = intent.getBooleanExtra("inBookshelf", true) ReadBook.inBookshelf = intent.getBooleanExtra("inBookshelf", true)
IntentDataHelp.getData<Book>(intent.getStringExtra("key"))?.let { IntentDataHelp.getData<Book>(intent.getStringExtra("key"))?.let {
initBook(it) initBook(it)
} ?: intent.getStringExtra("bookUrl")?.let { } ?: intent.getStringExtra("bookUrl")?.let {
@ -51,13 +37,13 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
private fun initBook(book: Book) { private fun initBook(book: Book) {
this.book = book ReadBook.book = book
titleDate.postValue(book.name) ReadBook.titleDate.postValue(book.name)
durChapterIndex = book.durChapterIndex ReadBook.durChapterIndex = book.durChapterIndex
durPageIndex = book.durChapterPos ReadBook.durPageIndex = book.durChapterPos
isLocalBook = book.origin == BookType.local ReadBook.isLocalBook = book.origin == BookType.local
App.db.bookSourceDao().getBookSource(book.origin)?.let { App.db.bookSourceDao().getBookSource(book.origin)?.let {
webBook = WebBook(it) ReadBook.webBook = WebBook(it)
} }
val count = App.db.bookChapterDao().getChapterCount(book.bookUrl) val count = App.db.bookChapterDao().getChapterCount(book.bookUrl)
if (count == 0) { if (count == 0) {
@ -67,13 +53,13 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
loadChapterList(book) loadChapterList(book)
} }
} else { } else {
if (durChapterIndex > count - 1) { if (ReadBook.durChapterIndex > count - 1) {
durChapterIndex = count - 1 ReadBook.durChapterIndex = count - 1
} }
chapterSize = count ReadBook.chapterSize = count
callBack?.loadContent() ReadBook.callBack?.loadContent()
} }
if (inBookshelf) { if (ReadBook.inBookshelf) {
saveRead() saveRead()
} }
} }
@ -83,7 +69,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) { ) {
execute { execute {
webBook?.getBookInfo(book, this) ReadBook.webBook?.getBookInfo(book, this)
?.onSuccess { ?.onSuccess {
loadChapterList(book, changeDruChapterIndex) loadChapterList(book, changeDruChapterIndex)
} }
@ -95,13 +81,13 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) { ) {
execute { execute {
webBook?.getChapterList(book, this) ReadBook.webBook?.getChapterList(book, this)
?.onSuccess(IO) { cList -> ?.onSuccess(IO) { cList ->
if (!cList.isNullOrEmpty()) { if (!cList.isNullOrEmpty()) {
if (changeDruChapterIndex == null) { if (changeDruChapterIndex == null) {
App.db.bookChapterDao().insert(*cList.toTypedArray()) App.db.bookChapterDao().insert(*cList.toTypedArray())
chapterSize = cList.size ReadBook.chapterSize = cList.size
callBack?.loadContent() ReadBook.callBack?.loadContent()
} else { } else {
changeDruChapterIndex(cList) changeDruChapterIndex(cList)
} }
@ -115,49 +101,49 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
fun moveToNextChapter(upContent: Boolean) { fun moveToNextChapter(upContent: Boolean) {
durChapterIndex++ ReadBook.durChapterIndex++
prevTextChapter = curTextChapter ReadBook.prevTextChapter = ReadBook.curTextChapter
curTextChapter = nextTextChapter ReadBook.curTextChapter = ReadBook.nextTextChapter
nextTextChapter = null ReadBook.nextTextChapter = null
book?.let { ReadBook.book?.let {
if (curTextChapter == null) { if (ReadBook.curTextChapter == null) {
loadContent(durChapterIndex) loadContent(ReadBook.durChapterIndex)
} else if (upContent) { } else if (upContent) {
callBack?.upContent() ReadBook.callBack?.upContent()
} }
loadContent(durChapterIndex.plus(1)) loadContent(ReadBook.durChapterIndex.plus(1))
launch(IO) { launch(IO) {
for (i in 2..10) { for (i in 2..10) {
delay(100) delay(100)
download(durChapterIndex + i) download(ReadBook.durChapterIndex + i)
} }
} }
} }
} }
fun moveToPrevChapter(upContent: Boolean) { fun moveToPrevChapter(upContent: Boolean) {
durChapterIndex-- ReadBook.durChapterIndex--
nextTextChapter = curTextChapter ReadBook.nextTextChapter = ReadBook.curTextChapter
curTextChapter = prevTextChapter ReadBook.curTextChapter = ReadBook.prevTextChapter
prevTextChapter = null ReadBook.prevTextChapter = null
book?.let { ReadBook.book?.let {
if (curTextChapter == null) { if (ReadBook.curTextChapter == null) {
loadContent(durChapterIndex) loadContent(ReadBook.durChapterIndex)
} else if (upContent) { } else if (upContent) {
callBack?.upContent() ReadBook.callBack?.upContent()
} }
loadContent(durChapterIndex.minus(1)) loadContent(ReadBook.durChapterIndex.minus(1))
launch(IO) { launch(IO) {
for (i in -5..-2) { for (i in -5..-2) {
delay(100) delay(100)
download(durChapterIndex + i) download(ReadBook.durChapterIndex + i)
} }
} }
} }
} }
fun loadContent(index: Int) { fun loadContent(index: Int) {
book?.let { book -> ReadBook.book?.let { book ->
if (addLoading(index)) { if (addLoading(index)) {
execute { execute {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
@ -174,7 +160,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
private fun download(index: Int) { private fun download(index: Int) {
book?.let { book -> ReadBook.book?.let { book ->
if (addLoading(index)) { if (addLoading(index)) {
execute { execute {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
@ -192,8 +178,8 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
private fun download(chapter: BookChapter) { private fun download(chapter: BookChapter) {
book?.let { book -> ReadBook.book?.let { book ->
webBook?.getContent(book, chapter, scope = this) ReadBook.webBook?.getContent(book, chapter, scope = this)
?.onSuccess(IO) { content -> ?.onSuccess(IO) { content ->
if (content.isNullOrEmpty()) { if (content.isNullOrEmpty()) {
contentLoadFinish(chapter, context.getString(R.string.content_empty)) contentLoadFinish(chapter, context.getString(R.string.content_empty))
@ -212,48 +198,48 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
private fun addLoading(index: Int): Boolean { private fun addLoading(index: Int): Boolean {
synchronized(this) { synchronized(this) {
if (loadingChapters.contains(index)) return false if (ReadBook.loadingChapters.contains(index)) return false
loadingChapters.add(index) ReadBook.loadingChapters.add(index)
return true return true
} }
} }
private fun removeLoading(index: Int) { private fun removeLoading(index: Int) {
synchronized(this) { synchronized(this) {
loadingChapters.remove(index) ReadBook.loadingChapters.remove(index)
} }
} }
private fun contentLoadFinish(chapter: BookChapter, content: String) { private fun contentLoadFinish(chapter: BookChapter, content: String) {
execute { execute {
if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { if (chapter.index in ReadBook.durChapterIndex - 1..ReadBook.durChapterIndex + 1) {
val c = BookHelp.disposeContent( val c = BookHelp.disposeContent(
chapter.title, chapter.title,
book!!.name, ReadBook.book!!.name,
webBook?.bookSource?.bookSourceUrl, ReadBook.webBook?.bookSource?.bookSourceUrl,
content, content,
book!!.useReplaceRule ReadBook.book!!.useReplaceRule
) )
callBack?.contentLoadFinish(chapter, c) ReadBook.callBack?.contentLoadFinish(chapter, c)
} }
} }
} }
fun changeTo(book1: Book) { fun changeTo(book1: Book) {
execute { execute {
book?.let { ReadBook.book?.let {
App.db.bookDao().delete(it.bookUrl) App.db.bookDao().delete(it.bookUrl)
} }
prevTextChapter = null ReadBook.prevTextChapter = null
curTextChapter = null ReadBook.curTextChapter = null
nextTextChapter = null ReadBook.nextTextChapter = null
withContext(Main) { withContext(Main) {
callBack?.upContent() ReadBook.callBack?.upContent()
} }
App.db.bookDao().insert(book1) App.db.bookDao().insert(book1)
book = book1 ReadBook.book = book1
App.db.bookSourceDao().getBookSource(book1.origin)?.let { App.db.bookSourceDao().getBookSource(book1.origin)?.let {
webBook = WebBook(it) ReadBook.webBook = WebBook(it)
} }
if (book1.tocUrl.isEmpty()) { if (book1.tocUrl.isEmpty()) {
loadBookInfo(book1) { upChangeDurChapterIndex(book1, it) } loadBookInfo(book1) { upChangeDurChapterIndex(book1, it) }
@ -269,41 +255,41 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
private fun upChangeDurChapterIndex(book: Book, chapters: List<BookChapter>) { private fun upChangeDurChapterIndex(book: Book, chapters: List<BookChapter>) {
execute { execute {
durChapterIndex = BookHelp.getDurChapterIndexByChapterTitle( ReadBook.durChapterIndex = BookHelp.getDurChapterIndexByChapterTitle(
book.durChapterTitle, book.durChapterTitle,
book.durChapterIndex, book.durChapterIndex,
chapters chapters
) )
book.durChapterIndex = durChapterIndex book.durChapterIndex = ReadBook.durChapterIndex
book.durChapterTitle = chapters[durChapterIndex].title book.durChapterTitle = chapters[ReadBook.durChapterIndex].title
App.db.bookDao().update(book) App.db.bookDao().update(book)
App.db.bookChapterDao().insert(*chapters.toTypedArray()) App.db.bookChapterDao().insert(*chapters.toTypedArray())
chapterSize = chapters.size ReadBook.chapterSize = chapters.size
callBack?.loadContent() ReadBook.callBack?.loadContent()
} }
} }
fun openChapter(index: Int) { fun openChapter(index: Int) {
prevTextChapter = null ReadBook.prevTextChapter = null
curTextChapter = null ReadBook.curTextChapter = null
nextTextChapter = null ReadBook.nextTextChapter = null
callBack?.upContent() ReadBook.callBack?.upContent()
if (index != durChapterIndex) { if (index != ReadBook.durChapterIndex) {
durChapterIndex = index ReadBook.durChapterIndex = index
durPageIndex = 0 ReadBook.durPageIndex = 0
} }
saveRead() saveRead()
callBack?.loadContent() ReadBook.callBack?.loadContent()
} }
fun saveRead() { fun saveRead() {
execute { execute {
book?.let { book -> ReadBook.book?.let { book ->
book.lastCheckCount = 0 book.lastCheckCount = 0
book.durChapterTime = System.currentTimeMillis() book.durChapterTime = System.currentTimeMillis()
book.durChapterIndex = durChapterIndex book.durChapterIndex = ReadBook.durChapterIndex
book.durChapterPos = durPageIndex book.durChapterPos = ReadBook.durPageIndex
curTextChapter?.let { ReadBook.curTextChapter?.let {
book.durChapterTitle = it.title book.durChapterTitle = it.title
} }
App.db.bookDao().update(book) App.db.bookDao().update(book)
@ -313,7 +299,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
fun removeFromBookshelf(success: (() -> Unit)?) { fun removeFromBookshelf(success: (() -> Unit)?) {
execute { execute {
book?.let { ReadBook.book?.let {
App.db.bookDao().delete(it.bookUrl) App.db.bookDao().delete(it.bookUrl)
} }
}.onSuccess { }.onSuccess {
@ -323,9 +309,9 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
fun upBookSource() { fun upBookSource() {
execute { execute {
book?.let { book -> ReadBook.book?.let { book ->
App.db.bookSourceDao().getBookSource(book.origin)?.let { App.db.bookSourceDao().getBookSource(book.origin)?.let {
webBook = WebBook(it) ReadBook.webBook = WebBook(it)
} }
} }
} }
@ -333,9 +319,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
fun refreshContent(book: Book) { fun refreshContent(book: Book) {
execute { execute {
App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, ReadBook.durChapterIndex)
?.let { chapter ->
BookHelp.delContent(book, chapter) BookHelp.delContent(book, chapter)
loadContent(durChapterIndex) loadContent(ReadBook.durChapterIndex)
} }
} }
} }

Loading…
Cancel
Save