Skip to content

Instantly share code, notes, and snippets.

@ruXlab
Created March 2, 2015 12:55
Show Gist options
  • Save ruXlab/73313c918357569b40c8 to your computer and use it in GitHub Desktop.
Save ruXlab/73313c918357569b40c8 to your computer and use it in GitHub Desktop.
package vc.rux.utils
import android.content.Context
import android.content.SharedPreferences
/**
* Shared preferences helper
*/
public fun Context.loadPref<T>(key: String, default: T): T {
val sp = getSharedPreferences(javaClass.getCanonicalName(), 0)
return when (default) {
is Int -> sp.getInt(key, default)
is Boolean -> sp.getBoolean(key, default)
is String -> sp.getString(key, default)
is Float -> sp.getFloat(key, default)
is Long -> sp.getLong(key, default)
else -> default
} as T
}
public fun Context.savePref<T>(key: String, value: T) {
val sp = getSharedPreferences(javaClass.getCanonicalName(), 0).edit()
when (value) {
is Int -> sp.putInt(key, value)
is Boolean -> sp.putBoolean(key, value)
is String -> sp.putString(key, value)
is Float -> sp.putFloat(key, value)
is Long -> sp.putLong(key, value)
else -> return
}
sp.commit()
}
public fun SharedPreferences.edit(noinline action: (editor: SharedPreferences.Editor) -> Unit) {
val sp = edit()
action(sp)
sp.commit()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment