pull/32/head
kunfei 5 years ago
parent 428061ec3c
commit b7798d8910
  1. 50
      app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt

@ -46,35 +46,39 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
fun loadBookInfo(book: Book) { fun loadBookInfo(book: Book) {
isLoadingData.postValue(true) execute {
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> isLoadingData.postValue(true)
WebBook(bookSource).getBookInfo(book, this) App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
.onSuccess { WebBook(bookSource).getBookInfo(book, this)
it?.let { loadChapter(it) } .onSuccess {
}.onError { it?.let { loadChapter(it) }
toast(R.string.error_get_book_info) }.onError {
} toast(R.string.error_get_book_info)
} ?: toast(R.string.error_no_source) }
} ?: toast(R.string.error_no_source)
}
} }
fun loadChapter(book: Book) { fun loadChapter(book: Book) {
isLoadingData.postValue(true) execute {
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> isLoadingData.postValue(true)
WebBook(bookSource).getChapterList(book, this) App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
.onSuccess(IO) { WebBook(bookSource).getChapterList(book, this)
it?.let { .onSuccess(IO) {
if (it.isNotEmpty()) { it?.let {
if (inBookshelf) { if (it.isNotEmpty()) {
App.db.bookChapterDao().insert(*it.toTypedArray()) if (inBookshelf) {
App.db.bookChapterDao().insert(*it.toTypedArray())
}
chapterListData.postValue(it)
isLoadingData.postValue(false)
} }
chapterListData.postValue(it)
isLoadingData.postValue(false)
} }
}.onError {
toast(R.string.error_get_chapter_list)
} }
}.onError { } ?: toast(R.string.error_no_source)
toast(R.string.error_get_chapter_list) }
}
} ?: toast(R.string.error_no_source)
} }
fun saveBook(success: (() -> Unit)?) { fun saveBook(success: (() -> Unit)?) {

Loading…
Cancel
Save