mirror of
https://gist.github.com/9eafa3a94afef6b6a50af07fdc940abf.git
synced 2025-04-24 21:37:11 -07:00
This commit is contained in:
commit
41109da54c
1 changed files with 38 additions and 0 deletions
38
Preferences.kt
Normal file
38
Preferences.kt
Normal file
|
@ -0,0 +1,38 @@
|
|||
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()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue