From 4be2f8d2b1abb0030fe32fa0002a1696ba589ca6 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 30 Aug 2019 13:49:07 +0800 Subject: [PATCH] up --- .../app/ui/readbook/ReadBookViewModel.kt | 42 +++++++++++++++---- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt index 7fe1db4fc..78b743705 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt @@ -69,23 +69,33 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { }.onError { it.printStackTrace() } } - private fun loadBookInfo(book: Book) { + private fun loadBookInfo( + book: Book, + changeDruChapterIndex: ((chapters: List) -> Unit)? = null + ) { execute { webBook?.getBookInfo(book, this) ?.onSuccess { - loadChapterList(book) + loadChapterList(book, changeDruChapterIndex) } } } - private fun loadChapterList(book: Book) { + private fun loadChapterList( + book: Book, + changeDruChapterIndex: ((chapters: List) -> Unit)? = null + ) { execute { webBook?.getChapterList(book, this) ?.onSuccess(IO) { cList -> if (!cList.isNullOrEmpty()) { - App.db.bookChapterDao().insert(*cList.toTypedArray()) - chapterSize = cList.size - chapterListFinish.postValue(true) + if (changeDruChapterIndex == null) { + App.db.bookChapterDao().insert(*cList.toTypedArray()) + chapterSize = cList.size + chapterListFinish.postValue(true) + } else { + changeDruChapterIndex(cList) + } } else { toast(R.string.error_load_toc) } @@ -221,9 +231,9 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { webBook = WebBook(it) } if (book.tocUrl.isEmpty()) { - loadBookInfo(book) + loadBookInfo(book) { upChangeDurChapterIndex(book, it) } } 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) { + 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) { prevTextChapter = null curTextChapter = null