feat: add TextInputDialog for generic text input dialogs

This commit is contained in:
Harsh Shandilya 2022-11-26 22:15:48 +05:30
parent 736022f814
commit 2a3f78d43e
No known key found for this signature in database
2 changed files with 91 additions and 0 deletions

View file

@ -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
}
}
}

View file

@ -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>