summaryrefslogtreecommitdiff
path: root/app/src
diff options
context:
space:
mode:
Diffstat (limited to 'app/src')
-rw-r--r--app/src/main/java/app/passwordstore/ui/dialogs/TextInputDialog.kt66
-rw-r--r--app/src/main/res/layout/dialog_text_input.xml25
2 files changed, 91 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
+ }
+ }
+}
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 @@
+<?xml version="1.0" encoding="utf-8"?><!--
+ ~ Copyright © 2022 The Android Password Store Authors. All Rights Reserved.
+ ~ SPDX-License-Identifier: GPL-3.0-only
+ -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical"
+ android:padding="@dimen/activity_horizontal_margin">
+
+ <com.google.android.material.textfield.TextInputLayout
+ android:id="@+id/text_input_layout"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content">
+
+ <com.google.android.material.textfield.TextInputEditText
+ android:id="@+id/edit_text"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content" />
+
+ <requestFocus />
+ </com.google.android.material.textfield.TextInputLayout>
+
+</LinearLayout>