From 744b5cdb129333156412d6aed818009373f9b4ec Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 15 Oct 2021 22:59:40 +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 --- app/src/main/assets/updateLog.md | 1 + .../io/legado/app/model/webBook/WebBook.kt | 68 ++++++++++++------- .../app/ui/book/read/ReadBookViewModel.kt | 2 +- .../ui/main/bookshelf/BookshelfViewModel.kt | 21 +++--- 4 files changed, 56 insertions(+), 36 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index c93c2c8ba..4f8d6a41c 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -14,6 +14,7 @@ **2021/10/15** * 再次修复朗读卡住问题 +* 导入书单改为多线程 * 修复其它一些bug **2021/10/14** diff --git a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt index f69b520c1..373014f91 100644 --- a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt @@ -7,6 +7,7 @@ import io.legado.app.data.entities.SearchBook import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.http.StrResponse import io.legado.app.model.Debug +import io.legado.app.model.NoStackTraceException import io.legado.app.model.analyzeRule.AnalyzeUrl import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -15,32 +16,6 @@ import kotlin.coroutines.CoroutineContext @Suppress("MemberVisibilityCanBePrivate") object WebBook { - /** - * 精准搜索 - */ - suspend fun preciseSearch( - scope: CoroutineScope, - bookSources: List, - name: String, - author: String - ): Pair? { - bookSources.forEach { source -> - kotlin.runCatching { - if (!scope.isActive) return null - searchBookAwait(scope, source, name).firstOrNull { - it.name == name && it.author == author - }?.let { searchBook -> - if (!scope.isActive) return null - var book = searchBook.toBook() - if (book.tocUrl.isBlank()) { - book = getBookInfoAwait(scope, source, book) - } - return Pair(source, book) - } - } - } - return null - } /** * 搜索 @@ -326,4 +301,45 @@ object WebBook { ) } } + + /** + * 精准搜索 + */ + fun preciseSearch( + scope: CoroutineScope, + bookSources: List, + name: String, + author: String, + context: CoroutineContext = Dispatchers.IO, + ): Coroutine> { + return Coroutine.async(scope, context) { + preciseSearchAwait(scope, bookSources, name, author) + ?: throw NoStackTraceException("没有搜索到<$name>$author") + } + } + + suspend fun preciseSearchAwait( + scope: CoroutineScope, + bookSources: List, + name: String, + author: String + ): Pair? { + bookSources.forEach { source -> + kotlin.runCatching { + if (!scope.isActive) return null + searchBookAwait(scope, source, name).firstOrNull { + it.name == name && it.author == author + }?.let { searchBook -> + if (!scope.isActive) return null + var book = searchBook.toBook() + if (book.tocUrl.isBlank()) { + book = getBookInfoAwait(scope, source, book) + } + return Pair(source, book) + } + } + } + return null + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index eed44a628..18af0b1b2 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -197,7 +197,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { if (!AppConfig.autoChangeSource) return execute { val sources = appDb.bookSourceDao.allTextEnabled - WebBook.preciseSearch(this, sources, name, author)?.let { + WebBook.preciseSearchAwait(this, sources, name, author)?.let { it.second.upInfoFromOld(ReadBook.book) changeTo(it.first, it.second) } ?: throw NoStackTraceException("自动换源失败") 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 d32190b20..4366edfaf 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 @@ -108,18 +108,21 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application) private fun importBookshelfByJson(json: String, groupId: Long) { execute { val bookSources = appDb.bookSourceDao.allEnabled - GSON.fromJsonArray>(json)?.forEach { + GSON.fromJsonArray>(json)?.forEach { bookInfo -> if (!isActive) return@execute - val name = it["name"] ?: "" - val author = it["author"] ?: "" + val name = bookInfo["name"] ?: "" + val author = bookInfo["author"] ?: "" if (name.isNotEmpty() && appDb.bookDao.getBook(name, author) == null) { - val book = WebBook.preciseSearch(this, bookSources, name, author)?.second - book?.let { - if (groupId > 0) { - book.group = groupId + WebBook.preciseSearch(this, bookSources, name, author) + .onSuccess { + val book = it.second + if (groupId > 0) { + book.group = groupId + } + book.save() + }.onError { e -> + context.toastOnUi(e.localizedMessage) } - book.save() - } } } }.onFinally {