Fix settings default values for preferences (#1828)

At some point settings DSL was refactored to first add a preference and
then run the builder. We shouldn't add a preference to the hierarchy
without setting a key for the preference first because preference gets
it's default value in `onAttachedToHierarchy()` and if the key is not
set then no default value will be set either.

This commit changes the order to execute builder (and set the key)
first and and preference to the point later.
This commit is contained in:
Ivan Kupalov 2020-06-14 19:58:05 +02:00 committed by GitHub
parent 4219871095
commit ecb94beb3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -12,22 +12,22 @@ class PreferenceParent(
inline fun PreferenceParent.preference(builder: Preference.() -> Unit): Preference { inline fun PreferenceParent.preference(builder: Preference.() -> Unit): Preference {
val pref = Preference(context) val pref = Preference(context)
addPref(pref)
builder(pref) builder(pref)
addPref(pref)
return pref return pref
} }
inline fun PreferenceParent.listPreference(builder: ListPreference.() -> Unit): ListPreference { inline fun PreferenceParent.listPreference(builder: ListPreference.() -> Unit): ListPreference {
val pref = ListPreference(context) val pref = ListPreference(context)
addPref(pref)
builder(pref) builder(pref)
addPref(pref)
return pref return pref
} }
inline fun PreferenceParent.emojiPreference(builder: EmojiPreference.() -> Unit): EmojiPreference { inline fun PreferenceParent.emojiPreference(builder: EmojiPreference.() -> Unit): EmojiPreference {
val pref = EmojiPreference(context) val pref = EmojiPreference(context)
addPref(pref)
builder(pref) builder(pref)
addPref(pref)
return pref return pref
} }
@ -35,8 +35,8 @@ inline fun PreferenceParent.switchPreference(
builder: SwitchPreference.() -> Unit builder: SwitchPreference.() -> Unit
): SwitchPreference { ): SwitchPreference {
val pref = SwitchPreference(context) val pref = SwitchPreference(context)
addPref(pref)
builder(pref) builder(pref)
addPref(pref)
return pref return pref
} }
@ -44,8 +44,8 @@ inline fun PreferenceParent.editTextPreference(
builder: EditTextPreference.() -> Unit builder: EditTextPreference.() -> Unit
): EditTextPreference { ): EditTextPreference {
val pref = EditTextPreference(context) val pref = EditTextPreference(context)
addPref(pref)
builder(pref) builder(pref)
addPref(pref)
return pref return pref
} }
@ -53,8 +53,8 @@ inline fun PreferenceParent.checkBoxPreference(
builder: CheckBoxPreference.() -> Unit builder: CheckBoxPreference.() -> Unit
): CheckBoxPreference { ): CheckBoxPreference {
val pref = CheckBoxPreference(context) val pref = CheckBoxPreference(context)
addPref(pref)
builder(pref) builder(pref)
addPref(pref)
return pref return pref
} }