pull/2477/head
kunfei 2 years ago
parent 3e5a84f2bd
commit 0f074266e7
  1. 45
      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.appDb
import io.legado.app.data.entities.TxtTocRule import io.legado.app.data.entities.TxtTocRule
import io.legado.app.databinding.DialogTocRegexEditBinding import io.legado.app.databinding.DialogTocRegexEditBinding
import io.legado.app.exception.NoStackTraceException
import io.legado.app.lib.theme.primaryColor import io.legado.app.lib.theme.primaryColor
import io.legado.app.utils.GSON import io.legado.app.utils.*
import io.legado.app.utils.applyTint
import io.legado.app.utils.sendToClip
import io.legado.app.utils.setLayout
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Dispatchers
class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit, true), class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit, true),
Toolbar.OnMenuItemClickListener { Toolbar.OnMenuItemClickListener {
@ -43,9 +42,7 @@ class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit
binding.toolBar.setBackgroundColor(primaryColor) binding.toolBar.setBackgroundColor(primaryColor)
initMenu() initMenu()
viewModel.initData(arguments?.getLong("id")) { viewModel.initData(arguments?.getLong("id")) {
binding.tvRuleName.setText(it?.name) upRuleView(it)
binding.tvRuleRegex.setText(it?.rule)
binding.tvRuleExample.setText(it?.example)
} }
} }
@ -62,11 +59,19 @@ class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit
dismissAllowingStateLoss() dismissAllowingStateLoss()
} }
R.id.menu_copy_rule -> context?.sendToClip(GSON.toJson(getRuleFromView())) 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 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 { private fun getRuleFromView(): TxtTocRule {
val tocRule = viewModel.tocRule ?: TxtTocRule().apply { val tocRule = viewModel.tocRule ?: TxtTocRule().apply {
viewModel.tocRule = this viewModel.tocRule = this
@ -84,19 +89,31 @@ class TxtTocRuleEditDialog() : BaseDialogFragment(R.layout.dialog_toc_regex_edit
var tocRule: TxtTocRule? = null var tocRule: TxtTocRule? = null
fun initData(id: Long?, finally: (tocRule: TxtTocRule?) -> Unit) { fun initData(id: Long?, finally: (tocRule: TxtTocRule?) -> Unit) {
if (tocRule != null) return
execute { execute {
tocRule?.let { if (id == null) return@execute
return@execute
}
if (id == null) {
return@execute
}
tocRule = appDb.txtTocRuleDao.get(id) tocRule = appDb.txtTocRuleDao.get(id)
}.onFinally { }.onFinally {
finally.invoke(tocRule) finally.invoke(tocRule)
} }
} }
fun pasteRule(success: (TxtTocRule) -> Unit) {
execute(context = Dispatchers.Main) {
val text = context.getClipText()
if (text.isNullOrBlank()) {
throw NoStackTraceException("剪贴板为空")
}
GSON.fromJsonObject<TxtTocRule>(text).getOrNull()
?: throw NoStackTraceException("格式不对")
}.onSuccess {
success.invoke(it)
}.onError {
context.toastOnUi(it.localizedMessage ?: "Error")
it.printOnDebug()
}
}
} }
interface Callback { interface Callback {

Loading…
Cancel
Save