mirror of
https://gist.github.com/9eafa3a94afef6b6a50af07fdc940abf.git
synced 2025-04-24 21:37:11 -07:00
38 lines
No EOL
1.1 KiB
Kotlin
38 lines
No EOL
1.1 KiB
Kotlin
import android.content.Context
|
|
import android.content.SharedPreferences
|
|
import android.preference.PreferenceManager
|
|
|
|
fun Context.getDefaultSharedPreferences(): SharedPreferences {
|
|
return PreferenceManager.getDefaultSharedPreferences(this)
|
|
}
|
|
|
|
fun SharedPreferences.clear() {
|
|
edit().clear().apply()
|
|
}
|
|
|
|
fun SharedPreferences.put(key: String, value: Any) {
|
|
when (value) {
|
|
is String -> edit().putString(key, value).apply()
|
|
is Float -> edit().putFloat(key, value).apply()
|
|
is Int -> edit().putInt(key, value).apply()
|
|
is Boolean -> edit().putBoolean(key, value).apply()
|
|
is Long -> edit().putLong(key, value).apply()
|
|
is Set<*> -> {
|
|
if (value.all { it is String }) {
|
|
edit().putStringSet(key, value as Set<String>)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fun SharedPreferences.put(vararg pairs: Pair<String, String>) {
|
|
edit().apply {
|
|
pairs.forEach {
|
|
putString(it.first, it.second)
|
|
}
|
|
}.apply()
|
|
}
|
|
|
|
fun SharedPreferences.remove(key: String) {
|
|
edit().remove(key).apply()
|
|
} |