|
|
|
@ -79,11 +79,11 @@ object ReadBook { |
|
|
|
|
nextTextChapter = null |
|
|
|
|
book?.let { |
|
|
|
|
if (curTextChapter == null) { |
|
|
|
|
loadContent(durChapterIndex, upContent) |
|
|
|
|
loadContent(durChapterIndex, upContent, false) |
|
|
|
|
} else if (upContent) { |
|
|
|
|
callBack?.upContent() |
|
|
|
|
} |
|
|
|
|
loadContent(durChapterIndex.plus(1), upContent) |
|
|
|
|
loadContent(durChapterIndex.plus(1), upContent, false) |
|
|
|
|
GlobalScope.launch(Dispatchers.IO) { |
|
|
|
|
for (i in 2..10) { |
|
|
|
|
delay(100) |
|
|
|
@ -109,11 +109,11 @@ object ReadBook { |
|
|
|
|
prevTextChapter = null |
|
|
|
|
book?.let { |
|
|
|
|
if (curTextChapter == null) { |
|
|
|
|
loadContent(durChapterIndex, upContent) |
|
|
|
|
loadContent(durChapterIndex, upContent, false) |
|
|
|
|
} else if (upContent) { |
|
|
|
|
callBack?.upContent() |
|
|
|
|
} |
|
|
|
|
loadContent(durChapterIndex.minus(1), upContent) |
|
|
|
|
loadContent(durChapterIndex.minus(1), upContent, false) |
|
|
|
|
GlobalScope.launch(Dispatchers.IO) { |
|
|
|
|
for (i in -5..-2) { |
|
|
|
|
delay(100) |
|
|
|
@ -194,21 +194,21 @@ object ReadBook { |
|
|
|
|
/** |
|
|
|
|
* 加载章节内容 |
|
|
|
|
*/ |
|
|
|
|
fun loadContent() { |
|
|
|
|
loadContent(durChapterIndex) |
|
|
|
|
loadContent(durChapterIndex + 1) |
|
|
|
|
loadContent(durChapterIndex - 1) |
|
|
|
|
fun loadContent(resetPageOffset: Boolean) { |
|
|
|
|
loadContent(durChapterIndex, resetPageOffset = resetPageOffset) |
|
|
|
|
loadContent(durChapterIndex + 1, resetPageOffset = resetPageOffset) |
|
|
|
|
loadContent(durChapterIndex - 1, resetPageOffset = resetPageOffset) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun loadContent(index: Int, upContent: Boolean = true) { |
|
|
|
|
fun loadContent(index: Int, upContent: Boolean = true, resetPageOffset: Boolean) { |
|
|
|
|
book?.let { book -> |
|
|
|
|
if (addLoading(index)) { |
|
|
|
|
Coroutine.async { |
|
|
|
|
App.db.bookChapterDao().getChapter(book.bookUrl, index)?.let { chapter -> |
|
|
|
|
BookHelp.getContent(book, chapter)?.let { |
|
|
|
|
contentLoadFinish(chapter, it, upContent) |
|
|
|
|
contentLoadFinish(chapter, it, upContent, resetPageOffset) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} ?: download(chapter) |
|
|
|
|
} ?: download(chapter, resetPageOffset = resetPageOffset) |
|
|
|
|
} ?: removeLoading(index) |
|
|
|
|
}.onError { |
|
|
|
|
removeLoading(index) |
|
|
|
@ -226,7 +226,7 @@ object ReadBook { |
|
|
|
|
if (BookHelp.hasContent(book, chapter)) { |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} else { |
|
|
|
|
download(chapter) |
|
|
|
|
download(chapter, false) |
|
|
|
|
} |
|
|
|
|
} ?: removeLoading(index) |
|
|
|
|
}.onError { |
|
|
|
@ -236,20 +236,28 @@ object ReadBook { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun download(chapter: BookChapter) { |
|
|
|
|
private fun download(chapter: BookChapter, resetPageOffset: Boolean) { |
|
|
|
|
book?.let { book -> |
|
|
|
|
webBook?.getContent(book, chapter) |
|
|
|
|
?.onSuccess(Dispatchers.IO) { content -> |
|
|
|
|
if (content.isNullOrEmpty()) { |
|
|
|
|
contentLoadFinish(chapter, App.INSTANCE.getString(R.string.content_empty)) |
|
|
|
|
contentLoadFinish( |
|
|
|
|
chapter, |
|
|
|
|
App.INSTANCE.getString(R.string.content_empty), |
|
|
|
|
resetPageOffset = resetPageOffset |
|
|
|
|
) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} else { |
|
|
|
|
BookHelp.saveContent(book, chapter, content) |
|
|
|
|
contentLoadFinish(chapter, content) |
|
|
|
|
contentLoadFinish(chapter, content, resetPageOffset = resetPageOffset) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} |
|
|
|
|
}?.onError { |
|
|
|
|
contentLoadFinish(chapter, it.localizedMessage ?: "未知错误") |
|
|
|
|
contentLoadFinish( |
|
|
|
|
chapter, |
|
|
|
|
it.localizedMessage ?: "未知错误", |
|
|
|
|
resetPageOffset = resetPageOffset |
|
|
|
|
) |
|
|
|
|
removeLoading(chapter.index) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -275,7 +283,8 @@ object ReadBook { |
|
|
|
|
private fun contentLoadFinish( |
|
|
|
|
chapter: BookChapter, |
|
|
|
|
content: String, |
|
|
|
|
upContent: Boolean = true |
|
|
|
|
upContent: Boolean = true, |
|
|
|
|
resetPageOffset: Boolean |
|
|
|
|
) { |
|
|
|
|
Coroutine.async { |
|
|
|
|
if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { |
|
|
|
@ -289,18 +298,18 @@ object ReadBook { |
|
|
|
|
when (chapter.index) { |
|
|
|
|
durChapterIndex -> { |
|
|
|
|
curTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize) |
|
|
|
|
if (upContent) callBack?.upContent() |
|
|
|
|
if (upContent) callBack?.upContent(resetPageOffset = resetPageOffset) |
|
|
|
|
callBack?.upView() |
|
|
|
|
curPageChanged() |
|
|
|
|
callBack?.contentLoadFinish() |
|
|
|
|
} |
|
|
|
|
durChapterIndex - 1 -> { |
|
|
|
|
prevTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize) |
|
|
|
|
if (upContent) callBack?.upContent(-1) |
|
|
|
|
if (upContent) callBack?.upContent(-1, resetPageOffset) |
|
|
|
|
} |
|
|
|
|
durChapterIndex + 1 -> { |
|
|
|
|
nextTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize) |
|
|
|
|
if (upContent) callBack?.upContent(1) |
|
|
|
|
if (upContent) callBack?.upContent(1, resetPageOffset) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -326,7 +335,7 @@ object ReadBook { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
interface CallBack { |
|
|
|
|
fun upContent(relativePosition: Int = 0) |
|
|
|
|
fun upContent(relativePosition: Int = 0, resetPageOffset: Boolean = true) |
|
|
|
|
fun upView() |
|
|
|
|
fun upPageProgress() |
|
|
|
|
fun contentLoadFinish() |
|
|
|
|