Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Oct 23, 2024
1 parent 1c0a7bc commit 003ffeb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
9 changes: 8 additions & 1 deletion app/src/main/java/io/legado/app/model/localBook/TextFile.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.legado.app.model.localBook

import io.legado.app.constant.AppLog
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
Expand All @@ -14,6 +15,7 @@ import java.io.FileNotFoundException
import java.nio.charset.Charset
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException
import kotlin.math.min

class TextFile(private var book: Book) {
Expand Down Expand Up @@ -395,7 +397,12 @@ class TextFile(private var book: Book) {
var maxCs = 1
var tocPattern: Pattern? = null
for (tocRule in rules) {
val pattern = tocRule.rule.toPattern(Pattern.MULTILINE)
val pattern = try {
tocRule.rule.toPattern(Pattern.MULTILINE)
} catch (e: PatternSyntaxException) {
AppLog.put("TXT目录规则正则语法错误:${tocRule.name}\n$e", e)
continue
}
val matcher = pattern.matcher(content)
var cs = 0
while (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.fragment.app.viewModels
import io.legado.app.R
import io.legado.app.base.BaseDialogFragment
import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppLog
import io.legado.app.data.appDb
import io.legado.app.data.entities.TxtTocRule
import io.legado.app.databinding.DialogTocRegexEditBinding
Expand All @@ -18,6 +19,8 @@ import io.legado.app.lib.theme.primaryColor
import io.legado.app.utils.*
import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Dispatchers
import java.util.regex.Pattern
import java.util.regex.PatternSyntaxException

class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit, true),
Toolbar.OnMenuItemClickListener {
Expand Down Expand Up @@ -55,8 +58,11 @@ class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_save -> {
callback?.saveTxtTocRule(getRuleFromView())
dismissAllowingStateLoss()
val tocRule = getRuleFromView()
if (checkValid(tocRule)) {
callback?.saveTxtTocRule(getRuleFromView())
dismissAllowingStateLoss()
}
}
R.id.menu_copy_rule -> context?.sendToClip(GSON.toJson(getRuleFromView()))
R.id.menu_paste_rule -> viewModel.pasteRule {
Expand All @@ -66,6 +72,22 @@ class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit
return true
}

private fun checkValid(tocRule: TxtTocRule): Boolean {
if (tocRule.name.isEmpty()) {
toastOnUi("名称不能为空")
return false
}

try {
Pattern.compile(tocRule.rule, Pattern.MULTILINE)
} catch (ex: PatternSyntaxException) {
AppLog.put("正则语法错误或不支持(txt):${ex.localizedMessage}", ex, true)
return false
}

return true
}

private fun upRuleView(tocRule: TxtTocRule?) {
binding.tvRuleName.setText(tocRule?.name)
binding.tvRuleRegex.setText(tocRule?.rule)
Expand Down

0 comments on commit 003ffeb

Please sign in to comment.