pull/2468/head
kunfei 2 years ago
parent 710adb58ee
commit dcce2c1aea
  1. 28
      app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditActivity.kt
  2. 19
      app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditViewModel.kt
  3. 18
      app/src/main/res/menu/replace_edit.xml
  4. 2
      app/src/main/res/values-es-rES/strings.xml
  5. 2
      app/src/main/res/values-ja-rJP/strings.xml
  6. 2
      app/src/main/res/values-pt-rBR/strings.xml
  7. 2
      app/src/main/res/values-zh-rHK/strings.xml
  8. 2
      app/src/main/res/values-zh-rTW/strings.xml
  9. 2
      app/src/main/res/values-zh/strings.xml
  10. 2
      app/src/main/res/values/strings.xml

@ -14,6 +14,8 @@ import io.legado.app.databinding.ActivityReplaceEditBinding
import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.SelectItem
import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.ui.widget.dialog.TextDialog
import io.legado.app.ui.widget.keyboard.KeyboardToolPop 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.showDialogFragment
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
@ -68,16 +70,10 @@ class ReplaceEditActivity :
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.menu_save -> { R.id.menu_save -> saveRule()
val rule = getReplaceRule() R.id.menu_copy_rule -> sendToClip(GSON.toJson(getReplaceRule()))
if (!rule.isValid()) { R.id.menu_paste_rule -> viewModel.pasteRule {
toastOnUi(R.string.replace_rule_invalid) upReplaceView(it)
} else {
viewModel.save(rule) {
setResult(RESULT_OK)
finish()
}
}
} }
} }
return true return true
@ -114,6 +110,18 @@ class ReplaceEditActivity :
return replaceRule 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>> { override fun helpActions(): List<SelectItem<String>> {
return arrayListOf( return arrayListOf(
SelectItem("正则教程", "regexHelp") SelectItem("正则教程", "regexHelp")

@ -5,6 +5,9 @@ import android.content.Intent
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.ReplaceRule 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) { 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) { fun save(replaceRule: ReplaceRule, success: () -> Unit) {
execute { execute {
if (replaceRule.order == Int.MIN_VALUE) { if (replaceRule.order == Int.MIN_VALUE) {

@ -1,11 +1,23 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:ignore="AlwaysShowAction">
<item <item
android:id="@+id/menu_save" android:id="@+id/menu_save"
android:icon="@drawable/ic_save" android:icon="@drawable/ic_save"
android:title="@string/action_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> </menu>

@ -1048,4 +1048,6 @@
<string name="wake_lock">唤醒锁</string> <string name="wake_lock">唤醒锁</string>
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string> <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
<string name="change_search_scope">切换搜索范围</string> <string name="change_search_scope">切换搜索范围</string>
<string name="copy_rule">拷贝规则</string>
<string name="paste_rule">粘贴规则</string>
</resources> </resources>

@ -1051,4 +1051,6 @@
<string name="wake_lock">唤醒锁</string> <string name="wake_lock">唤醒锁</string>
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string> <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
<string name="change_search_scope">切换搜索范围</string> <string name="change_search_scope">切换搜索范围</string>
<string name="copy_rule">拷贝规则</string>
<string name="paste_rule">粘贴规则</string>
</resources> </resources>

@ -1051,4 +1051,6 @@
<string name="wake_lock">唤醒锁</string> <string name="wake_lock">唤醒锁</string>
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string> <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
<string name="change_search_scope">切换搜索范围</string> <string name="change_search_scope">切换搜索范围</string>
<string name="copy_rule">拷贝规则</string>
<string name="paste_rule">粘贴规则</string>
</resources> </resources>

@ -1048,4 +1048,6 @@
<string name="wake_lock">唤醒锁</string> <string name="wake_lock">唤醒锁</string>
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string> <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
<string name="change_search_scope">切换搜索范围</string> <string name="change_search_scope">切换搜索范围</string>
<string name="copy_rule">拷贝规则</string>
<string name="paste_rule">粘贴规则</string>
</resources> </resources>

@ -1050,4 +1050,6 @@
<string name="wake_lock">唤醒锁</string> <string name="wake_lock">唤醒锁</string>
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string> <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
<string name="change_search_scope">切换搜索范围</string> <string name="change_search_scope">切换搜索范围</string>
<string name="copy_rule">拷贝规则</string>
<string name="paste_rule">粘贴规则</string>
</resources> </resources>

@ -1050,4 +1050,6 @@
<string name="wake_lock">唤醒锁</string> <string name="wake_lock">唤醒锁</string>
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string> <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
<string name="change_search_scope">切换搜索范围</string> <string name="change_search_scope">切换搜索范围</string>
<string name="copy_rule">拷贝规则</string>
<string name="paste_rule">粘贴规则</string>
</resources> </resources>

@ -1051,4 +1051,6 @@
<string name="wake_lock">唤醒锁</string> <string name="wake_lock">唤醒锁</string>
<string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string> <string name="wake_lock_summary">开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台</string>
<string name="change_search_scope">切换搜索范围</string> <string name="change_search_scope">切换搜索范围</string>
<string name="copy_rule">拷贝规则</string>
<string name="paste_rule">粘贴规则</string>
</resources> </resources>

Loading…
Cancel
Save