pull/32/head
kunfei 5 years ago
parent 1b2ace5e81
commit 4be2f8d2b1
  1. 42
      app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt

@ -69,23 +69,33 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
}.onError { it.printStackTrace() } }.onError { it.printStackTrace() }
} }
private fun loadBookInfo(book: Book) { private fun loadBookInfo(
book: Book,
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) {
execute { execute {
webBook?.getBookInfo(book, this) webBook?.getBookInfo(book, this)
?.onSuccess { ?.onSuccess {
loadChapterList(book) loadChapterList(book, changeDruChapterIndex)
} }
} }
} }
private fun loadChapterList(book: Book) { private fun loadChapterList(
book: Book,
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) {
execute { execute {
webBook?.getChapterList(book, this) webBook?.getChapterList(book, this)
?.onSuccess(IO) { cList -> ?.onSuccess(IO) { cList ->
if (!cList.isNullOrEmpty()) { if (!cList.isNullOrEmpty()) {
App.db.bookChapterDao().insert(*cList.toTypedArray()) if (changeDruChapterIndex == null) {
chapterSize = cList.size App.db.bookChapterDao().insert(*cList.toTypedArray())
chapterListFinish.postValue(true) chapterSize = cList.size
chapterListFinish.postValue(true)
} else {
changeDruChapterIndex(cList)
}
} else { } else {
toast(R.string.error_load_toc) toast(R.string.error_load_toc)
} }
@ -221,9 +231,9 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
webBook = WebBook(it) webBook = WebBook(it)
} }
if (book.tocUrl.isEmpty()) { if (book.tocUrl.isEmpty()) {
loadBookInfo(book) loadBookInfo(book) { upChangeDurChapterIndex(book, it) }
} else { } else {
loadChapterList(book) loadChapterList(book) { upChangeDurChapterIndex(book, it) }
} }
} }
} }
@ -232,6 +242,22 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
private fun upChangeDurChapterIndex(book: Book, chapters: List<BookChapter>) {
execute {
durChapterIndex = BookHelp.getDurChapterIndexByChapterTitle(
book.durChapterTitle,
book.durChapterIndex,
chapters
)
book.durChapterIndex = durChapterIndex
book.durChapterTitle = chapters[durChapterIndex].title
App.db.bookDao().update(book)
App.db.bookChapterDao().insert(*chapters.toTypedArray())
chapterSize = chapters.size
chapterListFinish.postValue(true)
}
}
fun openChapter(chapter: BookChapter) { fun openChapter(chapter: BookChapter) {
prevTextChapter = null prevTextChapter = null
curTextChapter = null curTextChapter = null

Loading…
Cancel
Save