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 package com.kunzisoft.keepass.settings.preferencedialogfragment
import android.view.View import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.kunzisoft.keepass.R import com.kunzisoft.keepass.R
@@ -47,10 +49,20 @@ abstract class AutofillBlocklistPreferenceDialogFragmentCompat
addSearchInfo(searchInfoString) 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) val addItemButton = view.findViewById<View>(R.id.add_item_button)
addItemButton?.setOnClickListener { addItemButton?.setOnClickListener {
addSearchInfo(inputText) addItemFromInputText()
filterAdapter?.replaceItems(persistedItems.toList())
} }
val recyclerView = view.findViewById<RecyclerView>(R.id.pref_dialog_list) 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) val itemToAdd = buildSearchInfoFromString(searchInfoString)
if (itemToAdd != null && !itemToAdd.containsOnlyNullValues()) return if (itemToAdd != null && !itemToAdd.containsOnlyNullValues()) {
persistedItems.add(itemToAdd) 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) { override fun onItemDeleted(item: SearchInfo) {

View File

@@ -21,10 +21,12 @@ package com.kunzisoft.keepass.settings.preferencedialogfragment
import android.view.View import android.view.View
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.CompoundButton import android.widget.CompoundButton
import android.widget.EditText import android.widget.EditText
import android.widget.TextView import android.widget.TextView
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceDialogFragmentCompat import androidx.preference.PreferenceDialogFragmentCompat
import com.kunzisoft.keepass.R import com.kunzisoft.keepass.R
@@ -34,6 +36,8 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
private var textExplanationView: TextView? = null private var textExplanationView: TextView? = null
private var switchElementView: CompoundButton? = null private var switchElementView: CompoundButton? = null
private var mOnInputTextEditorActionListener: TextView.OnEditorActionListener? = null
var inputText: String var inputText: String
get() = this.inputTextView?.text?.toString() ?: "" get() = this.inputTextView?.text?.toString() ?: ""
set(inputText) { 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? var explanationText: String?
get() = textExplanationView?.text?.toString() ?: "" get() = textExplanationView?.text?.toString() ?: ""
set(explanationText) { set(explanationText) {
@@ -63,16 +75,21 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
inputTextView = view.findViewById(R.id.input_text) inputTextView = view.findViewById(R.id.input_text)
inputTextView?.apply { inputTextView?.apply {
imeOptions = EditorInfo.IME_ACTION_DONE imeOptions = EditorInfo.IME_ACTION_DONE
setOnEditorActionListener { _, actionId, _ -> setOnEditorActionListener { v, actionId, event ->
when (actionId) { if (mOnInputTextEditorActionListener == null) {
EditorInfo.IME_ACTION_DONE -> { when (actionId) {
onDialogClosed(true) EditorInfo.IME_ACTION_DONE -> {
dialog?.dismiss() onDialogClosed(true)
true dialog?.dismiss()
} true
else -> { }
false else -> {
false
}
} }
} else {
mOnInputTextEditorActionListener?.onEditorAction(v, actionId, event)
?: false
} }
} }
} }
@@ -82,6 +99,20 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
switchElementView?.visibility = View.GONE 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) { fun setInoutText(@StringRes inputTextId: Int) {
inputText = getString(inputTextId) 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_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_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_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_name">Field name</string>
<string name="field_value">Field value</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> <string name="file_not_found_content">Could not find file. Try reopening it from your file browser.</string>