diff --git a/app/src/main/java/io/legado/app/ui/widget/dialog/CodeDialog.kt b/app/src/main/java/io/legado/app/ui/widget/dialog/CodeDialog.kt index 6a6c08536..5ff8ab53b 100644 --- a/app/src/main/java/io/legado/app/ui/widget/dialog/CodeDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/dialog/CodeDialog.kt @@ -9,14 +9,15 @@ import io.legado.app.lib.theme.primaryColor import io.legado.app.ui.widget.code.addJsPattern import io.legado.app.ui.widget.code.addJsonPattern import io.legado.app.ui.widget.code.addLegadoPattern +import io.legado.app.utils.disableEdit import io.legado.app.utils.setLayout import io.legado.app.utils.viewbindingdelegate.viewBinding class CodeDialog() : BaseDialogFragment(R.layout.dialog_code_view) { - constructor(code: String) : this() { + constructor(code: String, disableEdit: Boolean = true) : this() { arguments = Bundle().apply { - + putBoolean("disableEdit", disableEdit) putString("code", code) } } @@ -30,6 +31,11 @@ class CodeDialog() : BaseDialogFragment(R.layout.dialog_code_view) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { binding.toolBar.setBackgroundColor(primaryColor) + if (arguments?.getBoolean("disableEdit") == true) { + binding.codeView.disableEdit() + } else { + initMenu() + } binding.codeView.addLegadoPattern() binding.codeView.addJsonPattern() binding.codeView.addJsPattern() @@ -38,4 +44,24 @@ class CodeDialog() : BaseDialogFragment(R.layout.dialog_code_view) { } } + private fun initMenu() { + binding.toolBar.inflateMenu(R.menu.code_edit) + binding.toolBar.setOnMenuItemClickListener { + when (it.itemId) { + R.id.menu_save -> binding.codeView.text?.toString()?.let { code -> + (parentFragment as? Callback)?.saveCode(code) + ?: (activity as? Callback)?.saveCode(code) + } + } + return@setOnMenuItemClickListener true + } + } + + + interface Callback { + + fun saveCode(code: String) + + } + } \ No newline at end of file diff --git a/app/src/main/res/menu/code_edit.xml b/app/src/main/res/menu/code_edit.xml new file mode 100644 index 000000000..05397ae61 --- /dev/null +++ b/app/src/main/res/menu/code_edit.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file