From 2d07447dfa5ab5925589dba113402d73a90fa083 Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 11 Feb 2022 09:06:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E8=A7=84=E5=88=99=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BD=9C=E7=94=A8=E4=BA=8E=E6=A0=87=E9=A2=98=E5=92=8C?= =?UTF-8?q?=E4=BD=9C=E7=94=A8=E4=BA=8E=E6=AD=A3=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/dao/ReplaceRuleDao.kt | 11 ++++++-- .../legado/app/data/entities/BookChapter.kt | 2 +- .../io/legado/app/help/ContentProcessor.kt | 27 ++++++++++++++----- .../main/java/io/legado/app/model/ReadBook.kt | 2 +- .../app/ui/book/toc/ChapterListAdapter.kt | 2 +- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt index 891e0bf77..bd4f4a549 100644 --- a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt @@ -42,11 +42,18 @@ interface ReplaceRuleDao { fun findByIds(vararg ids: Long): List @Query( - """SELECT * FROM replace_rules WHERE isEnabled = 1 + """SELECT * FROM replace_rules WHERE isEnabled = 1 and scopeContent = 1 AND (scope LIKE '%' || :name || '%' or scope LIKE '%' || :origin || '%' or scope is null or scope = '') order by sortOrder""" ) - fun findEnabledByScope(name: String, origin: String): List + fun findEnabledByContentScope(name: String, origin: String): List + + @Query( + """SELECT * FROM replace_rules WHERE isEnabled = 1 and scopeTitle = 1 + AND (scope LIKE '%' || :name || '%' or scope LIKE '%' || :origin || '%' or scope is null or scope = '') + order by sortOrder""" + ) + fun findEnabledByTitleScope(name: String, origin: String): List @Query("select * from replace_rules where `group` like '%' || :group || '%'") fun getByGroup(group: String): List 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 8258151dd..9afff1941 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 @@ -74,7 +74,7 @@ data class BookChapter( @Suppress("unused") fun getDisplayTitle( - replaceRules: Array? = null, + replaceRules: List? = null, useReplace: Boolean = true, chineseConvert: Boolean = true, ): String { 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 f8ed62e9a..ead11f278 100644 --- a/app/src/main/java/io/legado/app/help/ContentProcessor.kt +++ b/app/src/main/java/io/legado/app/help/ContentProcessor.kt @@ -37,7 +37,8 @@ class ContentProcessor private constructor( } - private val replaceRules = arrayListOf() + private val titleReplaceRules = arrayListOf() + private val contentReplaceRules = arrayListOf() init { upReplaceRules() @@ -45,13 +46,25 @@ class ContentProcessor private constructor( @Synchronized fun upReplaceRules() { - replaceRules.clear() - replaceRules.addAll(appDb.replaceRuleDao.findEnabledByScope(bookName, bookOrigin)) + titleReplaceRules.clear() + contentReplaceRules.clear() + titleReplaceRules.addAll(appDb.replaceRuleDao.findEnabledByTitleScope(bookName, bookOrigin)) + contentReplaceRules.addAll( + appDb.replaceRuleDao.findEnabledByContentScope( + bookName, + bookOrigin + ) + ) } @Synchronized - fun getReplaceRules(): Array { - return replaceRules.toTypedArray() + fun getTitleReplaceRules(): List { + return titleReplaceRules + } + + @Synchronized + fun getContentReplaceRules(): List { + return contentReplaceRules } fun getContent( @@ -94,7 +107,7 @@ class ContentProcessor private constructor( } if (includeTitle) { //重新添加标题 - mContent = chapter.getDisplayTitle(getReplaceRules()) + "\n" + mContent + mContent = chapter.getDisplayTitle(getTitleReplaceRules()) + "\n" + mContent } val contents = arrayListOf() mContent.split("\n").forEach { str -> @@ -114,7 +127,7 @@ class ContentProcessor private constructor( fun replaceContent(content: String): String { var mContent = content - getReplaceRules().forEach { item -> + getContentReplaceRules().forEach { item -> if (item.pattern.isNotEmpty()) { try { mContent = if (item.isRegex) { diff --git a/app/src/main/java/io/legado/app/model/ReadBook.kt b/app/src/main/java/io/legado/app/model/ReadBook.kt index ef2af53d5..2af0be2b3 100644 --- a/app/src/main/java/io/legado/app/model/ReadBook.kt +++ b/app/src/main/java/io/legado/app/model/ReadBook.kt @@ -352,7 +352,7 @@ object ReadBook : CoroutineScope by MainScope() { if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { val contentProcessor = ContentProcessor.get(book.name, book.origin) val displayTitle = chapter.getDisplayTitle( - contentProcessor.getReplaceRules(), + contentProcessor.getContentReplaceRules(), book.getUseReplaceRule() ) val contents = contentProcessor.getContent(book, chapter, content) 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 324de0410..bf58f8670 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 @@ -52,7 +52,7 @@ class ChapterListAdapter(context: Context, val callback: Callback) : private val replaceRules get() = callback.book?.let { - ContentProcessor.get(it.name, it.origin).getReplaceRules() + ContentProcessor.get(it.name, it.origin).getTitleReplaceRules() } private val useReplace get() = AppConfig.tocUiUseReplace && callback.book?.getUseReplaceRule() == true