优化
This commit is contained in:
@@ -164,39 +164,37 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
canReName: Boolean = true,
|
canReName: Boolean = true,
|
||||||
scope: CoroutineScope = viewModelScope
|
scope: CoroutineScope = viewModelScope
|
||||||
) {
|
) {
|
||||||
execute(scope) {
|
if (book.isLocal) {
|
||||||
if (book.isLocal) {
|
loadChapter(book, scope)
|
||||||
loadChapter(book, scope)
|
} else {
|
||||||
} else {
|
val bookSource = bookSource ?: let {
|
||||||
bookSource?.let { bookSource ->
|
chapterListData.postValue(emptyList())
|
||||||
WebBook.getBookInfo(this, bookSource, book, canReName = canReName)
|
context.toastOnUi(R.string.error_no_source)
|
||||||
.onSuccess(IO) {
|
return
|
||||||
val dbBook = appDb.bookDao.getBook(book.name, book.author)
|
|
||||||
if (dbBook != null) {
|
|
||||||
dbBook.updateTo(it)
|
|
||||||
inBookshelf = true
|
|
||||||
}
|
|
||||||
bookData.postValue(it)
|
|
||||||
if (inBookshelf) {
|
|
||||||
appDb.bookDao.update(it)
|
|
||||||
if (dbBook!!.name != book.name) {
|
|
||||||
BookHelp.updateCacheFolder(dbBook, book)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (it.isWebFile) {
|
|
||||||
loadWebFile(it, scope)
|
|
||||||
} else {
|
|
||||||
loadChapter(it, scope)
|
|
||||||
}
|
|
||||||
}.onError {
|
|
||||||
AppLog.put("获取书籍信息失败\n${it.localizedMessage}", it)
|
|
||||||
context.toastOnUi(R.string.error_get_book_info)
|
|
||||||
}
|
|
||||||
} ?: let {
|
|
||||||
chapterListData.postValue(emptyList())
|
|
||||||
context.toastOnUi(R.string.error_no_source)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
WebBook.getBookInfo(scope, bookSource, book, canReName = canReName)
|
||||||
|
.onSuccess(IO) {
|
||||||
|
val dbBook = appDb.bookDao.getBook(book.name, book.author)
|
||||||
|
if (dbBook != null) {
|
||||||
|
dbBook.updateTo(it)
|
||||||
|
inBookshelf = true
|
||||||
|
}
|
||||||
|
bookData.postValue(it)
|
||||||
|
if (inBookshelf) {
|
||||||
|
appDb.bookDao.update(it)
|
||||||
|
if (dbBook!!.name != book.name) {
|
||||||
|
BookHelp.updateCacheFolder(dbBook, book)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (it.isWebFile) {
|
||||||
|
loadWebFile(it, scope)
|
||||||
|
} else {
|
||||||
|
loadChapter(it, scope)
|
||||||
|
}
|
||||||
|
}.onError {
|
||||||
|
AppLog.put("获取书籍信息失败\n${it.localizedMessage}", it)
|
||||||
|
context.toastOnUi(R.string.error_get_book_info)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,47 +202,47 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
book: Book,
|
book: Book,
|
||||||
scope: CoroutineScope = viewModelScope
|
scope: CoroutineScope = viewModelScope
|
||||||
) {
|
) {
|
||||||
execute(scope) {
|
if (book.isLocal) {
|
||||||
if (book.isLocal) {
|
execute(scope) {
|
||||||
LocalBook.getChapterList(book).let {
|
LocalBook.getChapterList(book).let {
|
||||||
appDb.bookDao.update(book)
|
appDb.bookDao.update(book)
|
||||||
appDb.bookChapterDao.delByBook(book.bookUrl)
|
appDb.bookChapterDao.delByBook(book.bookUrl)
|
||||||
appDb.bookChapterDao.insert(*it.toTypedArray())
|
appDb.bookChapterDao.insert(*it.toTypedArray())
|
||||||
chapterListData.postValue(it)
|
chapterListData.postValue(it)
|
||||||
}
|
}
|
||||||
} else {
|
}.onError {
|
||||||
bookSource?.let { bookSource ->
|
context.toastOnUi("LoadTocError:${it.localizedMessage}")
|
||||||
val oldBook = book.copy()
|
|
||||||
WebBook.getChapterList(this, bookSource, book, true)
|
|
||||||
.onSuccess(IO) {
|
|
||||||
val dbBook = appDb.bookDao.getBook(book.name, book.author)
|
|
||||||
if (dbBook?.bookUrl == oldBook.bookUrl) {
|
|
||||||
if (oldBook.bookUrl == book.bookUrl) {
|
|
||||||
appDb.bookDao.update(book)
|
|
||||||
} else {
|
|
||||||
appDb.bookDao.insert(book)
|
|
||||||
BookHelp.updateCacheFolder(oldBook, book)
|
|
||||||
}
|
|
||||||
appDb.bookChapterDao.delByBook(oldBook.bookUrl)
|
|
||||||
appDb.bookChapterDao.insert(*it.toTypedArray())
|
|
||||||
if (book.isSameNameAuthor(ReadBook.book)) {
|
|
||||||
ReadBook.book = book
|
|
||||||
ReadBook.chapterSize = book.totalChapterNum
|
|
||||||
}
|
|
||||||
}
|
|
||||||
chapterListData.postValue(it)
|
|
||||||
}.onError {
|
|
||||||
chapterListData.postValue(emptyList())
|
|
||||||
AppLog.put("获取目录失败\n${it.localizedMessage}", it)
|
|
||||||
context.toastOnUi(R.string.error_get_chapter_list)
|
|
||||||
}
|
|
||||||
} ?: let {
|
|
||||||
chapterListData.postValue(emptyList())
|
|
||||||
context.toastOnUi(R.string.error_no_source)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}.onError {
|
} else {
|
||||||
context.toastOnUi("LoadTocError:${it.localizedMessage}")
|
val bookSource = bookSource ?: let {
|
||||||
|
chapterListData.postValue(emptyList())
|
||||||
|
context.toastOnUi(R.string.error_no_source)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val oldBook = book.copy()
|
||||||
|
WebBook.getChapterList(scope, bookSource, book, true)
|
||||||
|
.onSuccess(IO) {
|
||||||
|
val dbBook = appDb.bookDao.getBook(book.name, book.author)
|
||||||
|
if (dbBook?.bookUrl == oldBook.bookUrl) {
|
||||||
|
if (oldBook.bookUrl == book.bookUrl) {
|
||||||
|
appDb.bookDao.update(book)
|
||||||
|
} else {
|
||||||
|
appDb.bookDao.insert(book)
|
||||||
|
BookHelp.updateCacheFolder(oldBook, book)
|
||||||
|
}
|
||||||
|
appDb.bookChapterDao.delByBook(oldBook.bookUrl)
|
||||||
|
appDb.bookChapterDao.insert(*it.toTypedArray())
|
||||||
|
if (book.isSameNameAuthor(ReadBook.book)) {
|
||||||
|
ReadBook.book = book
|
||||||
|
ReadBook.chapterSize = book.totalChapterNum
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chapterListData.postValue(it)
|
||||||
|
}.onError {
|
||||||
|
chapterListData.postValue(emptyList())
|
||||||
|
AppLog.put("获取目录失败\n${it.localizedMessage}", it)
|
||||||
|
context.toastOnUi(R.string.error_get_chapter_list)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user