pull/32/head
kunfei 5 years ago
parent 1eab69c288
commit 1b2ace5e81
  1. 7
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 39
      app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt

@ -94,17 +94,20 @@ object BookHelp {
} }
fun getDurChapterIndexByChapterTitle( fun getDurChapterIndexByChapterTitle(
title: String, title: String?,
index: Int, index: Int,
chapters: List<BookChapter> chapters: List<BookChapter>
): Int { ): Int {
if (title.isNullOrEmpty()) {
return min(index, chapters.lastIndex)
}
if (chapters.size > index && title == chapters[index].title) { if (chapters.size > index && title == chapters[index].title) {
return index return index
} }
var similarity = 0F var similarity = 0F
var newIndex = index var newIndex = index
val start = max(index - 10, 0) val start = max(index - 10, 0)
val end = min(index + 10, chapters.size - 1) val end = min(index + 10, chapters.lastIndex)
if (start < end) { if (start < end) {
for (i in start..end) { for (i in start..end) {
val s = title.similarity(chapters[i].title) val s = title.similarity(chapters[i].title)

@ -8,6 +8,7 @@ import io.legado.app.R
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import kotlinx.coroutines.Dispatchers.IO 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<BookChapter>) -> Unit)? = null
) {
execute { execute {
isLoadingData.postValue(true) isLoadingData.postValue(true)
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
@ -58,7 +62,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
if (inBookshelf) { if (inBookshelf) {
App.db.bookDao().update(book) App.db.bookDao().update(book)
} }
loadChapter(it) loadChapter(it, changeDruChapterIndex)
} }
}.onError { }.onError {
toast(R.string.error_get_book_info) 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<BookChapter>) -> Unit)? = null
) {
execute { execute {
isLoadingData.postValue(true) isLoadingData.postValue(true)
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> 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.bookDao().update(book)
App.db.bookChapterDao().insert(*it.toTypedArray()) App.db.bookChapterDao().insert(*it.toTypedArray())
} }
if (changeDruChapterIndex == null) {
chapterListData.postValue(it) chapterListData.postValue(it)
isLoadingData.postValue(false) isLoadingData.postValue(false)
} else {
changeDruChapterIndex(it)
}
} else {
toast(R.string.chapter_list_empty)
} }
} }
}.onError { }.onError {
toast(R.string.error_get_chapter_list) toast(R.string.error_get_chapter_list)
it.printStackTrace()
} }
} ?: toast(R.string.error_no_source) } ?: toast(R.string.error_no_source)
} }
@ -100,11 +114,26 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
bookData.postValue(book) bookData.postValue(book)
if (book.tocUrl.isEmpty()) { if (book.tocUrl.isEmpty()) {
loadBookInfo(book) loadBookInfo(book) { upChangeDurChapterIndex(book, it) }
} else { } else {
loadChapter(book) loadChapter(book) { upChangeDurChapterIndex(book, it) }
}
} }
} }
private fun upChangeDurChapterIndex(book: Book, chapters: List<BookChapter>) {
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)?) { fun saveBook(success: (() -> Unit)?) {

Loading…
Cancel
Save