From 1b2ace5e81e512d0cec206d4bd1b7a1af32e8742 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 30 Aug 2019 12:46:12 +0800 Subject: [PATCH] up --- .../main/java/io/legado/app/help/BookHelp.kt | 7 ++- .../app/ui/bookinfo/BookInfoViewModel.kt | 43 ++++++++++++++++--- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index c121e1781..187064ed8 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -94,17 +94,20 @@ object BookHelp { } fun getDurChapterIndexByChapterTitle( - title: String, + title: String?, index: Int, chapters: List ): Int { + if (title.isNullOrEmpty()) { + return min(index, chapters.lastIndex) + } if (chapters.size > index && title == chapters[index].title) { return index } var similarity = 0F var newIndex = index val start = max(index - 10, 0) - val end = min(index + 10, chapters.size - 1) + val end = min(index + 10, chapters.lastIndex) if (start < end) { for (i in start..end) { val s = title.similarity(chapters[i].title) diff --git a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt index 01308e15f..21a1b83ca 100644 --- a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt @@ -8,6 +8,7 @@ import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter +import io.legado.app.help.BookHelp import io.legado.app.model.WebBook import kotlinx.coroutines.Dispatchers.IO @@ -48,7 +49,10 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } } - fun loadBookInfo(book: Book) { + fun loadBookInfo( + book: Book, + changeDruChapterIndex: ((chapters: List) -> Unit)? = null + ) { execute { isLoadingData.postValue(true) App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> @@ -58,7 +62,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { if (inBookshelf) { App.db.bookDao().update(book) } - loadChapter(it) + loadChapter(it, changeDruChapterIndex) } }.onError { toast(R.string.error_get_book_info) @@ -67,7 +71,10 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } } - private fun loadChapter(book: Book) { + private fun loadChapter( + book: Book, + changeDruChapterIndex: ((chapters: List) -> Unit)? = null + ) { execute { isLoadingData.postValue(true) App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> @@ -79,12 +86,19 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { App.db.bookDao().update(book) App.db.bookChapterDao().insert(*it.toTypedArray()) } - chapterListData.postValue(it) - isLoadingData.postValue(false) + if (changeDruChapterIndex == null) { + chapterListData.postValue(it) + isLoadingData.postValue(false) + } else { + changeDruChapterIndex(it) + } + } else { + toast(R.string.chapter_list_empty) } } }.onError { toast(R.string.error_get_chapter_list) + it.printStackTrace() } } ?: toast(R.string.error_no_source) } @@ -100,13 +114,28 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } bookData.postValue(book) if (book.tocUrl.isEmpty()) { - loadBookInfo(book) + loadBookInfo(book) { upChangeDurChapterIndex(book, it) } } else { - loadChapter(book) + loadChapter(book) { upChangeDurChapterIndex(book, it) } } } } + private fun upChangeDurChapterIndex(book: Book, chapters: List) { + execute { + book.durChapterIndex = BookHelp.getDurChapterIndexByChapterTitle( + book.durChapterTitle, + book.durChapterIndex, + chapters + ) + book.durChapterTitle = chapters[book.durChapterIndex].title + App.db.bookDao().update(book) + App.db.bookChapterDao().insert(*chapters.toTypedArray()) + bookData.postValue(book) + chapterListData.postValue(chapters) + } + } + fun saveBook(success: (() -> Unit)?) { execute { bookData.value?.let { book ->