From 0f074266e758089ad32752132e0a71e490451c2f Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 24 Oct 2022 12:28:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/book/toc/rule/TxtTocRuleEditDialog.kt | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/toc/rule/TxtTocRuleEditDialog.kt b/app/src/main/java/io/legado/app/ui/book/toc/rule/TxtTocRuleEditDialog.kt index c6506c456..1a00134c3 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/rule/TxtTocRuleEditDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/rule/TxtTocRuleEditDialog.kt @@ -13,12 +13,11 @@ import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb import io.legado.app.data.entities.TxtTocRule import io.legado.app.databinding.DialogTocRegexEditBinding +import io.legado.app.exception.NoStackTraceException import io.legado.app.lib.theme.primaryColor -import io.legado.app.utils.GSON -import io.legado.app.utils.applyTint -import io.legado.app.utils.sendToClip -import io.legado.app.utils.setLayout +import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.Dispatchers class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit, true), Toolbar.OnMenuItemClickListener { @@ -43,9 +42,7 @@ class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit binding.toolBar.setBackgroundColor(primaryColor) initMenu() viewModel.initData(arguments?.getLong("id")) { - binding.tvRuleName.setText(it?.name) - binding.tvRuleRegex.setText(it?.rule) - binding.tvRuleExample.setText(it?.example) + upRuleView(it) } } @@ -62,11 +59,19 @@ class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit dismissAllowingStateLoss() } R.id.menu_copy_rule -> context?.sendToClip(GSON.toJson(getRuleFromView())) - R.id.menu_paste_rule -> {} + R.id.menu_paste_rule -> viewModel.pasteRule { + upRuleView(it) + } } return true } + private fun upRuleView(tocRule: TxtTocRule?) { + binding.tvRuleName.setText(tocRule?.name) + binding.tvRuleRegex.setText(tocRule?.rule) + binding.tvRuleExample.setText(tocRule?.example) + } + private fun getRuleFromView(): TxtTocRule { val tocRule = viewModel.tocRule ?: TxtTocRule().apply { viewModel.tocRule = this @@ -84,19 +89,31 @@ class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit var tocRule: TxtTocRule? = null fun initData(id: Long?, finally: (tocRule: TxtTocRule?) -> Unit) { + if (tocRule != null) return execute { - tocRule?.let { - return@execute - } - if (id == null) { - return@execute - } + if (id == null) return@execute tocRule = appDb.txtTocRuleDao.get(id) }.onFinally { finally.invoke(tocRule) } } + fun pasteRule(success: (TxtTocRule) -> 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() + } + } + } interface Callback {