Dialog input much better

This commit is contained in:
J-Jamet
2020-06-10 17:52:35 +02:00
parent ade9af9ecd
commit 9e542d0bbe
3 changed files with 70 additions and 13 deletions

View File

@@ -20,6 +20,8 @@
package com.kunzisoft.keepass.settings.preferencedialogfragment
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.kunzisoft.keepass.R
@@ -47,10 +49,20 @@ abstract class AutofillBlocklistPreferenceDialogFragmentCompat
addSearchInfo(searchInfoString)
}
setOnInputTextEditorActionListener(TextView.OnEditorActionListener { _, actionId, _ ->
when (actionId) {
EditorInfo.IME_ACTION_DONE -> {
addItemFromInputText()
hideKeyboard()
false
}
else -> false
}
})
val addItemButton = view.findViewById<View>(R.id.add_item_button)
addItemButton?.setOnClickListener {
addSearchInfo(inputText)
filterAdapter?.replaceItems(persistedItems.toList())
addItemFromInputText()
}
val recyclerView = view.findViewById<RecyclerView>(R.id.pref_dialog_list)
@@ -64,10 +76,23 @@ abstract class AutofillBlocklistPreferenceDialogFragmentCompat
}
}
private fun addSearchInfo(searchInfoString: String) {
private fun addSearchInfo(searchInfoString: String): Boolean {
val itemToAdd = buildSearchInfoFromString(searchInfoString)
if (itemToAdd != null && !itemToAdd.containsOnlyNullValues())
return if (itemToAdd != null && !itemToAdd.containsOnlyNullValues()) {
persistedItems.add(itemToAdd)
true
} else {
false
}
}
private fun addItemFromInputText() {
if (addSearchInfo(inputText)) {
inputText = ""
} else {
setInputTextError(getString(R.string.error_string_type))
}
filterAdapter?.replaceItems(persistedItems.toList())
}
override fun onItemDeleted(item: SearchInfo) {

View File

@@ -21,10 +21,12 @@ package com.kunzisoft.keepass.settings.preferencedialogfragment
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.CompoundButton
import android.widget.EditText
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceDialogFragmentCompat
import com.kunzisoft.keepass.R
@@ -34,6 +36,8 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
private var textExplanationView: TextView? = null
private var switchElementView: CompoundButton? = null
private var mOnInputTextEditorActionListener: TextView.OnEditorActionListener? = null
var inputText: String
get() = this.inputTextView?.text?.toString() ?: ""
set(inputText) {
@@ -43,6 +47,14 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
}
}
fun setInputTextError(error: CharSequence) {
this.inputTextView?.error = error
}
fun setOnInputTextEditorActionListener(onEditorActionListener: TextView.OnEditorActionListener) {
this.mOnInputTextEditorActionListener = onEditorActionListener
}
var explanationText: String?
get() = textExplanationView?.text?.toString() ?: ""
set(explanationText) {
@@ -63,16 +75,21 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
inputTextView = view.findViewById(R.id.input_text)
inputTextView?.apply {
imeOptions = EditorInfo.IME_ACTION_DONE
setOnEditorActionListener { _, actionId, _ ->
when (actionId) {
EditorInfo.IME_ACTION_DONE -> {
onDialogClosed(true)
dialog?.dismiss()
true
}
else -> {
false
setOnEditorActionListener { v, actionId, event ->
if (mOnInputTextEditorActionListener == null) {
when (actionId) {
EditorInfo.IME_ACTION_DONE -> {
onDialogClosed(true)
dialog?.dismiss()
true
}
else -> {
false
}
}
} else {
mOnInputTextEditorActionListener?.onEditorAction(v, actionId, event)
?: false
}
}
}
@@ -82,6 +99,20 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
switchElementView?.visibility = View.GONE
}
protected fun hideKeyboard(): Boolean {
context?.let {
ContextCompat.getSystemService(it, InputMethodManager::class.java)?.let { inputManager ->
activity?.currentFocus?.let { focus ->
val windowToken = focus.windowToken
if (windowToken != null) {
return inputManager.hideSoftInputFromWindow(windowToken, 0)
}
}
}
}
return false
}
fun setInoutText(@StringRes inputTextId: Int) {
inputText = getString(inputTextId)
}

View File

@@ -132,6 +132,7 @@
<string name="error_otp_counter">Counter must be between %1$d and %2$d.</string>
<string name="error_otp_period">Period must be between %1$d and %2$d seconds.</string>
<string name="error_otp_digits">Token must contain %1$d to %2$d digits.</string>
<string name="error_string_type">This text does not match the requested item.</string>
<string name="field_name">Field name</string>
<string name="field_value">Field value</string>
<string name="file_not_found_content">Could not find file. Try reopening it from your file browser.</string>