|
|
@ -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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
object ReadBookViewModel { |
|
|
|
class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
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 @@ object ReadBookViewModel { |
|
|
|
private val loadingChapters = arrayListOf<Int>() |
|
|
|
private val loadingChapters = arrayListOf<Int>() |
|
|
|
|
|
|
|
|
|
|
|
fun initData(intent: Intent) { |
|
|
|
fun initData(intent: Intent) { |
|
|
|
Coroutine.async { |
|
|
|
execute { |
|
|
|
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 @@ object ReadBookViewModel { |
|
|
|
book: Book, |
|
|
|
book: Book, |
|
|
|
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null |
|
|
|
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null |
|
|
|
) { |
|
|
|
) { |
|
|
|
Coroutine.async { |
|
|
|
execute { |
|
|
|
webBook?.getBookInfo(book, this) |
|
|
|
webBook?.getBookInfo(book, this) |
|
|
|
?.onSuccess { |
|
|
|
?.onSuccess { |
|
|
|
loadChapterList(book, changeDruChapterIndex) |
|
|
|
loadChapterList(book, changeDruChapterIndex) |
|
|
@ -94,7 +94,7 @@ object ReadBookViewModel { |
|
|
|
book: Book, |
|
|
|
book: Book, |
|
|
|
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null |
|
|
|
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null |
|
|
|
) { |
|
|
|
) { |
|
|
|
Coroutine.async { |
|
|
|
execute { |
|
|
|
webBook?.getChapterList(book, this) |
|
|
|
webBook?.getChapterList(book, this) |
|
|
|
?.onSuccess(IO) { cList -> |
|
|
|
?.onSuccess(IO) { cList -> |
|
|
|
if (!cList.isNullOrEmpty()) { |
|
|
|
if (!cList.isNullOrEmpty()) { |
|
|
@ -106,33 +106,30 @@ object ReadBookViewModel { |
|
|
|
changeDruChapterIndex(cList) |
|
|
|
changeDruChapterIndex(cList) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
App.INSTANCE.toast(R.string.error_load_toc) |
|
|
|
toast(R.string.error_load_toc) |
|
|
|
} |
|
|
|
} |
|
|
|
}?.onError { |
|
|
|
}?.onError { |
|
|
|
App.INSTANCE.toast(R.string.error_load_toc) |
|
|
|
toast(R.string.error_load_toc) |
|
|
|
} ?: autoChangeSource() |
|
|
|
} ?: autoChangeSource() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun moveToNextChapter(upContent: Boolean) { |
|
|
|
fun moveToNextChapter(upContent: Boolean) { |
|
|
|
if (durChapterIndex < chapterSize - 1) { |
|
|
|
durChapterIndex++ |
|
|
|
durPageIndex = 0 |
|
|
|
prevTextChapter = curTextChapter |
|
|
|
durChapterIndex++ |
|
|
|
curTextChapter = nextTextChapter |
|
|
|
prevTextChapter = curTextChapter |
|
|
|
nextTextChapter = null |
|
|
|
curTextChapter = nextTextChapter |
|
|
|
book?.let { |
|
|
|
nextTextChapter = null |
|
|
|
if (curTextChapter == null) { |
|
|
|
book?.let { |
|
|
|
loadContent(durChapterIndex) |
|
|
|
if (curTextChapter == null) { |
|
|
|
} else if (upContent) { |
|
|
|
loadContent(durChapterIndex) |
|
|
|
callBack?.upContent() |
|
|
|
} else if (upContent) { |
|
|
|
} |
|
|
|
callBack?.upContent() |
|
|
|
loadContent(durChapterIndex.plus(1)) |
|
|
|
} |
|
|
|
launch(IO) { |
|
|
|
loadContent(durChapterIndex.plus(1)) |
|
|
|
for (i in 2..10) { |
|
|
|
GlobalScope.launch(IO) { |
|
|
|
delay(100) |
|
|
|
for (i in 2..10) { |
|
|
|
download(durChapterIndex + i) |
|
|
|
delay(100) |
|
|
|
|
|
|
|
download(durChapterIndex + i) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -150,7 +147,7 @@ object ReadBookViewModel { |
|
|
|
callBack?.upContent() |
|
|
|
callBack?.upContent() |
|
|
|
} |
|
|
|
} |
|
|
|
loadContent(durChapterIndex.minus(1)) |
|
|
|
loadContent(durChapterIndex.minus(1)) |
|
|
|
GlobalScope.launch(IO) { |
|
|
|
launch(IO) { |
|
|
|
for (i in -5..-2) { |
|
|
|
for (i in -5..-2) { |
|
|
|
delay(100) |
|
|
|
delay(100) |
|
|
|
download(durChapterIndex + i) |
|
|
|
download(durChapterIndex + i) |
|
|
@ -162,7 +159,7 @@ object ReadBookViewModel { |
|
|
|
fun loadContent(index: Int) { |
|
|
|
fun loadContent(index: Int) { |
|
|
|
book?.let { book -> |
|
|
|
book?.let { book -> |
|
|
|
if (addLoading(index)) { |
|
|
|
if (addLoading(index)) { |
|
|
|
Coroutine.async { |
|
|
|
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 { |
|
|
|
contentLoadFinish(chapter, it) |
|
|
|
contentLoadFinish(chapter, it) |
|
|
@ -179,7 +176,7 @@ object ReadBookViewModel { |
|
|
|
private fun download(index: Int) { |
|
|
|
private fun download(index: Int) { |
|
|
|
book?.let { book -> |
|
|
|
book?.let { book -> |
|
|
|
if (addLoading(index)) { |
|
|
|
if (addLoading(index)) { |
|
|
|
Coroutine.async { |
|
|
|
execute { |
|
|
|
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) |
|
|
@ -196,10 +193,10 @@ object ReadBookViewModel { |
|
|
|
|
|
|
|
|
|
|
|
private fun download(chapter: BookChapter) { |
|
|
|
private fun download(chapter: BookChapter) { |
|
|
|
book?.let { book -> |
|
|
|
book?.let { book -> |
|
|
|
webBook?.getContent(book, chapter) |
|
|
|
webBook?.getContent(book, chapter, scope = this) |
|
|
|
?.onSuccess(IO) { content -> |
|
|
|
?.onSuccess(IO) { content -> |
|
|
|
if (content.isNullOrEmpty()) { |
|
|
|
if (content.isNullOrEmpty()) { |
|
|
|
contentLoadFinish(chapter, App.INSTANCE.getString(R.string.content_empty)) |
|
|
|
contentLoadFinish(chapter, context.getString(R.string.content_empty)) |
|
|
|
removeLoading(chapter.index) |
|
|
|
removeLoading(chapter.index) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
BookHelp.saveContent(book, chapter, content) |
|
|
|
BookHelp.saveContent(book, chapter, content) |
|
|
@ -228,7 +225,7 @@ object ReadBookViewModel { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun contentLoadFinish(chapter: BookChapter, content: String) { |
|
|
|
private fun contentLoadFinish(chapter: BookChapter, content: String) { |
|
|
|
Coroutine.async { |
|
|
|
execute { |
|
|
|
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, |
|
|
@ -243,7 +240,7 @@ object ReadBookViewModel { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun changeTo(book1: Book) { |
|
|
|
fun changeTo(book1: Book) { |
|
|
|
Coroutine.async { |
|
|
|
execute { |
|
|
|
book?.let { |
|
|
|
book?.let { |
|
|
|
App.db.bookDao().delete(it.bookUrl) |
|
|
|
App.db.bookDao().delete(it.bookUrl) |
|
|
|
} |
|
|
|
} |
|
|
@ -271,7 +268,7 @@ object ReadBookViewModel { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun upChangeDurChapterIndex(book: Book, chapters: List<BookChapter>) { |
|
|
|
private fun upChangeDurChapterIndex(book: Book, chapters: List<BookChapter>) { |
|
|
|
Coroutine.async { |
|
|
|
execute { |
|
|
|
durChapterIndex = BookHelp.getDurChapterIndexByChapterTitle( |
|
|
|
durChapterIndex = BookHelp.getDurChapterIndexByChapterTitle( |
|
|
|
book.durChapterTitle, |
|
|
|
book.durChapterTitle, |
|
|
|
book.durChapterIndex, |
|
|
|
book.durChapterIndex, |
|
|
@ -300,7 +297,7 @@ object ReadBookViewModel { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun saveRead() { |
|
|
|
fun saveRead() { |
|
|
|
Coroutine.async { |
|
|
|
execute { |
|
|
|
book?.let { book -> |
|
|
|
book?.let { book -> |
|
|
|
book.lastCheckCount = 0 |
|
|
|
book.lastCheckCount = 0 |
|
|
|
book.durChapterTime = System.currentTimeMillis() |
|
|
|
book.durChapterTime = System.currentTimeMillis() |
|
|
@ -315,7 +312,7 @@ object ReadBookViewModel { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun removeFromBookshelf(success: (() -> Unit)?) { |
|
|
|
fun removeFromBookshelf(success: (() -> Unit)?) { |
|
|
|
Coroutine.async { |
|
|
|
execute { |
|
|
|
book?.let { |
|
|
|
book?.let { |
|
|
|
App.db.bookDao().delete(it.bookUrl) |
|
|
|
App.db.bookDao().delete(it.bookUrl) |
|
|
|
} |
|
|
|
} |
|
|
@ -325,7 +322,7 @@ object ReadBookViewModel { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun upBookSource() { |
|
|
|
fun upBookSource() { |
|
|
|
Coroutine.async { |
|
|
|
execute { |
|
|
|
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) |
|
|
@ -335,7 +332,7 @@ object ReadBookViewModel { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun refreshContent(book: Book) { |
|
|
|
fun refreshContent(book: Book) { |
|
|
|
Coroutine.async { |
|
|
|
execute { |
|
|
|
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) |
|
|
@ -343,6 +340,11 @@ object ReadBookViewModel { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|