diff --git a/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt b/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt index 8744ce2cb..dc1364559 100644 --- a/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt +++ b/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt @@ -1,11 +1,14 @@ package io.legado.app.data.entities import android.os.Parcelable +import android.text.TextUtils import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.Index import androidx.room.PrimaryKey import kotlinx.android.parcel.Parcelize +import java.util.regex.Pattern +import java.util.regex.PatternSyntaxException @Parcelize @Entity( @@ -37,4 +40,20 @@ data class ReplaceRule( override fun hashCode(): Int { return id.hashCode() } + + fun isValid(): Boolean{ + if (TextUtils.isEmpty(pattern)){ + return false; + } + //判断正则表达式是否正确 + if (isRegex){ + try { + Pattern.compile(pattern); + } + catch (ex: PatternSyntaxException){ + return false; + } + } + return true; + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt index 8d0c9cd2c..4af39cbb2 100644 --- a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt +++ b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt @@ -136,13 +136,17 @@ object ImportOldData { return bookSources.size } + + fun importOldReplaceRule(json: String): Int { val replaceRules = mutableListOf() val items: List> = Restore.jsonPath.parse(json).read("$") for (item in items) { val jsonItem = Restore.jsonPath.parse(item) OldRule.jsonToReplaceRule(jsonItem.jsonString())?.let { - replaceRules.add(it) + if (it.isValid()){ + replaceRules.add(it) + } } } App.db.replaceRuleDao().insert(*replaceRules.toTypedArray()) 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 d125674c5..73c64a0e1 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 @@ -16,6 +16,7 @@ import io.legado.app.constant.Theme import io.legado.app.data.entities.ReplaceRule import io.legado.app.utils.applyTint import io.legado.app.utils.getViewModel +import io.legado.app.utils.toast import kotlinx.android.synthetic.main.dialog_replace_edit.* class ReplaceEditDialog : DialogFragment(), @@ -73,9 +74,15 @@ class ReplaceEditDialog : DialogFragment(), override fun onMenuItemClick(item: MenuItem?): Boolean { when (item?.itemId) { R.id.menu_save -> { - viewModel.save(getReplaceRule()) { - callBack?.onReplaceRuleSave() - dismiss() + val rule = getReplaceRule(); + if (!rule.isValid()){ + toast(R.string.replace_rule_invalid) + } + else{ + viewModel.save(rule) { + callBack?.onReplaceRuleSave() + dismiss() + } } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 74aa0d74d..309f2bec2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -204,6 +204,7 @@ 清除缓存会删除所有已保存章节,是否确认删除? 书源共享 替换规则名称 + 替换规则为空或者不满足正则表达式要求 选择操作 全选 全选(%d/%d)