summaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2022-11-26 22:15:48 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2022-11-27 13:28:45 +0530
commit2a3f78d43e20e22686ef3fdf35fbf56f85478268 (patch)
tree9c7045098a88ddc97a0f2601783d2ac517369475 /app/src/main/java
parent736022f8148c06f515fecf10b47587071ede9bae (diff)
feat: add `TextInputDialog` for generic text input dialogs
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/app/passwordstore/ui/dialogs/TextInputDialog.kt66
1 files changed, 66 insertions, 0 deletions
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
+ }
+ }
+}