pull/84/head
kunfei 5 years ago
parent 2f3e74c66f
commit 46d9acbf03
  1. 97
      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.data.entities.BookGroup
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import io.legado.app.model.localBook.AnalyzeTxtFile
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
class BookInfoViewModel(application: Application) : BaseViewModel(application) { class BookInfoViewModel(application: Application) : BaseViewModel(application) {
@ -55,26 +56,29 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
book: Book, book: Book,
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) { ) {
if (book.isLocalBook()) return
execute { execute {
isLoadingData.postValue(true) if (book.isLocalBook()) {
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> loadChapter(book, changeDruChapterIndex)
WebBook(bookSource).getBookInfo(book, this) } else {
.onSuccess(IO) { isLoadingData.postValue(true)
it?.let { App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
bookData.postValue(book) WebBook(bookSource).getBookInfo(book, this)
if (inBookshelf) { .onSuccess(IO) {
App.db.bookDao().update(book) 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 { } ?: let {
isLoadingData.postValue(false) isLoadingData.postValue(false)
toast(R.string.error_get_book_info) 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 { execute {
isLoadingData.postValue(true) isLoadingData.postValue(true)
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> if (book.isLocalBook()) {
WebBook(bookSource).getChapterList(book, this) AnalyzeTxtFile.analyze(context, book).let {
.onSuccess(IO) { App.db.bookDao().update(book)
it?.let { App.db.bookChapterDao().insert(*it.toTypedArray())
if (it.isNotEmpty()) { if (changeDruChapterIndex == null) {
if (inBookshelf) { chapterListData.postValue(it)
App.db.bookDao().update(book) isLoadingData.postValue(false)
App.db.bookChapterDao().insert(*it.toTypedArray()) } else {
} changeDruChapterIndex(it)
if (changeDruChapterIndex == null) { }
chapterListData.postValue(it) }
isLoadingData.postValue(false) } 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 { } 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 { } ?: let {
isLoadingData.postValue(false) isLoadingData.postValue(false)
toast(R.string.error_get_chapter_list) toast(R.string.error_no_source)
} }
} ?: let {
isLoadingData.postValue(false)
toast(R.string.error_no_source)
} }
} }
} }

Loading…
Cancel
Save