/
PrefsDelegate.kt
49 lines (39 loc) · 1.65 KB
/
PrefsDelegate.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package me.jfenn.androidutils.prefs
import android.app.Dialog
import android.content.Context
import android.content.SharedPreferences
import android.view.View
import androidx.fragment.app.Fragment
import androidx.preference.PreferenceManager
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
/**
* Create a SharedPreferences property delegate
* with the provided type argument.
*
* `var theme by prefs.get("theme")`
*
* @param key The preference key. (optional: defaults to property name)
* @param defaultValue Default value of the preference.
*/
inline fun <reified T> SharedPreferences.get(
key: String? = null,
defaultValue: T? = null
) = object : ReadWriteProperty<Any?, T?> {
override fun getValue(thisRef: Any?, property: KProperty<*>): T? {
return this@get[key ?: property.name] ?: defaultValue
}
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) {
this@get[key ?: property.name] = value
}
}
inline fun <reified T> SharedPreferences.get(pref: TypedPreference<T>)
= this.get(pref.key, pref.defaultValue)
inline fun <reified T> Context.prefs(pref: TypedPreference<T>)
= PreferenceManager.getDefaultSharedPreferences(this).get(pref)
inline fun <reified T> Fragment.prefs(pref: TypedPreference<T>)
= PreferenceManager.getDefaultSharedPreferences(context).get(pref)
inline fun <reified T> View.prefs(pref: TypedPreference<T>)
= PreferenceManager.getDefaultSharedPreferences(context).get(pref)
inline fun <reified T> Dialog.prefs(pref: TypedPreference<T>)
= PreferenceManager.getDefaultSharedPreferences(context).get(pref)