Created
March 2, 2015 12:55
-
-
Save ruXlab/73313c918357569b40c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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