Merge pull request #133 from wqfantexi/patch1

替换规则有效性判断
pull/134/head^2
kunfei 5 years ago committed by GitHub
commit 2aace7bce8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt
  2. 4
      app/src/main/java/io/legado/app/help/storage/ImportOldData.kt
  3. 9
      app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt
  4. 1
      app/src/main/res/values/strings.xml

@ -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;
}
}

@ -136,15 +136,19 @@ object ImportOldData {
return bookSources.size
}
fun importOldReplaceRule(json: String): Int {
val replaceRules = mutableListOf<ReplaceRule>()
val items: List<Map<String, Any>> = Restore.jsonPath.parse(json).read("$")
for (item in items) {
val jsonItem = Restore.jsonPath.parse(item)
OldRule.jsonToReplaceRule(jsonItem.jsonString())?.let {
if (it.isValid()){
replaceRules.add(it)
}
}
}
App.db.replaceRuleDao().insert(*replaceRules.toTypedArray())
return replaceRules.size
}

@ -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,12 +74,18 @@ class ReplaceEditDialog : DialogFragment(),
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_save -> {
viewModel.save(getReplaceRule()) {
val rule = getReplaceRule();
if (!rule.isValid()){
toast(R.string.replace_rule_invalid)
}
else{
viewModel.save(rule) {
callBack?.onReplaceRuleSave()
dismiss()
}
}
}
}
return true
}

@ -204,6 +204,7 @@
<string name="clear_all_content">清除缓存会删除所有已保存章节,是否确认删除?</string>
<string name="book_source_share_url">书源共享</string>
<string name="replace_rule_summary">替换规则名称</string>
<string name="replace_rule_invalid">替换规则为空或者不满足正则表达式要求</string>
<string name="select_action">选择操作</string>
<string name="select_all">全选</string>
<string name="select_all_count">全选(%d/%d)</string>

Loading…
Cancel
Save