From 08ce86ba408d73a7975f3f73f9a15a6b241d4e74 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 2 Feb 2020 08:08:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/main/bookshelf/BookshelfViewModel.kt | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt index b41031484..0c4033587 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt @@ -38,6 +38,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) } fun addBookByUrl(bookUrls: String) { + var successCount = 0 execute { var hasBookUrlPattern: List? = null val urls = bookUrls.split("\n") @@ -59,17 +60,30 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) } } source?.let { bookSource -> - val book = Book(bookUrl = bookUrl) - WebBook(bookSource).getBookInfo(book, this) - .onSuccess(IO) { - it?.let { book -> - App.db.bookDao().insert(book) + val book = Book( + bookUrl = bookUrl, + origin = bookSource.bookSourceUrl, + originName = bookSource.bookSourceName + ) + suspend { + WebBook(bookSource).getBookInfo(book, this) + .onSuccess(IO) { + it?.let { book -> + App.db.bookDao().insert(book) + successCount++ + } + }.onError { + throw Exception(it.localizedMessage) } - } + } } } }.onSuccess { - toast(R.string.success) + if (successCount > 0) { + toast(R.string.success) + } else { + toast("ERROR") + } }.onError { toast(it.localizedMessage ?: "ERROR") }