Skip to content

Commit

Permalink
Correctly initialize editor prefs with custom data store. (#1568)
Browse files Browse the repository at this point in the history
When using editor prefs with a custom data store, the initial data load
happens before assigning the data store and thus too early. Re-do the
initialization when assigning the data store to fetch the content from
the correct place.

Fixes #1558

Signed-off-by: Danny Baumann <dannybaumann@web.de>
  • Loading branch information
maniac103 authored and mueller-ma committed Oct 2, 2019
1 parent 8bba88c commit 82f78e7
Showing 1 changed file with 15 additions and 0 deletions.
Expand Up @@ -22,13 +22,15 @@ import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import androidx.preference.EditTextPreference
import androidx.preference.EditTextPreferenceDialogFragmentCompat
import androidx.preference.PreferenceDataStore
import com.google.android.material.textfield.TextInputLayout
import org.openhab.habdroid.R

open class CustomInputTypePreference constructor(context: Context, attrs: AttributeSet) :
EditTextPreference(context, attrs) {
private val inputType: Int
private var autofillHints: Array<String>? = null
private var defValue: Any? = null

init {
val attrArray = intArrayOf(android.R.attr.inputType, android.R.attr.autofillHints)
Expand All @@ -39,6 +41,19 @@ open class CustomInputTypePreference constructor(context: Context, attrs: Attrib
}
}

override fun onSetInitialValue(defaultValue: Any?) {
defValue = defaultValue
super.onSetInitialValue(defaultValue)
}

override fun setPreferenceDataStore(dataStore: PreferenceDataStore?) {
super.setPreferenceDataStore(dataStore)
// The initial onSetInitialValue call, which initializes the editor content, is called before
// setPreferenceDataStore can possibly be called, so re-do that initialization here to get the content
// populated from the data store that was just set
super.onSetInitialValue(defValue)
}

override fun getDialogLayoutResource(): Int {
return R.layout.text_input_pref_dialog
}
Expand Down

0 comments on commit 82f78e7

Please sign in to comment.