mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -6,7 +6,10 @@ import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import io.legado.app.R
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import splitties.init.appCtx
|
||||
import java.util.regex.Pattern
|
||||
import java.util.regex.PatternSyntaxException
|
||||
|
||||
@@ -85,6 +88,12 @@ data class ReplaceRule(
|
||||
return true
|
||||
}
|
||||
|
||||
fun checkValid() {
|
||||
if (!isValid()) {
|
||||
throw NoStackTraceException(appCtx.getString(R.string.replace_rule_invalid))
|
||||
}
|
||||
}
|
||||
|
||||
fun getValidTimeoutMillisecond(): Long {
|
||||
if (timeoutMillisecond <= 0) {
|
||||
return 3000L
|
||||
|
||||
@@ -17,7 +17,6 @@ import io.legado.app.ui.widget.keyboard.KeyboardToolPop
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.sendToClip
|
||||
import io.legado.app.utils.showDialogFragment
|
||||
import io.legado.app.utils.toastOnUi
|
||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||
|
||||
/**
|
||||
@@ -70,7 +69,10 @@ class ReplaceEditActivity :
|
||||
|
||||
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
R.id.menu_save -> saveRule()
|
||||
R.id.menu_save -> viewModel.save(getReplaceRule()) {
|
||||
setResult(RESULT_OK)
|
||||
finish()
|
||||
}
|
||||
R.id.menu_copy_rule -> sendToClip(GSON.toJson(getReplaceRule()))
|
||||
R.id.menu_paste_rule -> viewModel.pasteRule {
|
||||
upReplaceView(it)
|
||||
@@ -110,18 +112,6 @@ class ReplaceEditActivity :
|
||||
return replaceRule
|
||||
}
|
||||
|
||||
private fun saveRule() {
|
||||
val rule = getReplaceRule()
|
||||
if (!rule.isValid()) {
|
||||
toastOnUi(R.string.replace_rule_invalid)
|
||||
} else {
|
||||
viewModel.save(rule) {
|
||||
setResult(RESULT_OK)
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun helpActions(): List<SelectItem<String>> {
|
||||
return arrayListOf(
|
||||
SelectItem("正则教程", "regexHelp")
|
||||
|
||||
@@ -54,12 +54,15 @@ class ReplaceEditViewModel(application: Application) : BaseViewModel(application
|
||||
|
||||
fun save(replaceRule: ReplaceRule, success: () -> Unit) {
|
||||
execute {
|
||||
replaceRule.checkValid()
|
||||
if (replaceRule.order == Int.MIN_VALUE) {
|
||||
replaceRule.order = appDb.replaceRuleDao.maxOrder + 1
|
||||
}
|
||||
appDb.replaceRuleDao.insert(replaceRule)
|
||||
}.onSuccess {
|
||||
success()
|
||||
}.onError {
|
||||
context.toastOnUi("save error, ${it.localizedMessage}")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user