Skip to content

Instantly share code, notes, and snippets.

View kosratdev's full-sized avatar
🎯
Focusing

Kosrat Ahmed kosratdev

🎯
Focusing
View GitHub Profile
@kosratdev
kosratdev / PreferencesKtx.kt
Last active June 14, 2020 13:20
Better SharedPreferences with kotlin extensions
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