|
|
@ -121,17 +121,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
nextTextChapter = null |
|
|
|
nextTextChapter = null |
|
|
|
book?.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) |
|
|
|
book?.let { book -> |
|
|
|
download(durChapterIndex + i) |
|
|
|
download(book, durChapterIndex + i) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -144,68 +142,72 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { |
|
|
|
prevTextChapter = null |
|
|
|
prevTextChapter = null |
|
|
|
book?.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) |
|
|
|
book?.let { book -> |
|
|
|
download(durChapterIndex + i) |
|
|
|
download(book, durChapterIndex + i) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun loadContent(book: Book, index: Int) { |
|
|
|
fun loadContent(index: Int) { |
|
|
|
if (addLoading(index)) { |
|
|
|
book?.let { book -> |
|
|
|
execute { |
|
|
|
if (addLoading(index)) { |
|
|
|
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> |
|
|
|
execute { |
|
|
|
BookHelp.getContent(book, chapter)?.let { |
|
|
|
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> |
|
|
|
contentLoadFinish(chapter, it) |
|
|
|
BookHelp.getContent(book, chapter)?.let { |
|
|
|
removeLoading(chapter.index) |
|
|
|
contentLoadFinish(chapter, it) |
|
|
|
} ?: download(book, chapter) |
|
|
|
removeLoading(chapter.index) |
|
|
|
} ?: removeLoading(index) |
|
|
|
} ?: download(chapter) |
|
|
|
}.onError { |
|
|
|
} ?: removeLoading(index) |
|
|
|
removeLoading(index) |
|
|
|
}.onError { |
|
|
|
|
|
|
|
removeLoading(index) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun download(book: Book, index: Int) { |
|
|
|
private fun download(index: Int) { |
|
|
|
if (addLoading(index)) { |
|
|
|
book?.let { book -> |
|
|
|
execute { |
|
|
|
if (addLoading(index)) { |
|
|
|
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> |
|
|
|
execute { |
|
|
|
if (BookHelp.hasContent(book, chapter)) { |
|
|
|
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> |
|
|
|
removeLoading(chapter.index) |
|
|
|
if (BookHelp.hasContent(book, chapter)) { |
|
|
|
} else { |
|
|
|
removeLoading(chapter.index) |
|
|
|
download(book, chapter) |
|
|
|
} else { |
|
|
|
} |
|
|
|
download(chapter) |
|
|
|
} ?: removeLoading(index) |
|
|
|
} |
|
|
|
}.onError { |
|
|
|
} ?: removeLoading(index) |
|
|
|
removeLoading(index) |
|
|
|
}.onError { |
|
|
|
|
|
|
|
removeLoading(index) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun download(book: Book, chapter: BookChapter) { |
|
|
|
private fun download(chapter: BookChapter) { |
|
|
|
webBook?.getContent(book, chapter, scope = this) |
|
|
|
book?.let { book -> |
|
|
|
?.onSuccess(IO) { content -> |
|
|
|
webBook?.getContent(book, chapter, scope = this) |
|
|
|
if (content.isNullOrEmpty()) { |
|
|
|
?.onSuccess(IO) { content -> |
|
|
|
contentLoadFinish(chapter, context.getString(R.string.content_empty)) |
|
|
|
if (content.isNullOrEmpty()) { |
|
|
|
removeLoading(chapter.index) |
|
|
|
contentLoadFinish(chapter, context.getString(R.string.content_empty)) |
|
|
|
} else { |
|
|
|
removeLoading(chapter.index) |
|
|
|
BookHelp.saveContent(book, chapter, content) |
|
|
|
} else { |
|
|
|
contentLoadFinish(chapter, content) |
|
|
|
BookHelp.saveContent(book, chapter, content) |
|
|
|
|
|
|
|
contentLoadFinish(chapter, content) |
|
|
|
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}?.onError { |
|
|
|
|
|
|
|
contentLoadFinish(chapter, it.localizedMessage) |
|
|
|
removeLoading(chapter.index) |
|
|
|
removeLoading(chapter.index) |
|
|
|
} |
|
|
|
} |
|
|
|
}?.onError { |
|
|
|
} |
|
|
|
contentLoadFinish(chapter, it.localizedMessage) |
|
|
|
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun addLoading(index: Int): Boolean { |
|
|
|
private fun addLoading(index: Int): Boolean { |
|
|
@ -332,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) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|