|
|
|
@ -21,8 +21,9 @@ import kotlinx.coroutines.launch |
|
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
|
|
|
|
|
|
class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
var titleDate = MutableLiveData<String>() |
|
|
|
|
var book: Book? = null |
|
|
|
|
var inBookshelf = false |
|
|
|
|
var bookData = MutableLiveData<Book>() |
|
|
|
|
var chapterSize = 0 |
|
|
|
|
var callBack: CallBack? = null |
|
|
|
|
var durChapterIndex = 0 |
|
|
|
@ -38,22 +39,23 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
execute { |
|
|
|
|
inBookshelf = intent.getBooleanExtra("inBookshelf", true) |
|
|
|
|
IntentDataHelp.getData<Book>(intent.getStringExtra("key"))?.let { |
|
|
|
|
setBook(it) |
|
|
|
|
initBook(it) |
|
|
|
|
} ?: intent.getStringExtra("bookUrl")?.let { |
|
|
|
|
App.db.bookDao().getBook(it)?.let { book -> |
|
|
|
|
setBook(book) |
|
|
|
|
initBook(book) |
|
|
|
|
} |
|
|
|
|
} ?: App.db.bookDao().lastReadBook?.let { |
|
|
|
|
setBook(it) |
|
|
|
|
initBook(it) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun setBook(book: Book) { |
|
|
|
|
private fun initBook(book: Book) { |
|
|
|
|
this.book = book |
|
|
|
|
titleDate.postValue(book.name) |
|
|
|
|
durChapterIndex = book.durChapterIndex |
|
|
|
|
durPageIndex = book.durChapterPos |
|
|
|
|
isLocalBook = book.origin == BookType.local |
|
|
|
|
bookData.postValue(book) |
|
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { |
|
|
|
|
webBook = WebBook(it) |
|
|
|
|
} |
|
|
|
@ -72,7 +74,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
callBack?.loadContent() |
|
|
|
|
} |
|
|
|
|
if (inBookshelf) { |
|
|
|
|
saveRead(book) |
|
|
|
|
saveRead() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -117,19 +119,17 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
prevTextChapter = curTextChapter |
|
|
|
|
curTextChapter = nextTextChapter |
|
|
|
|
nextTextChapter = null |
|
|
|
|
bookData.value?.let { |
|
|
|
|
book?.let { |
|
|
|
|
if (curTextChapter == null) { |
|
|
|
|
loadContent(it, durChapterIndex) |
|
|
|
|
loadContent(durChapterIndex) |
|
|
|
|
} else if (upContent) { |
|
|
|
|
callBack?.upContent() |
|
|
|
|
} |
|
|
|
|
loadContent(it, durChapterIndex.plus(1)) |
|
|
|
|
loadContent(durChapterIndex.plus(1)) |
|
|
|
|
launch(IO) { |
|
|
|
|
for (i in 2..10) { |
|
|
|
|
delay(100) |
|
|
|
|
bookData.value?.let { book -> |
|
|
|
|
download(book, durChapterIndex + i) |
|
|
|
|
} |
|
|
|
|
download(durChapterIndex + i) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -140,70 +140,74 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
nextTextChapter = curTextChapter |
|
|
|
|
curTextChapter = prevTextChapter |
|
|
|
|
prevTextChapter = null |
|
|
|
|
bookData.value?.let { |
|
|
|
|
book?.let { |
|
|
|
|
if (curTextChapter == null) { |
|
|
|
|
loadContent(it, durChapterIndex) |
|
|
|
|
loadContent(durChapterIndex) |
|
|
|
|
} else if (upContent) { |
|
|
|
|
callBack?.upContent() |
|
|
|
|
} |
|
|
|
|
loadContent(it, durChapterIndex.minus(1)) |
|
|
|
|
loadContent(durChapterIndex.minus(1)) |
|
|
|
|
launch(IO) { |
|
|
|
|
for (i in -5..-2) { |
|
|
|
|
delay(100) |
|
|
|
|
bookData.value?.let { book -> |
|
|
|
|
download(book, durChapterIndex + i) |
|
|
|
|
} |
|
|
|
|
download(durChapterIndex + i) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun loadContent(book: Book, index: Int) { |
|
|
|
|
if (addLoading(index)) { |
|
|
|
|
execute { |
|
|
|
|
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> |
|
|
|
|
BookHelp.getContent(book, chapter)?.let { |
|
|
|
|
contentLoadFinish(chapter, it) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} ?: download(book, chapter) |
|
|
|
|
} ?: removeLoading(index) |
|
|
|
|
}.onError { |
|
|
|
|
removeLoading(index) |
|
|
|
|
fun loadContent(index: Int) { |
|
|
|
|
book?.let { book -> |
|
|
|
|
if (addLoading(index)) { |
|
|
|
|
execute { |
|
|
|
|
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> |
|
|
|
|
BookHelp.getContent(book, chapter)?.let { |
|
|
|
|
contentLoadFinish(chapter, it) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} ?: download(chapter) |
|
|
|
|
} ?: removeLoading(index) |
|
|
|
|
}.onError { |
|
|
|
|
removeLoading(index) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun download(book: Book, index: Int) { |
|
|
|
|
if (addLoading(index)) { |
|
|
|
|
execute { |
|
|
|
|
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> |
|
|
|
|
if (BookHelp.hasContent(book, chapter)) { |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} else { |
|
|
|
|
download(book, chapter) |
|
|
|
|
} |
|
|
|
|
} ?: removeLoading(index) |
|
|
|
|
}.onError { |
|
|
|
|
removeLoading(index) |
|
|
|
|
private fun download(index: Int) { |
|
|
|
|
book?.let { book -> |
|
|
|
|
if (addLoading(index)) { |
|
|
|
|
execute { |
|
|
|
|
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> |
|
|
|
|
if (BookHelp.hasContent(book, chapter)) { |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} else { |
|
|
|
|
download(chapter) |
|
|
|
|
} |
|
|
|
|
} ?: removeLoading(index) |
|
|
|
|
}.onError { |
|
|
|
|
removeLoading(index) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun download(book: Book, chapter: BookChapter) { |
|
|
|
|
webBook?.getContent(book, chapter, scope = this) |
|
|
|
|
?.onSuccess(IO) { content -> |
|
|
|
|
if (content.isNullOrEmpty()) { |
|
|
|
|
contentLoadFinish(chapter, context.getString(R.string.content_empty)) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} else { |
|
|
|
|
BookHelp.saveContent(book, chapter, content) |
|
|
|
|
contentLoadFinish(chapter, content) |
|
|
|
|
private fun download(chapter: BookChapter) { |
|
|
|
|
book?.let { book -> |
|
|
|
|
webBook?.getContent(book, chapter, scope = this) |
|
|
|
|
?.onSuccess(IO) { content -> |
|
|
|
|
if (content.isNullOrEmpty()) { |
|
|
|
|
contentLoadFinish(chapter, context.getString(R.string.content_empty)) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} else { |
|
|
|
|
BookHelp.saveContent(book, chapter, content) |
|
|
|
|
contentLoadFinish(chapter, content) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} |
|
|
|
|
}?.onError { |
|
|
|
|
contentLoadFinish(chapter, it.localizedMessage) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} |
|
|
|
|
}?.onError { |
|
|
|
|
contentLoadFinish(chapter, it.localizedMessage) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun addLoading(index: Int): Boolean { |
|
|
|
@ -225,19 +229,19 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { |
|
|
|
|
val c = BookHelp.disposeContent( |
|
|
|
|
chapter.title, |
|
|
|
|
bookData.value!!.name, |
|
|
|
|
book!!.name, |
|
|
|
|
webBook?.bookSource?.bookSourceUrl, |
|
|
|
|
content, |
|
|
|
|
bookData.value!!.useReplaceRule |
|
|
|
|
book!!.useReplaceRule |
|
|
|
|
) |
|
|
|
|
callBack?.contentLoadFinish(chapter, c) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun changeTo(book: Book) { |
|
|
|
|
fun changeTo(book1: Book) { |
|
|
|
|
execute { |
|
|
|
|
bookData.value?.let { |
|
|
|
|
book?.let { |
|
|
|
|
App.db.bookDao().delete(it.bookUrl) |
|
|
|
|
} |
|
|
|
|
prevTextChapter = null |
|
|
|
@ -246,15 +250,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
withContext(Main) { |
|
|
|
|
callBack?.upContent() |
|
|
|
|
} |
|
|
|
|
App.db.bookDao().insert(book) |
|
|
|
|
bookData.postValue(book) |
|
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { |
|
|
|
|
App.db.bookDao().insert(book1) |
|
|
|
|
book = book1 |
|
|
|
|
App.db.bookSourceDao().getBookSource(book1.origin)?.let { |
|
|
|
|
webBook = WebBook(it) |
|
|
|
|
} |
|
|
|
|
if (book.tocUrl.isEmpty()) { |
|
|
|
|
loadBookInfo(book) { upChangeDurChapterIndex(book, it) } |
|
|
|
|
if (book1.tocUrl.isEmpty()) { |
|
|
|
|
loadBookInfo(book1) { upChangeDurChapterIndex(book1, it) } |
|
|
|
|
} else { |
|
|
|
|
loadChapterList(book) { upChangeDurChapterIndex(book, it) } |
|
|
|
|
loadChapterList(book1) { upChangeDurChapterIndex(book1, it) } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -279,19 +283,19 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun openChapter(chapter: BookChapter) { |
|
|
|
|
fun openChapter(index: Int) { |
|
|
|
|
prevTextChapter = null |
|
|
|
|
curTextChapter = null |
|
|
|
|
nextTextChapter = null |
|
|
|
|
if (chapter.index != durChapterIndex) { |
|
|
|
|
durChapterIndex = chapter.index |
|
|
|
|
if (index != durChapterIndex) { |
|
|
|
|
durChapterIndex = index |
|
|
|
|
durPageIndex = 0 |
|
|
|
|
} |
|
|
|
|
saveRead() |
|
|
|
|
callBack?.loadContent() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun saveRead(book: Book? = bookData.value) { |
|
|
|
|
fun saveRead() { |
|
|
|
|
execute { |
|
|
|
|
book?.let { book -> |
|
|
|
|
book.lastCheckCount = 0 |
|
|
|
@ -308,7 +312,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
|
|
|
|
|
fun removeFromBookshelf(success: (() -> Unit)?) { |
|
|
|
|
execute { |
|
|
|
|
bookData.value?.let { |
|
|
|
|
book?.let { |
|
|
|
|
App.db.bookDao().delete(it.bookUrl) |
|
|
|
|
} |
|
|
|
|
}.onSuccess { |
|
|
|
@ -318,7 +322,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
|
|
|
|
|
fun upBookSource() { |
|
|
|
|
execute { |
|
|
|
|
bookData.value?.let { book -> |
|
|
|
|
book?.let { book -> |
|
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { |
|
|
|
|
webBook = WebBook(it) |
|
|
|
|
} |
|
|
|
@ -330,7 +334,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
execute { |
|
|
|
|
App.db.bookChapterDao().getChapter(book.bookUrl, durChapterIndex)?.let { chapter -> |
|
|
|
|
BookHelp.delContent(book, chapter) |
|
|
|
|
loadContent(book, durChapterIndex) |
|
|
|
|
loadContent(durChapterIndex) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|