diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index 8179c058a..0a54aaeac 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -11,6 +11,7 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookGroup import io.legado.app.help.BookHelp import io.legado.app.model.WebBook +import io.legado.app.model.localBook.AnalyzeTxtFile import kotlinx.coroutines.Dispatchers.IO class BookInfoViewModel(application: Application) : BaseViewModel(application) { @@ -55,26 +56,29 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { book: Book, changeDruChapterIndex: ((chapters: List) -> Unit)? = null ) { - if (book.isLocalBook()) return execute { - isLoadingData.postValue(true) - App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> - WebBook(bookSource).getBookInfo(book, this) - .onSuccess(IO) { - it?.let { - bookData.postValue(book) - if (inBookshelf) { - App.db.bookDao().update(book) + if (book.isLocalBook()) { + loadChapter(book, changeDruChapterIndex) + } else { + isLoadingData.postValue(true) + App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> + WebBook(bookSource).getBookInfo(book, this) + .onSuccess(IO) { + it?.let { + bookData.postValue(book) + if (inBookshelf) { + App.db.bookDao().update(book) + } + loadChapter(it, changeDruChapterIndex) } - loadChapter(it, changeDruChapterIndex) + }.onError { + isLoadingData.postValue(false) + toast(R.string.error_get_book_info) } - }.onError { - isLoadingData.postValue(false) - toast(R.string.error_get_book_info) - } - } ?: let { - isLoadingData.postValue(false) - toast(R.string.error_no_source) + } ?: let { + isLoadingData.postValue(false) + toast(R.string.error_no_source) + } } } } @@ -85,33 +89,46 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { ) { execute { isLoadingData.postValue(true) - App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> - WebBook(bookSource).getChapterList(book, this) - .onSuccess(IO) { - it?.let { - if (it.isNotEmpty()) { - if (inBookshelf) { - App.db.bookDao().update(book) - App.db.bookChapterDao().insert(*it.toTypedArray()) - } - if (changeDruChapterIndex == null) { - chapterListData.postValue(it) - isLoadingData.postValue(false) + if (book.isLocalBook()) { + AnalyzeTxtFile.analyze(context, book).let { + App.db.bookDao().update(book) + App.db.bookChapterDao().insert(*it.toTypedArray()) + if (changeDruChapterIndex == null) { + chapterListData.postValue(it) + isLoadingData.postValue(false) + } else { + changeDruChapterIndex(it) + } + } + } else { + App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> + WebBook(bookSource).getChapterList(book, this) + .onSuccess(IO) { + it?.let { + if (it.isNotEmpty()) { + if (inBookshelf) { + App.db.bookDao().update(book) + App.db.bookChapterDao().insert(*it.toTypedArray()) + } + if (changeDruChapterIndex == null) { + chapterListData.postValue(it) + isLoadingData.postValue(false) + } else { + changeDruChapterIndex(it) + } } else { - changeDruChapterIndex(it) + isLoadingData.postValue(false) + toast(R.string.chapter_list_empty) } - } else { - isLoadingData.postValue(false) - toast(R.string.chapter_list_empty) } + }.onError { + isLoadingData.postValue(false) + toast(R.string.error_get_chapter_list) } - }.onError { - isLoadingData.postValue(false) - toast(R.string.error_get_chapter_list) - } - } ?: let { - isLoadingData.postValue(false) - toast(R.string.error_no_source) + } ?: let { + isLoadingData.postValue(false) + toast(R.string.error_no_source) + } } } }