From 2a3f78d43e20e22686ef3fdf35fbf56f85478268 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sat, 26 Nov 2022 22:15:48 +0530 Subject: feat: add `TextInputDialog` for generic text input dialogs --- .../passwordstore/ui/dialogs/TextInputDialog.kt | 66 ++++++++++++++++++++++ app/src/main/res/layout/dialog_text_input.xml | 25 ++++++++ 2 files changed, 91 insertions(+) create mode 100644 app/src/main/java/app/passwordstore/ui/dialogs/TextInputDialog.kt create mode 100644 app/src/main/res/layout/dialog_text_input.xml diff --git a/app/src/main/java/app/passwordstore/ui/dialogs/TextInputDialog.kt b/app/src/main/java/app/passwordstore/ui/dialogs/TextInputDialog.kt new file mode 100644 index 00000000..b0eb1aeb --- /dev/null +++ b/app/src/main/java/app/passwordstore/ui/dialogs/TextInputDialog.kt @@ -0,0 +1,66 @@ +package app.passwordstore.ui.dialogs + +import android.app.Dialog +import android.content.DialogInterface +import android.os.Bundle +import android.view.WindowManager +import androidx.core.os.bundleOf +import androidx.fragment.app.DialogFragment +import androidx.fragment.app.setFragmentResult +import app.passwordstore.databinding.DialogTextInputBinding +import app.passwordstore.util.extensions.finish +import app.passwordstore.util.extensions.unsafeLazy +import com.google.android.material.dialog.MaterialAlertDialogBuilder + +/** + * General purpose [DialogFragment] that can be used to accept a single text input. It provides a + * configurable title and text input hint through the instantiation helper at + * [TextInputDialog.newInstance]. Typical usage would look something like this: + * + * ```kotlin + * val dialog = TextInputDialog.newInstance(getString(R.string.dialog_title), getString(R.string.dialog_hint)) + * dialog.show(supportFragmentManager, "text_input_dialog") + * dialog.setFragmentResultListener(TextInputDialog.REQUEST_KEY) { _, bundle -> + * doSomething(bundle.getString(TextInputDialog.BUNDLE_KEY_TEXT)) + * } + * ``` + */ +class TextInputDialog : DialogFragment() { + private val binding by unsafeLazy { DialogTextInputBinding.inflate(layoutInflater) } + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val builder = MaterialAlertDialogBuilder(requireContext()) + builder.setView(binding.root) + builder.setTitle(arguments?.getString(BUNDLE_KEY_TITLE)) + arguments?.getString(BUNDLE_KEY_HINT)?.let { hint -> + binding.textInputLayout.isHintEnabled = true + binding.textInputLayout.isHintAnimationEnabled = true + binding.textInputLayout.hint = hint + } + builder.setPositiveButton(android.R.string.ok) { dialogInterface, _ -> + setFragmentResult(REQUEST_KEY, bundleOf(BUNDLE_KEY_TEXT to binding.editText.text.toString())) + dialogInterface.dismiss() + } + val dialog = builder.create() + dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) + return dialog + } + + override fun onCancel(dialog: DialogInterface) { + super.onCancel(dialog) + finish() + } + companion object { + const val REQUEST_KEY = "text_input_dialog" + const val BUNDLE_KEY_TEXT = "text" + private const val BUNDLE_KEY_TITLE = "title" + private const val BUNDLE_KEY_HINT = "hint" + + fun newInstance(title: String, hint: String? = null): TextInputDialog { + val args = bundleOf(BUNDLE_KEY_TITLE to title, BUNDLE_KEY_HINT to hint) + val dialog = TextInputDialog() + dialog.arguments = args + return dialog + } + } +} diff --git a/app/src/main/res/layout/dialog_text_input.xml b/app/src/main/res/layout/dialog_text_input.xml new file mode 100644 index 00000000..63660385 --- /dev/null +++ b/app/src/main/res/layout/dialog_text_input.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + -- cgit v1.2.3