|
|
@ -21,8 +21,9 @@ 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 inBookshelf = false |
|
|
|
var bookData = MutableLiveData<Book>() |
|
|
|
|
|
|
|
var chapterSize = 0 |
|
|
|
var chapterSize = 0 |
|
|
|
var callBack: CallBack? = null |
|
|
|
var callBack: CallBack? = null |
|
|
|
var durChapterIndex = 0 |
|
|
|
var durChapterIndex = 0 |
|
|
@ -38,22 +39,23 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
execute { |
|
|
|
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 { |
|
|
|
setBook(it) |
|
|
|
initBook(it) |
|
|
|
} ?: intent.getStringExtra("bookUrl")?.let { |
|
|
|
} ?: intent.getStringExtra("bookUrl")?.let { |
|
|
|
App.db.bookDao().getBook(it)?.let { book -> |
|
|
|
App.db.bookDao().getBook(it)?.let { book -> |
|
|
|
setBook(book) |
|
|
|
initBook(book) |
|
|
|
} |
|
|
|
} |
|
|
|
} ?: App.db.bookDao().lastReadBook?.let { |
|
|
|
} ?: 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 |
|
|
|
durChapterIndex = book.durChapterIndex |
|
|
|
durPageIndex = book.durChapterPos |
|
|
|
durPageIndex = book.durChapterPos |
|
|
|
isLocalBook = book.origin == BookType.local |
|
|
|
isLocalBook = book.origin == BookType.local |
|
|
|
bookData.postValue(book) |
|
|
|
|
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { |
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { |
|
|
|
webBook = WebBook(it) |
|
|
|
webBook = WebBook(it) |
|
|
|
} |
|
|
|
} |
|
|
@ -72,7 +74,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
callBack?.loadContent() |
|
|
|
callBack?.loadContent() |
|
|
|
} |
|
|
|
} |
|
|
|
if (inBookshelf) { |
|
|
|
if (inBookshelf) { |
|
|
|
saveRead(book) |
|
|
|
saveRead() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -117,19 +119,17 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
prevTextChapter = curTextChapter |
|
|
|
prevTextChapter = curTextChapter |
|
|
|
curTextChapter = nextTextChapter |
|
|
|
curTextChapter = nextTextChapter |
|
|
|
nextTextChapter = null |
|
|
|
nextTextChapter = null |
|
|
|
bookData.value?.let { |
|
|
|
book?.let { |
|
|
|
if (curTextChapter == null) { |
|
|
|
if (curTextChapter == null) { |
|
|
|
loadContent(it, durChapterIndex) |
|
|
|
loadContent(durChapterIndex) |
|
|
|
} else if (upContent) { |
|
|
|
} else if (upContent) { |
|
|
|
callBack?.upContent() |
|
|
|
callBack?.upContent() |
|
|
|
} |
|
|
|
} |
|
|
|
loadContent(it, durChapterIndex.plus(1)) |
|
|
|
loadContent(durChapterIndex.plus(1)) |
|
|
|
launch(IO) { |
|
|
|
launch(IO) { |
|
|
|
for (i in 2..10) { |
|
|
|
for (i in 2..10) { |
|
|
|
delay(100) |
|
|
|
delay(100) |
|
|
|
bookData.value?.let { book -> |
|
|
|
download(durChapterIndex + i) |
|
|
|
download(book, durChapterIndex + i) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -140,47 +140,48 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
nextTextChapter = curTextChapter |
|
|
|
nextTextChapter = curTextChapter |
|
|
|
curTextChapter = prevTextChapter |
|
|
|
curTextChapter = prevTextChapter |
|
|
|
prevTextChapter = null |
|
|
|
prevTextChapter = null |
|
|
|
bookData.value?.let { |
|
|
|
book?.let { |
|
|
|
if (curTextChapter == null) { |
|
|
|
if (curTextChapter == null) { |
|
|
|
loadContent(it, durChapterIndex) |
|
|
|
loadContent(durChapterIndex) |
|
|
|
} else if (upContent) { |
|
|
|
} else if (upContent) { |
|
|
|
callBack?.upContent() |
|
|
|
callBack?.upContent() |
|
|
|
} |
|
|
|
} |
|
|
|
loadContent(it, durChapterIndex.minus(1)) |
|
|
|
loadContent(durChapterIndex.minus(1)) |
|
|
|
launch(IO) { |
|
|
|
launch(IO) { |
|
|
|
for (i in -5..-2) { |
|
|
|
for (i in -5..-2) { |
|
|
|
delay(100) |
|
|
|
delay(100) |
|
|
|
bookData.value?.let { book -> |
|
|
|
download(durChapterIndex + i) |
|
|
|
download(book, durChapterIndex + i) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun loadContent(book: Book, index: Int) { |
|
|
|
fun loadContent(index: Int) { |
|
|
|
|
|
|
|
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 -> |
|
|
|
BookHelp.getContent(book, chapter)?.let { |
|
|
|
BookHelp.getContent(book, chapter)?.let { |
|
|
|
contentLoadFinish(chapter, it) |
|
|
|
contentLoadFinish(chapter, it) |
|
|
|
removeLoading(chapter.index) |
|
|
|
removeLoading(chapter.index) |
|
|
|
} ?: download(book, chapter) |
|
|
|
} ?: download(chapter) |
|
|
|
} ?: removeLoading(index) |
|
|
|
} ?: removeLoading(index) |
|
|
|
}.onError { |
|
|
|
}.onError { |
|
|
|
removeLoading(index) |
|
|
|
removeLoading(index) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun download(book: Book, index: Int) { |
|
|
|
private fun download(index: Int) { |
|
|
|
|
|
|
|
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 -> |
|
|
|
if (BookHelp.hasContent(book, chapter)) { |
|
|
|
if (BookHelp.hasContent(book, chapter)) { |
|
|
|
removeLoading(chapter.index) |
|
|
|
removeLoading(chapter.index) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
download(book, chapter) |
|
|
|
download(chapter) |
|
|
|
} |
|
|
|
} |
|
|
|
} ?: removeLoading(index) |
|
|
|
} ?: removeLoading(index) |
|
|
|
}.onError { |
|
|
|
}.onError { |
|
|
@ -188,8 +189,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun download(book: Book, chapter: BookChapter) { |
|
|
|
private fun download(chapter: BookChapter) { |
|
|
|
|
|
|
|
book?.let { book -> |
|
|
|
webBook?.getContent(book, chapter, scope = this) |
|
|
|
webBook?.getContent(book, chapter, scope = this) |
|
|
|
?.onSuccess(IO) { content -> |
|
|
|
?.onSuccess(IO) { content -> |
|
|
|
if (content.isNullOrEmpty()) { |
|
|
|
if (content.isNullOrEmpty()) { |
|
|
@ -205,6 +208,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
removeLoading(chapter.index) |
|
|
|
removeLoading(chapter.index) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun addLoading(index: Int): Boolean { |
|
|
|
private fun addLoading(index: Int): Boolean { |
|
|
|
synchronized(this) { |
|
|
|
synchronized(this) { |
|
|
@ -225,19 +229,19 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
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, |
|
|
|
bookData.value!!.name, |
|
|
|
book!!.name, |
|
|
|
webBook?.bookSource?.bookSourceUrl, |
|
|
|
webBook?.bookSource?.bookSourceUrl, |
|
|
|
content, |
|
|
|
content, |
|
|
|
bookData.value!!.useReplaceRule |
|
|
|
book!!.useReplaceRule |
|
|
|
) |
|
|
|
) |
|
|
|
callBack?.contentLoadFinish(chapter, c) |
|
|
|
callBack?.contentLoadFinish(chapter, c) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun changeTo(book: Book) { |
|
|
|
fun changeTo(book1: Book) { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
bookData.value?.let { |
|
|
|
book?.let { |
|
|
|
App.db.bookDao().delete(it.bookUrl) |
|
|
|
App.db.bookDao().delete(it.bookUrl) |
|
|
|
} |
|
|
|
} |
|
|
|
prevTextChapter = null |
|
|
|
prevTextChapter = null |
|
|
@ -246,15 +250,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
withContext(Main) { |
|
|
|
withContext(Main) { |
|
|
|
callBack?.upContent() |
|
|
|
callBack?.upContent() |
|
|
|
} |
|
|
|
} |
|
|
|
App.db.bookDao().insert(book) |
|
|
|
App.db.bookDao().insert(book1) |
|
|
|
bookData.postValue(book) |
|
|
|
book = book1 |
|
|
|
App.db.bookSourceDao().getBookSource(book.origin)?.let { |
|
|
|
App.db.bookSourceDao().getBookSource(book1.origin)?.let { |
|
|
|
webBook = WebBook(it) |
|
|
|
webBook = WebBook(it) |
|
|
|
} |
|
|
|
} |
|
|
|
if (book.tocUrl.isEmpty()) { |
|
|
|
if (book1.tocUrl.isEmpty()) { |
|
|
|
loadBookInfo(book) { upChangeDurChapterIndex(book, it) } |
|
|
|
loadBookInfo(book1) { upChangeDurChapterIndex(book1, it) } |
|
|
|
} else { |
|
|
|
} 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 |
|
|
|
prevTextChapter = null |
|
|
|
curTextChapter = null |
|
|
|
curTextChapter = null |
|
|
|
nextTextChapter = null |
|
|
|
nextTextChapter = null |
|
|
|
if (chapter.index != durChapterIndex) { |
|
|
|
if (index != durChapterIndex) { |
|
|
|
durChapterIndex = chapter.index |
|
|
|
durChapterIndex = index |
|
|
|
durPageIndex = 0 |
|
|
|
durPageIndex = 0 |
|
|
|
} |
|
|
|
} |
|
|
|
saveRead() |
|
|
|
saveRead() |
|
|
|
callBack?.loadContent() |
|
|
|
callBack?.loadContent() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun saveRead(book: Book? = bookData.value) { |
|
|
|
fun saveRead() { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
book?.let { book -> |
|
|
|
book?.let { book -> |
|
|
|
book.lastCheckCount = 0 |
|
|
|
book.lastCheckCount = 0 |
|
|
@ -308,7 +312,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
|
|
|
|
|
|
|
fun removeFromBookshelf(success: (() -> Unit)?) { |
|
|
|
fun removeFromBookshelf(success: (() -> Unit)?) { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
bookData.value?.let { |
|
|
|
book?.let { |
|
|
|
App.db.bookDao().delete(it.bookUrl) |
|
|
|
App.db.bookDao().delete(it.bookUrl) |
|
|
|
} |
|
|
|
} |
|
|
|
}.onSuccess { |
|
|
|
}.onSuccess { |
|
|
@ -318,7 +322,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
|
|
|
|
|
|
|
|
fun upBookSource() { |
|
|
|
fun upBookSource() { |
|
|
|
execute { |
|
|
|
execute { |
|
|
|
bookData.value?.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) |
|
|
|
} |
|
|
|
} |
|
|
@ -330,7 +334,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
execute { |
|
|
|
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(book, durChapterIndex) |
|
|
|
loadContent(durChapterIndex) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|