mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -14,6 +14,8 @@ import io.legado.app.databinding.ActivityReplaceEditBinding
|
||||
import io.legado.app.lib.dialogs.SelectItem
|
||||
import io.legado.app.ui.widget.dialog.TextDialog
|
||||
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
|
||||
@@ -68,16 +70,10 @@ class ReplaceEditActivity :
|
||||
|
||||
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
R.id.menu_save -> {
|
||||
val rule = getReplaceRule()
|
||||
if (!rule.isValid()) {
|
||||
toastOnUi(R.string.replace_rule_invalid)
|
||||
} else {
|
||||
viewModel.save(rule) {
|
||||
setResult(RESULT_OK)
|
||||
finish()
|
||||
}
|
||||
}
|
||||
R.id.menu_save -> saveRule()
|
||||
R.id.menu_copy_rule -> sendToClip(GSON.toJson(getReplaceRule()))
|
||||
R.id.menu_paste_rule -> viewModel.pasteRule {
|
||||
upReplaceView(it)
|
||||
}
|
||||
}
|
||||
return true
|
||||
@@ -114,6 +110,18 @@ 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")
|
||||
|
||||
@@ -5,6 +5,9 @@ import android.content.Intent
|
||||
import io.legado.app.base.BaseViewModel
|
||||
import io.legado.app.data.appDb
|
||||
import io.legado.app.data.entities.ReplaceRule
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.utils.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
class ReplaceEditViewModel(application: Application) : BaseViewModel(application) {
|
||||
|
||||
@@ -33,6 +36,22 @@ class ReplaceEditViewModel(application: Application) : BaseViewModel(application
|
||||
}
|
||||
}
|
||||
|
||||
fun pasteRule(success: (ReplaceRule) -> Unit) {
|
||||
execute(context = Dispatchers.Main) {
|
||||
val text = context.getClipText()
|
||||
if (text.isNullOrBlank()) {
|
||||
throw NoStackTraceException("剪贴板为空")
|
||||
}
|
||||
GSON.fromJsonObject<ReplaceRule>(text).getOrNull()
|
||||
?: throw NoStackTraceException("格式不对")
|
||||
}.onSuccess {
|
||||
success.invoke(it)
|
||||
}.onError {
|
||||
context.toastOnUi(it.localizedMessage ?: "Error")
|
||||
it.printOnDebug()
|
||||
}
|
||||
}
|
||||
|
||||
fun save(replaceRule: ReplaceRule, success: () -> Unit) {
|
||||
execute {
|
||||
if (replaceRule.order == Int.MIN_VALUE) {
|
||||
|
||||
@@ -1,11 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<menu xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
tools:ignore="AlwaysShowAction">
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_save"
|
||||
android:icon="@drawable/ic_save"
|
||||
android:title="@string/action_save"
|
||||
app:showAsAction="ifRoom" />
|
||||
app:showAsAction="always" />
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_copy_rule"
|
||||
android:title="@string/copy_rule"
|
||||
app:showAsAction="never" />
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_paste_rule"
|
||||
android:title="@string/paste_rule"
|
||||
app:showAsAction="never" />
|
||||
|
||||
</menu>
|
||||
@@ -1048,4 +1048,6 @@
|
||||
<string name="wake_lock">唤醒锁</string>
|
||||
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
|
||||
<string name="change_search_scope">切换搜索范围</string>
|
||||
<string name="copy_rule">拷贝规则</string>
|
||||
<string name="paste_rule">粘贴规则</string>
|
||||
</resources>
|
||||
|
||||
@@ -1051,4 +1051,6 @@
|
||||
<string name="wake_lock">唤醒锁</string>
|
||||
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
|
||||
<string name="change_search_scope">切换搜索范围</string>
|
||||
<string name="copy_rule">拷贝规则</string>
|
||||
<string name="paste_rule">粘贴规则</string>
|
||||
</resources>
|
||||
|
||||
@@ -1051,4 +1051,6 @@
|
||||
<string name="wake_lock">唤醒锁</string>
|
||||
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
|
||||
<string name="change_search_scope">切换搜索范围</string>
|
||||
<string name="copy_rule">拷贝规则</string>
|
||||
<string name="paste_rule">粘贴规则</string>
|
||||
</resources>
|
||||
|
||||
@@ -1048,4 +1048,6 @@
|
||||
<string name="wake_lock">唤醒锁</string>
|
||||
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
|
||||
<string name="change_search_scope">切换搜索范围</string>
|
||||
<string name="copy_rule">拷贝规则</string>
|
||||
<string name="paste_rule">粘贴规则</string>
|
||||
</resources>
|
||||
|
||||
@@ -1050,4 +1050,6 @@
|
||||
<string name="wake_lock">唤醒锁</string>
|
||||
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
|
||||
<string name="change_search_scope">切换搜索范围</string>
|
||||
<string name="copy_rule">拷贝规则</string>
|
||||
<string name="paste_rule">粘贴规则</string>
|
||||
</resources>
|
||||
|
||||
@@ -1050,4 +1050,6 @@
|
||||
<string name="wake_lock">唤醒锁</string>
|
||||
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
|
||||
<string name="change_search_scope">切换搜索范围</string>
|
||||
<string name="copy_rule">拷贝规则</string>
|
||||
<string name="paste_rule">粘贴规则</string>
|
||||
</resources>
|
||||
|
||||
@@ -1051,4 +1051,6 @@
|
||||
<string name="wake_lock">唤醒锁</string>
|
||||
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
|
||||
<string name="change_search_scope">切换搜索范围</string>
|
||||
<string name="copy_rule">拷贝规则</string>
|
||||
<string name="paste_rule">粘贴规则</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user