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