pull/49/head
kunfei 5 years ago
parent 8e857e2457
commit 92a2a28841
  1. 49
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -1,26 +1,26 @@
package io.legado.app.ui.book.read package io.legado.app.ui.book.read
import android.app.Application
import android.content.Intent import android.content.Intent
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
import io.legado.app.base.BaseViewModel
import io.legado.app.constant.BookType import io.legado.app.constant.BookType
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.IntentDataHelp import io.legado.app.help.IntentDataHelp
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.service.help.ReadAloud
import io.legado.app.ui.widget.page.TextChapter import io.legado.app.ui.widget.page.TextChapter
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.jetbrains.anko.toast
class ReadBookViewModel(application: Application) : BaseViewModel(application) { class ReadBookViewModel {
var titleDate = MutableLiveData<String>() var titleDate = MutableLiveData<String>()
var book: Book? = null var book: Book? = null
var inBookshelf = false var inBookshelf = false
@ -36,7 +36,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
private val loadingChapters = arrayListOf<Int>() private val loadingChapters = arrayListOf<Int>()
fun initData(intent: Intent) { fun initData(intent: Intent) {
execute { Coroutine.async {
inBookshelf = intent.getBooleanExtra("inBookshelf", true) inBookshelf = intent.getBooleanExtra("inBookshelf", true)
IntentDataHelp.getData<Book>(intent.getStringExtra("key"))?.let { IntentDataHelp.getData<Book>(intent.getStringExtra("key"))?.let {
initBook(it) initBook(it)
@ -82,7 +82,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
book: Book, book: Book,
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) { ) {
execute { Coroutine.async {
webBook?.getBookInfo(book, this) webBook?.getBookInfo(book, this)
?.onSuccess { ?.onSuccess {
loadChapterList(book, changeDruChapterIndex) loadChapterList(book, changeDruChapterIndex)
@ -94,7 +94,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
book: Book, book: Book,
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) { ) {
execute { Coroutine.async {
webBook?.getChapterList(book, this) webBook?.getChapterList(book, this)
?.onSuccess(IO) { cList -> ?.onSuccess(IO) { cList ->
if (!cList.isNullOrEmpty()) { if (!cList.isNullOrEmpty()) {
@ -106,10 +106,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
changeDruChapterIndex(cList) changeDruChapterIndex(cList)
} }
} else { } else {
toast(R.string.error_load_toc) App.INSTANCE.toast(R.string.error_load_toc)
} }
}?.onError { }?.onError {
toast(R.string.error_load_toc) App.INSTANCE.toast(R.string.error_load_toc)
} ?: autoChangeSource() } ?: autoChangeSource()
} }
} }
@ -126,7 +126,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
callBack?.upContent() callBack?.upContent()
} }
loadContent(durChapterIndex.plus(1)) loadContent(durChapterIndex.plus(1))
launch(IO) { GlobalScope.launch(IO) {
for (i in 2..10) { for (i in 2..10) {
delay(100) delay(100)
download(durChapterIndex + i) download(durChapterIndex + i)
@ -147,7 +147,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
callBack?.upContent() callBack?.upContent()
} }
loadContent(durChapterIndex.minus(1)) loadContent(durChapterIndex.minus(1))
launch(IO) { GlobalScope.launch(IO) {
for (i in -5..-2) { for (i in -5..-2) {
delay(100) delay(100)
download(durChapterIndex + i) download(durChapterIndex + i)
@ -159,7 +159,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
fun loadContent(index: Int) { fun loadContent(index: Int) {
book?.let { book -> book?.let { book ->
if (addLoading(index)) { if (addLoading(index)) {
execute { Coroutine.async {
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 {
contentLoadFinish(chapter, it) contentLoadFinish(chapter, it)
@ -176,7 +176,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
private fun download(index: Int) { private fun download(index: Int) {
book?.let { book -> book?.let { book ->
if (addLoading(index)) { if (addLoading(index)) {
execute { Coroutine.async {
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter ->
if (BookHelp.hasContent(book, chapter)) { if (BookHelp.hasContent(book, chapter)) {
removeLoading(chapter.index) removeLoading(chapter.index)
@ -193,10 +193,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
private fun download(chapter: BookChapter) { private fun download(chapter: BookChapter) {
book?.let { book -> book?.let { book ->
webBook?.getContent(book, chapter, scope = this) webBook?.getContent(book, chapter)
?.onSuccess(IO) { content -> ?.onSuccess(IO) { content ->
if (content.isNullOrEmpty()) { if (content.isNullOrEmpty()) {
contentLoadFinish(chapter, context.getString(R.string.content_empty)) contentLoadFinish(chapter, App.INSTANCE.getString(R.string.content_empty))
removeLoading(chapter.index) removeLoading(chapter.index)
} else { } else {
BookHelp.saveContent(book, chapter, content) BookHelp.saveContent(book, chapter, content)
@ -225,7 +225,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
private fun contentLoadFinish(chapter: BookChapter, content: String) { private fun contentLoadFinish(chapter: BookChapter, content: String) {
execute { Coroutine.async {
if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) {
val c = BookHelp.disposeContent( val c = BookHelp.disposeContent(
chapter.title, chapter.title,
@ -240,7 +240,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
fun changeTo(book1: Book) { fun changeTo(book1: Book) {
execute { Coroutine.async {
book?.let { book?.let {
App.db.bookDao().delete(it.bookUrl) App.db.bookDao().delete(it.bookUrl)
} }
@ -268,7 +268,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
private fun upChangeDurChapterIndex(book: Book, chapters: List<BookChapter>) { private fun upChangeDurChapterIndex(book: Book, chapters: List<BookChapter>) {
execute { Coroutine.async {
durChapterIndex = BookHelp.getDurChapterIndexByChapterTitle( durChapterIndex = BookHelp.getDurChapterIndexByChapterTitle(
book.durChapterTitle, book.durChapterTitle,
book.durChapterIndex, book.durChapterIndex,
@ -297,7 +297,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
fun saveRead() { fun saveRead() {
execute { Coroutine.async {
book?.let { book -> book?.let { book ->
book.lastCheckCount = 0 book.lastCheckCount = 0
book.durChapterTime = System.currentTimeMillis() book.durChapterTime = System.currentTimeMillis()
@ -312,7 +312,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
fun removeFromBookshelf(success: (() -> Unit)?) { fun removeFromBookshelf(success: (() -> Unit)?) {
execute { Coroutine.async {
book?.let { book?.let {
App.db.bookDao().delete(it.bookUrl) App.db.bookDao().delete(it.bookUrl)
} }
@ -322,7 +322,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
fun upBookSource() { fun upBookSource() {
execute { Coroutine.async {
book?.let { book -> book?.let { book ->
App.db.bookSourceDao().getBookSource(book.origin)?.let { App.db.bookSourceDao().getBookSource(book.origin)?.let {
webBook = WebBook(it) webBook = WebBook(it)
@ -332,7 +332,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
fun refreshContent(book: Book) { fun refreshContent(book: Book) {
execute { Coroutine.async {
App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter ->
BookHelp.delContent(book, chapter) BookHelp.delContent(book, chapter)
loadContent(durChapterIndex) loadContent(durChapterIndex)
@ -340,11 +340,6 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
} }
override fun onCleared() {
super.onCleared()
ReadAloud.stop(context)
}
interface CallBack { interface CallBack {
fun loadContent() fun loadContent()
fun contentLoadFinish(bookChapter: BookChapter, content: String) fun contentLoadFinish(bookChapter: BookChapter, content: String)

Loading…
Cancel
Save