pull/32/head
kunfei 5 years ago
parent 4d3d802653
commit 35968acd4c
  1. 14
      app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt
  2. 14
      app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt

@ -249,28 +249,20 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
* 内容加载完成 * 内容加载完成
*/ */
override fun contentLoadFinish(bookChapter: BookChapter, content: String) { override fun contentLoadFinish(bookChapter: BookChapter, content: String) {
launch {
when (bookChapter.index) { when (bookChapter.index) {
viewModel.durChapterIndex -> { viewModel.durChapterIndex -> launch {
viewModel.curTextChapter = viewModel.curTextChapter =
ChapterProvider.getTextChapter(content_text_view, bookChapter, content) ChapterProvider.getTextChapter(content_text_view, bookChapter, content)
page_view.upContent() page_view.upContent()
curChapterChanged() curChapterChanged()
} }
viewModel.durChapterIndex - 1 -> { viewModel.durChapterIndex - 1 -> launch {
viewModel.prevTextChapter = viewModel.prevTextChapter =
ChapterProvider.getTextChapter(content_text_view, bookChapter, content) ChapterProvider.getTextChapter(content_text_view, bookChapter, content)
} }
viewModel.durChapterIndex + 1 -> { viewModel.durChapterIndex + 1 -> launch {
viewModel.nextTextChapter = viewModel.nextTextChapter =
ChapterProvider.getTextChapter(content_text_view, bookChapter, content) ChapterProvider.getTextChapter(content_text_view, bookChapter, content)
viewModel.bookData.value?.let {
for (i in viewModel.durChapterIndex.plus(2)
..viewModel.durChapterIndex.plus(8)) {
viewModel.download(it, i)
}
}
}
} }
} }
} }

@ -101,7 +101,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
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 {
callBack?.contentLoadFinish(chapter, it) contentLoadFinish(chapter, it)
synchronized(loadingLock) { synchronized(loadingLock) {
loadingChapters.remove(index) loadingChapters.remove(index)
} }
@ -138,25 +138,31 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
webBook?.getContent(book, chapter, scope = this) webBook?.getContent(book, chapter, scope = this)
?.onSuccess(IO) { content -> ?.onSuccess(IO) { content ->
if (content.isNullOrEmpty()) { if (content.isNullOrEmpty()) {
callBack?.contentLoadFinish(chapter, context.getString(R.string.content_empty)) contentLoadFinish(chapter, context.getString(R.string.content_empty))
synchronized(loadingLock) { synchronized(loadingLock) {
loadingChapters.remove(chapter.index) loadingChapters.remove(chapter.index)
} }
} else { } else {
BookHelp.saveContent(book, chapter, content) BookHelp.saveContent(book, chapter, content)
callBack?.contentLoadFinish(chapter, content) contentLoadFinish(chapter, content)
synchronized(loadingLock) { synchronized(loadingLock) {
loadingChapters.remove(chapter.index) loadingChapters.remove(chapter.index)
} }
} }
}?.onError { }?.onError {
callBack?.contentLoadFinish(chapter, it.localizedMessage) contentLoadFinish(chapter, it.localizedMessage)
synchronized(loadingLock) { synchronized(loadingLock) {
loadingChapters.remove(chapter.index) loadingChapters.remove(chapter.index)
} }
} }
} }
private fun contentLoadFinish(chapter: BookChapter, content: String) {
if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) {
callBack?.contentLoadFinish(chapter, content)
}
}
fun changeTo(book: Book) { fun changeTo(book: Book) {
execute { execute {
bookData.value?.let { bookData.value?.let {

Loading…
Cancel
Save