Refine AutoCompleteEditTextPreferenceDialogFragment
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
package be.mygod.vpnhotspot.preference
|
||||
|
||||
import android.content.Context
|
||||
import android.text.TextUtils
|
||||
import android.util.AttributeSet
|
||||
import androidx.preference.EditTextPreference
|
||||
import be.mygod.vpnhotspot.R
|
||||
import be.mygod.vpnhotspot.widget.AlwaysAutoCompleteEditText
|
||||
|
||||
open class AlwaysAutoCompleteEditTextPreference @JvmOverloads constructor(
|
||||
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = R.attr.editTextPreferenceStyle,
|
||||
defStyleRes: Int = 0) : EditTextPreference(context, attrs, defStyleAttr, defStyleRes) {
|
||||
val editText = AlwaysAutoCompleteEditText(context, attrs)
|
||||
|
||||
init {
|
||||
editText.id = android.R.id.edit
|
||||
SummaryFallbackProvider(this)
|
||||
}
|
||||
|
||||
override fun setText(text: String?) {
|
||||
val oldText = getText()
|
||||
super.setText(text)
|
||||
if (!TextUtils.equals(text, oldText)) notifyChanged()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package be.mygod.vpnhotspot.preference
|
||||
|
||||
import android.content.Context
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ArrayAdapter
|
||||
import androidx.core.os.bundleOf
|
||||
import androidx.preference.EditTextPreferenceDialogFragmentCompat
|
||||
import be.mygod.vpnhotspot.widget.AlwaysAutoCompleteEditText
|
||||
|
||||
class AlwaysAutoCompleteEditTextPreferenceDialogFragment : EditTextPreferenceDialogFragmentCompat() {
|
||||
companion object {
|
||||
private const val ARG_SUGGESTIONS = "suggestions"
|
||||
}
|
||||
|
||||
fun setArguments(key: String, suggestions: Array<String>) {
|
||||
arguments = bundleOf(ARG_KEY to key, ARG_SUGGESTIONS to suggestions)
|
||||
}
|
||||
|
||||
private lateinit var editText: AlwaysAutoCompleteEditText
|
||||
|
||||
override fun onCreateDialogView(context: Context) = super.onCreateDialogView(context).apply {
|
||||
editText = AlwaysAutoCompleteEditText(context).apply { id = android.R.id.edit }
|
||||
val oldEditText = findViewById<View>(android.R.id.edit)!!
|
||||
val container = oldEditText.parent as ViewGroup
|
||||
container.removeView(oldEditText)
|
||||
container.addView(editText, oldEditText.layoutParams)
|
||||
}
|
||||
|
||||
override fun onBindDialogView(view: View) {
|
||||
super.onBindDialogView(view)
|
||||
editText.hint = (preference.summaryProvider as SummaryFallbackProvider).fallback
|
||||
arguments?.getStringArray(ARG_SUGGESTIONS)?.let { suggestions ->
|
||||
editText.setAdapter(ArrayAdapter(view.context, android.R.layout.select_dialog_item, suggestions))
|
||||
}
|
||||
editText.clearFocus() // having focus is buggy currently
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package be.mygod.vpnhotspot.preference
|
||||
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ArrayAdapter
|
||||
import androidx.appcompat.widget.AppCompatAutoCompleteTextView
|
||||
import androidx.core.os.bundleOf
|
||||
import androidx.preference.PreferenceDialogFragmentCompat
|
||||
|
||||
class AlwaysAutoCompleteEditTextPreferenceDialogFragmentCompat : PreferenceDialogFragmentCompat() {
|
||||
companion object {
|
||||
private const val ARG_SUGGESTIONS = "suggestions"
|
||||
}
|
||||
|
||||
fun setArguments(key: String, suggestions: Array<String>) {
|
||||
arguments = bundleOf(Pair(ARG_KEY, key), Pair(ARG_SUGGESTIONS, suggestions))
|
||||
}
|
||||
|
||||
private lateinit var editText: AppCompatAutoCompleteTextView
|
||||
private val editTextPreference get() = this.preference as AlwaysAutoCompleteEditTextPreference
|
||||
|
||||
override fun onBindDialogView(view: View) {
|
||||
super.onBindDialogView(view)
|
||||
|
||||
editText = editTextPreference.editText
|
||||
editText.hint = (editTextPreference.summaryProvider as SummaryFallbackProvider).fallback
|
||||
editText.setText(this.editTextPreference.text)
|
||||
|
||||
val text = editText.text
|
||||
if (text != null) editText.setSelection(text.length, text.length)
|
||||
|
||||
val suggestions = arguments?.getStringArray(ARG_SUGGESTIONS)
|
||||
if (suggestions != null)
|
||||
editText.setAdapter(ArrayAdapter(view.context, android.R.layout.select_dialog_item, suggestions))
|
||||
|
||||
val oldParent = editText.parent as? ViewGroup?
|
||||
if (oldParent === view) return
|
||||
oldParent?.removeView(editText)
|
||||
val oldEditText = view.findViewById<View>(android.R.id.edit) ?: return
|
||||
val container = oldEditText.parent as? ViewGroup? ?: return
|
||||
container.removeView(oldEditText)
|
||||
container.addView(editText, oldEditText.layoutParams)
|
||||
}
|
||||
|
||||
override fun needInputMethod(): Boolean = true
|
||||
|
||||
override fun onDialogClosed(positiveResult: Boolean) {
|
||||
if (positiveResult) {
|
||||
val value = this.editText.text.toString()
|
||||
if (this.editTextPreference.callChangeListener(value)) {
|
||||
this.editTextPreference.text = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user