diff --git a/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt index db469fd97..3690a3701 100644 --- a/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt @@ -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> { return arrayListOf( SelectItem("正则教程", "regexHelp") diff --git a/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditViewModel.kt index 8e5f963fd..ee89f5c7a 100644 --- a/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditViewModel.kt @@ -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(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) { diff --git a/app/src/main/res/menu/replace_edit.xml b/app/src/main/res/menu/replace_edit.xml index 652d0076e..bfd386fa1 100644 --- a/app/src/main/res/menu/replace_edit.xml +++ b/app/src/main/res/menu/replace_edit.xml @@ -1,11 +1,23 @@ - + + app:showAsAction="always" /> + + + + \ No newline at end of file diff --git a/app/src/main/res/values-es-rES/strings.xml b/app/src/main/res/values-es-rES/strings.xml index 28ccd97fa..383c89696 100644 --- a/app/src/main/res/values-es-rES/strings.xml +++ b/app/src/main/res/values-es-rES/strings.xml @@ -1048,4 +1048,6 @@ 唤醒锁 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 + 拷贝规则 + 粘贴规则 diff --git a/app/src/main/res/values-ja-rJP/strings.xml b/app/src/main/res/values-ja-rJP/strings.xml index 6f4acf9c4..0171f04c9 100644 --- a/app/src/main/res/values-ja-rJP/strings.xml +++ b/app/src/main/res/values-ja-rJP/strings.xml @@ -1051,4 +1051,6 @@ 唤醒锁 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 + 拷贝规则 + 粘贴规则 diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index d456b76ca..43865743e 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -1051,4 +1051,6 @@ 唤醒锁 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 + 拷贝规则 + 粘贴规则 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index f5cc7eb15..d4aba53a4 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -1048,4 +1048,6 @@ 唤醒锁 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 + 拷贝规则 + 粘贴规则 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index b175a7d82..bbc2205e2 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -1050,4 +1050,6 @@ 唤醒锁 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 + 拷贝规则 + 粘贴规则 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 63c4cb030..15e20d7e9 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -1050,4 +1050,6 @@ 唤醒锁 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 + 拷贝规则 + 粘贴规则 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5e0fe4896..4cc8d3603 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1051,4 +1051,6 @@ 唤醒锁 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 + 拷贝规则 + 粘贴规则