From 4fa4be52f75d44b899a9cd9ed692a90f7c0e9015 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 21 Apr 2022 10:00:18 +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/java/io/legado/app/data/entities/Book.kt | 9 ++++++--- .../java/io/legado/app/data/entities/BookChapter.kt | 10 +++++++--- .../io/legado/app/ui/book/toc/ChapterListAdapter.kt | 5 ++++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/Book.kt b/app/src/main/java/io/legado/app/data/entities/Book.kt index e042d2525..072fbe9a3 100644 --- a/app/src/main/java/io/legado/app/data/entities/Book.kt +++ b/app/src/main/java/io/legado/app/data/entities/Book.kt @@ -14,6 +14,7 @@ import io.legado.app.model.ReadBook import io.legado.app.utils.GSON import io.legado.app.utils.MD5Utils import io.legado.app.utils.fromJsonObject +import kotlinx.coroutines.runBlocking import kotlinx.parcelize.IgnoredOnParcel import kotlinx.parcelize.Parcelize import java.nio.charset.Charset @@ -283,9 +284,11 @@ data class Book( fun changeTo(newBook: Book, toc: List): Book { newBook.durChapterIndex = BookHelp .getDurChapter(durChapterIndex, durChapterTitle, toc, totalChapterNum) - newBook.durChapterTitle = toc[newBook.durChapterIndex].getDisplayTitle( - ContentProcessor.get(newBook.name, newBook.origin).getTitleReplaceRules() - ) + newBook.durChapterTitle = runBlocking { + toc[newBook.durChapterIndex].getDisplayTitle( + ContentProcessor.get(newBook.name, newBook.origin).getTitleReplaceRules() + ) + } newBook.durChapterPos = durChapterPos newBook.group = group newBook.order = order diff --git a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt index 507e50261..6176cb35f 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookChapter.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookChapter.kt @@ -8,6 +8,8 @@ import androidx.room.Index import com.github.liuyueyi.quick.transfer.ChineseUtils import io.legado.app.R import io.legado.app.constant.AppPattern +import io.legado.app.data.appDb +import io.legado.app.exception.RegexTimeoutException import io.legado.app.help.RuleBigDataHelp import io.legado.app.help.config.AppConfig import io.legado.app.model.analyzeRule.AnalyzeUrl @@ -79,8 +81,7 @@ data class BookChapter( return false } - @Suppress("unused") - fun getDisplayTitle( + suspend fun getDisplayTitle( replaceRules: List? = null, useReplace: Boolean = true, chineseConvert: Boolean = true, @@ -97,13 +98,16 @@ data class BookChapter( if (item.pattern.isNotEmpty()) { try { val mDisplayTitle = if (item.isRegex) { - displayTitle.replace(item.pattern.toRegex(), item.replacement) + displayTitle.replace(item.pattern.toRegex(), item.replacement, 100) } else { displayTitle.replace(item.pattern, item.replacement) } if (mDisplayTitle.isNotBlank()) { displayTitle = mDisplayTitle } + } catch (e: RegexTimeoutException) { + item.isEnabled = false + appDb.replaceRuleDao.update(item) } catch (e: Exception) { appCtx.toastOnUi("${item.name}替换出错") } diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt index 2c16a8530..ba6420570 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListAdapter.kt @@ -20,6 +20,7 @@ import io.legado.app.utils.longToastOnUi import io.legado.app.utils.visible import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.isActive +import kotlinx.coroutines.runBlocking import java.util.concurrent.ConcurrentHashMap class ChapterListAdapter(context: Context, val callback: Callback) : @@ -97,7 +98,9 @@ class ChapterListAdapter(context: Context, val callback: Callback) : if (displayTitle != null) { return displayTitle } - displayTitle = chapter.getDisplayTitle(replaceRules, useReplace) + displayTitle = runBlocking { + chapter.getDisplayTitle(replaceRules, useReplace) + } displayTitleMap[chapter.title] = displayTitle return displayTitle }