From c43b9b2b07fbbd1f887d7a2bbdfb356f7c94633b Mon Sep 17 00:00:00 2001 From: gedoor Date: Wed, 30 Dec 2020 17:03:36 +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 | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 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 1d26c7159..d8ec442ad 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 @@ -72,6 +72,7 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) bookMap["name"] = it.name bookMap["author"] = it.author bookMap["bookUrl"] = it.bookUrl + bookMap["coverUrl"] = it.coverUrl bookMap["tocUrl"] = it.tocUrl bookMap["kind"] = it.kind bookMap["intro"] = it.getDisplayIntro() @@ -88,29 +89,36 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) fun importBookshelf(str: String, groupId: Long) { execute { val text = str.trim() - if (text.isAbsUrl()) { - RxHttp.get(text).toText().await().let { - importBookshelf(it, groupId) + when { + text.isAbsUrl() -> { + RxHttp.get(text).toText().await().let { + importBookshelf(it, groupId) + } } - } else if (text.isJsonArray()) { - GSON.fromJsonArray>(text)?.forEach { - val book = Book( - bookUrl = it["bookUrl"] ?: "", - name = it["name"] ?: "", - author = it["author"] ?: "", - tocUrl = it["tocUrl"] ?: "", - kind = it["kind"], - intro = it["intro"] ?: "", - origin = it["origin"] ?: "", - originName = it["originName"] ?: "" - ) - if (groupId > 0) { - book.group = groupId + text.isJsonArray() -> { + GSON.fromJsonArray>(text)?.forEach { + val book = Book( + bookUrl = it["bookUrl"] ?: "", + name = it["name"] ?: "", + author = it["author"] ?: "", + coverUrl = it["coverUrl"], + tocUrl = it["tocUrl"] ?: "", + kind = it["kind"], + intro = it["intro"] ?: "", + origin = it["origin"] ?: "", + originName = it["originName"] ?: "" + ) + if (groupId > 0) { + book.group = groupId + } + if (App.db.bookDao.getBook(book.name, book.author) == null) { + App.db.bookDao.insert(book) + } } - App.db.bookDao.insert(book) } - } else { - throw Exception("格式不对") + else -> { + throw Exception("格式不对") + } } }.onError { toast(it.localizedMessage ?: "ERROR")