@@ -36,25 +36,23 @@ inline fun <reified T> pref(
3636 defaultValue : T ? = null
3737) = object : ReadOnlyProperty <Any ?, TypedPreference <T >> {
3838
39- val pref: TypedPreference <T >? = null
39+ var pref: TypedPreference <T >? = null
4040
41- override fun getValue (thisRef : Any? , property : KProperty <* >) = pref ? : run {
42- object : TypedPreference <T > {
41+ override fun getValue (thisRef : Any? , property : KProperty <* >) = pref ? : object : TypedPreference <T > {
4342
44- override val key = key ? : property.name
45- override val defaultValue: T ? = defaultValue
43+ override val key = key ? : property.name
44+ override val defaultValue: T ? = defaultValue
4645
47- override fun get (ctx : Context ): T ? {
48- val prefs = PreferenceManager .getDefaultSharedPreferences(ctx)
49- return prefs[this .key] ? : this .defaultValue
50- }
51-
52- override fun set (ctx : Context , value : T ? ) {
53- val prefs = PreferenceManager .getDefaultSharedPreferences(ctx)
54- prefs[this .key] = value
55- }
46+ override fun get (ctx : Context ): T ? {
47+ val prefs = PreferenceManager .getDefaultSharedPreferences(ctx)
48+ return prefs[this .key] ? : this .defaultValue
49+ }
5650
51+ override fun set (ctx : Context , value : T ? ) {
52+ val prefs = PreferenceManager .getDefaultSharedPreferences(ctx)
53+ prefs[this .key] = value
5754 }
58- }
55+
56+ }.also { pref = it }
5957
6058}
0 commit comments