优化
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package io.legado.app.model.localBook
|
package io.legado.app.model.localBook
|
||||||
|
|
||||||
|
import io.legado.app.constant.AppLog
|
||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
import io.legado.app.data.entities.Book
|
import io.legado.app.data.entities.Book
|
||||||
import io.legado.app.data.entities.BookChapter
|
import io.legado.app.data.entities.BookChapter
|
||||||
@@ -14,6 +15,7 @@ import java.io.FileNotFoundException
|
|||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
import java.util.regex.Matcher
|
import java.util.regex.Matcher
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
import java.util.regex.PatternSyntaxException
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
|
||||||
class TextFile(private var book: Book) {
|
class TextFile(private var book: Book) {
|
||||||
@@ -395,7 +397,12 @@ class TextFile(private var book: Book) {
|
|||||||
var maxCs = 1
|
var maxCs = 1
|
||||||
var tocPattern: Pattern? = null
|
var tocPattern: Pattern? = null
|
||||||
for (tocRule in rules) {
|
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)
|
val matcher = pattern.matcher(content)
|
||||||
var cs = 0
|
var cs = 0
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import androidx.fragment.app.viewModels
|
|||||||
import io.legado.app.R
|
import io.legado.app.R
|
||||||
import io.legado.app.base.BaseDialogFragment
|
import io.legado.app.base.BaseDialogFragment
|
||||||
import io.legado.app.base.BaseViewModel
|
import io.legado.app.base.BaseViewModel
|
||||||
|
import io.legado.app.constant.AppLog
|
||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
import io.legado.app.data.entities.TxtTocRule
|
import io.legado.app.data.entities.TxtTocRule
|
||||||
import io.legado.app.databinding.DialogTocRegexEditBinding
|
import io.legado.app.databinding.DialogTocRegexEditBinding
|
||||||
@@ -18,6 +19,8 @@ import io.legado.app.lib.theme.primaryColor
|
|||||||
import io.legado.app.utils.*
|
import io.legado.app.utils.*
|
||||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import java.util.regex.Pattern
|
||||||
|
import java.util.regex.PatternSyntaxException
|
||||||
|
|
||||||
class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit, true),
|
class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit, true),
|
||||||
Toolbar.OnMenuItemClickListener {
|
Toolbar.OnMenuItemClickListener {
|
||||||
@@ -55,8 +58,11 @@ class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit
|
|||||||
override fun onMenuItemClick(item: MenuItem?): Boolean {
|
override fun onMenuItemClick(item: MenuItem?): Boolean {
|
||||||
when (item?.itemId) {
|
when (item?.itemId) {
|
||||||
R.id.menu_save -> {
|
R.id.menu_save -> {
|
||||||
callback?.saveTxtTocRule(getRuleFromView())
|
val tocRule = getRuleFromView()
|
||||||
dismissAllowingStateLoss()
|
if (checkValid(tocRule)) {
|
||||||
|
callback?.saveTxtTocRule(getRuleFromView())
|
||||||
|
dismissAllowingStateLoss()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
R.id.menu_copy_rule -> context?.sendToClip(GSON.toJson(getRuleFromView()))
|
R.id.menu_copy_rule -> context?.sendToClip(GSON.toJson(getRuleFromView()))
|
||||||
R.id.menu_paste_rule -> viewModel.pasteRule {
|
R.id.menu_paste_rule -> viewModel.pasteRule {
|
||||||
@@ -66,6 +72,22 @@ class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit
|
|||||||
return true
|
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?) {
|
private fun upRuleView(tocRule: TxtTocRule?) {
|
||||||
binding.tvRuleName.setText(tocRule?.name)
|
binding.tvRuleName.setText(tocRule?.name)
|
||||||
binding.tvRuleRegex.setText(tocRule?.rule)
|
binding.tvRuleRegex.setText(tocRule?.rule)
|
||||||
|
|||||||
Reference in New Issue
Block a user