From b5bdea83ebf2551438e1b1064badd35a9755df10 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 23 Mar 2020 20:30:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/help/BookHelp.kt | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 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 b6e3ec000..0400d21cc 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -11,6 +11,7 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.ReplaceRule import io.legado.app.model.localBook.AnalyzeTxtFile import io.legado.app.utils.* +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.withContext import org.apache.commons.text.similarity.JaccardSimilarity @@ -242,24 +243,16 @@ object BookHelp { private var replaceRules: List = arrayListOf() @Synchronized - fun upReplaceRules(name: String? = null, origin: String? = null) { - if (name != null) { - if (bookName != name || bookOrigin != origin) { - replaceRules = if (origin.isNullOrEmpty()) { - App.db.replaceRuleDao().findEnabledByScope(name) - } else { - App.db.replaceRuleDao().findEnabledByScope(name, origin) - } - bookName = name - bookOrigin = origin - } - } else { - val o = bookOrigin - bookName?.let { - replaceRules = if (o.isNullOrEmpty()) { - App.db.replaceRuleDao().findEnabledByScope(it) - } else { - App.db.replaceRuleDao().findEnabledByScope(it, o) + suspend fun upReplaceRules() { + withContext(IO) { + synchronized(this) { + val o = bookOrigin + bookName?.let { + replaceRules = if (o.isNullOrEmpty()) { + App.db.replaceRuleDao().findEnabledByScope(it) + } else { + App.db.replaceRuleDao().findEnabledByScope(it, o) + } } } } @@ -274,7 +267,17 @@ object BookHelp { ): String { var c = content if (enableReplace) { - upReplaceRules(name, origin) + synchronized(this) { + if (bookName != name || bookOrigin != origin) { + bookName = name + bookOrigin = origin + replaceRules = if (origin.isNullOrEmpty()) { + App.db.replaceRuleDao().findEnabledByScope(name) + } else { + App.db.replaceRuleDao().findEnabledByScope(name, origin) + } + } + } replaceRules.forEach { item -> item.pattern.let { if (it.isNotEmpty()) {