From 0f63f81411813a005c3aa196de3a7d0c7fc4d04a Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 26 Dec 2021 16:32:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=9B=BE=E7=89=87=E9=87=87?= =?UTF-8?q?=E7=94=A8=E5=A4=9A=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/BookHelp.kt | 33 +++++++++++++++---- .../legado/app/model/webBook/BookContent.kt | 2 +- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index 621959634..63dc4464d 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -10,6 +10,9 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.localBook.LocalBook import io.legado.app.utils.* +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Deferred +import kotlinx.coroutines.async import kotlinx.coroutines.delay import org.apache.commons.text.similarity.JaccardSimilarity import splitties.init.appCtx @@ -57,7 +60,19 @@ object BookHelp { } } - fun saveContent( + suspend fun saveContent( + scope: CoroutineScope, + bookSource: BookSource, + book: Book, + bookChapter: BookChapter, + content: String + ) { + saveText(book, bookChapter, content) + saveImages(scope, bookSource, book, bookChapter, content) + postEvent(EventBus.SAVE_CONTENT, bookChapter) + } + + private fun saveText( book: Book, bookChapter: BookChapter, content: String @@ -72,24 +87,28 @@ object BookHelp { ).writeText(content) } - suspend fun saveContent( + private suspend fun saveImages( + scope: CoroutineScope, bookSource: BookSource, book: Book, bookChapter: BookChapter, content: String ) { - saveContent(book, bookChapter, content) - //保存图片 + val awaitList = arrayListOf>() content.split("\n").forEach { val matcher = AppPattern.imgPattern.matcher(it) if (matcher.find()) { matcher.group(1)?.let { src -> val mSrc = NetworkUtils.getAbsoluteURL(bookChapter.url, src) - saveImage(bookSource, book, mSrc) + awaitList.add(scope.async { + saveImage(bookSource, book, mSrc) + }) } } } - postEvent(EventBus.SAVE_CONTENT, bookChapter) + awaitList.forEach { + it.await() + } } suspend fun saveImage(bookSource: BookSource?, book: Book, src: String) { @@ -194,7 +213,7 @@ object BookHelp { } else if (book.isEpub() && !hasContent(book, bookChapter)) { val string = LocalBook.getContent(book, bookChapter) string?.let { - saveContent(book, bookChapter, it) + saveText(book, bookChapter, it) } return string } else { diff --git a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt index c51a4e44e..d7b3116c6 100644 --- a/app/src/main/java/io/legado/app/model/webBook/BookContent.kt +++ b/app/src/main/java/io/legado/app/model/webBook/BookContent.kt @@ -121,7 +121,7 @@ object BookContent { if (contentStr.isBlank()) { throw ContentEmptyException("内容为空") } - BookHelp.saveContent(bookSource, book, bookChapter, contentStr) + BookHelp.saveContent(scope, bookSource, book, bookChapter, contentStr) return contentStr }