From 708a071e2e82f686cc5d2a6a67a12a4f5c1f60ff Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 9 Apr 2022 23:15:58 +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/api/controller/BookController.kt | 21 ++++++++++--------- .../io/legado/app/help/ContentProcessor.kt | 21 +++++++++++-------- .../app/ui/book/cache/CacheViewModel.kt | 9 ++++---- .../searchContent/SearchContentViewModel.kt | 2 +- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/io/legado/app/api/controller/BookController.kt b/app/src/main/java/io/legado/app/api/controller/BookController.kt index 85d6acc69..1ae31291b 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookController.kt @@ -183,23 +183,24 @@ object BookController { if (content != null) { val contentProcessor = ContentProcessor.get(book.name, book.origin) saveBookReadIndex(book, index) - return returnData.setData( - contentProcessor.getContent(book, chapter, content, includeTitle = false) + content = runBlocking { + contentProcessor.getContent(book, chapter, content!!, includeTitle = false) .joinToString("\n") - ) + } + return returnData.setData(content) } val bookSource = appDb.bookSourceDao.getBookSource(book.origin) ?: return returnData.setErrorMsg("未找到书源") try { content = runBlocking { - WebBook.getContentAwait(this, bookSource, book, chapter) + WebBook.getContentAwait(this, bookSource, book, chapter).let { + val contentProcessor = ContentProcessor.get(book.name, book.origin) + saveBookReadIndex(book, index) + contentProcessor.getContent(book, chapter, it, includeTitle = false) + .joinToString("\n") + } } - val contentProcessor = ContentProcessor.get(book.name, book.origin) - saveBookReadIndex(book, index) - returnData.setData( - contentProcessor.getContent(book, chapter, content, includeTitle = false) - .joinToString("\n") - ) + returnData.setData(content) } catch (e: Exception) { returnData.setErrorMsg(e.msg) } diff --git a/app/src/main/java/io/legado/app/help/ContentProcessor.kt b/app/src/main/java/io/legado/app/help/ContentProcessor.kt index 9b1c50472..3985099c0 100644 --- a/app/src/main/java/io/legado/app/help/ContentProcessor.kt +++ b/app/src/main/java/io/legado/app/help/ContentProcessor.kt @@ -9,6 +9,7 @@ import io.legado.app.data.entities.ReplaceRule import io.legado.app.help.config.AppConfig import io.legado.app.help.config.ReadBookConfig import io.legado.app.utils.toastOnUi +import kotlinx.coroutines.withTimeout import splitties.init.appCtx import java.lang.ref.WeakReference import java.util.concurrent.CopyOnWriteArrayList @@ -67,7 +68,7 @@ class ContentProcessor private constructor( return contentReplaceRules } - fun getContent( + suspend fun getContent( book: Book, chapter: BookChapter, content: String, @@ -128,18 +129,20 @@ class ContentProcessor private constructor( return contents } - fun replaceContent(content: String): String { + suspend fun replaceContent(content: String): String { var mContent = content getContentReplaceRules().forEach { item -> if (item.pattern.isNotEmpty()) { - try { - mContent = if (item.isRegex) { - mContent.replace(item.pattern.toRegex(), item.replacement) - } else { - mContent.replace(item.pattern, item.replacement) + kotlin.runCatching { + withTimeout(1000) { + mContent = if (item.isRegex) { + mContent.replace(item.pattern.toRegex(), item.replacement) + } else { + mContent.replace(item.pattern, item.replacement) + } } - } catch (e: Exception) { - AppLog.put("${item.name}替换出错\n${e.localizedMessage}") + }.onFailure { + AppLog.put("${item.name}替换出错\n${it.localizedMessage}") appCtx.toastOnUi("${item.name}替换出错") } } diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index 16b2bde11..945dc6a2a 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -137,7 +137,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { } } - private fun getAllContents( + private suspend fun getAllContents( scope: CoroutineScope, book: Book, append: (text: String, srcList: ArrayList>?) -> Unit @@ -214,7 +214,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { } @Suppress("BlockingMethodInNonBlockingContext") - private fun exportEpub(scope: CoroutineScope, doc: DocumentFile, book: Book) { + private suspend fun exportEpub(scope: CoroutineScope, doc: DocumentFile, book: Book) { val filename = "${getExportFileName(book)}.epub" DocumentUtils.delete(doc, filename) val epubBook = EpubBook() @@ -237,7 +237,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { } - private fun exportEpub(scope: CoroutineScope, file: File, book: Book) { + private suspend fun exportEpub(scope: CoroutineScope, file: File, book: Book) { val filename = "${getExportFileName(book)}.epub" val epubBook = EpubBook() epubBook.version = "2.0" @@ -252,6 +252,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { val bookFile = FileUtils.createFileWithReplace(bookPath) //设置正文 setEpubContent(scope, contentModel, book, epubBook) + @Suppress("BlockingMethodInNonBlockingContext") EpubWriter().write(epubBook, FileOutputStream(bookFile)) } @@ -404,7 +405,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { }) } - private fun setEpubContent( + private suspend fun setEpubContent( scope: CoroutineScope, contentModel: String, book: Book, diff --git a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt index bdabf6701..9eb680591 100644 --- a/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt @@ -76,7 +76,7 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati return searchResultsWithinChapter } - private fun searchPosition(pattern: String): List { + private suspend fun searchPosition(pattern: String): List { val position: MutableList = mutableListOf() var index = mContent.indexOf(pattern) if (index >= 0) {