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 97c71f04c..7df2540c5 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -238,7 +238,29 @@ object BookHelp { private var bookOrigin: String? = null 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) + } + } + } + } fun disposeContent( title: String, @@ -247,23 +269,17 @@ object BookHelp { content: String, enableReplace: Boolean ): String { - synchronized(this) { - if (enableReplace && (bookName != name || bookOrigin != origin)) { - replaceRules = if (origin.isNullOrEmpty()) { - App.db.replaceRuleDao().findEnabledByScope(name) - } else { - App.db.replaceRuleDao().findEnabledByScope(name, origin) - } - } - } var c = content - for (item in replaceRules) { - item.pattern.let { - if (it.isNotEmpty()) { - c = if (item.isRegex) { - c.replace(it.toRegex(), item.replacement) - } else { - c.replace(it, item.replacement) + if (enableReplace) { + upReplaceRules(name, origin) + for (item in replaceRules) { + item.pattern.let { + if (it.isNotEmpty()) { + c = if (item.isRegex) { + c.replace(it.toRegex(), item.replacement) + } else { + c.replace(it, item.replacement) + } } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index f57a6d067..9b03a4991 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -20,7 +20,9 @@ import io.legado.app.constant.PreferKey import io.legado.app.constant.Status import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter +import io.legado.app.help.BookHelp import io.legado.app.help.ReadBookConfig +import io.legado.app.help.coroutine.Coroutine import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.noButton import io.legado.app.lib.dialogs.okButton @@ -64,6 +66,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo ChangeSourceDialog.CallBack, ReadBook.CallBack, TocRegexDialog.CallBack, + ReplaceEditDialog.CallBack, ColorPickerDialogListener { private val requestCodeChapterList = 568 private val requestCodeEditSource = 111 @@ -551,6 +554,10 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo } } + override fun onReplaceRuleSave() { + Coroutine.async { BookHelp.upReplaceRules() } + } + override fun showReadStyle() { ReadStyleDialog().show(supportFragmentManager, "readStyle") } diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt index 7910b9cb8..e5be539c5 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt @@ -20,7 +20,9 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.ReplaceRule +import io.legado.app.help.BookHelp import io.legado.app.help.ItemTouchCallback +import io.legado.app.help.coroutine.Coroutine import io.legado.app.lib.dialogs.* import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor @@ -292,6 +294,11 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi } } + override fun onDestroy() { + super.onDestroy() + Coroutine.async { BookHelp.upReplaceRules() } + } + override fun upCountView() { select_action_bar.upCountView(adapter.getSelection().size, adapter.getActualItemCount()) } diff --git a/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt b/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt index de5002931..71026502e 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt @@ -68,6 +68,7 @@ class ReplaceEditDialog : DialogFragment(), when (item?.itemId) { R.id.menu_save -> { viewModel.save(getReplaceRule()) { + callBack?.onReplaceRuleSave() dismiss() } } @@ -94,4 +95,10 @@ class ReplaceEditDialog : DialogFragment(), replaceRule.scope = et_scope.text.toString() return replaceRule } + + val callBack = activity as? CallBack + + interface CallBack { + fun onReplaceRuleSave() + } }