Skip to content

Instantly share code, notes, and snippets.


Kosrat Ahmed kosratdev

View GitHub Profile
kosratdev / PreferencesKtx.kt
Last active Jun 14, 2020
Better SharedPreferences with kotlin extensions
View PreferencesKtx.kt
inline val Context.sharedPreferences: SharedPreferences
get() = PreferenceManager.getDefaultSharedPreferences(this)
inline fun <reified T> SharedPreferences.get(key: String, defaultValue: T): T {
return when (T::class) {
Boolean::class -> this.getBoolean(key, defaultValue as Boolean) as T
Float::class -> this.getFloat(key, defaultValue as Float) as T
Int::class -> this.getInt(key, defaultValue as Int) as T
Long::class -> this.getLong(key, defaultValue as Long) as T
String::class -> this.getString(key, defaultValue as String) as T