Last active
September 28, 2019 12:45
-
-
Save krupalshah/67eb1155aef745c8a188ff17f35e3c49 to your computer and use it in GitHub Desktop.
helper for shared prefs - kotlin version - Refactoring step 4
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
inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) { | |
val editor = this.edit() | |
operation(editor) | |
editor.apply() | |
} | |
fun SharedPreferences.setValue(key: String, value: Any?) { | |
when (value) { | |
is String? -> edit({ it.putString(key, value) }) | |
is Int -> edit({ it.putInt(key, value) }) | |
is Boolean -> edit({ it.putBoolean(key, value) }) | |
is Float -> edit({ it.putFloat(key, value) }) | |
is Long -> edit({ it.putLong(key, value) }) | |
else -> throw UnsupportedOperationException("Not yet implemented") | |
} | |
} |
Don't work, I can't call this form
val prefs = PreferenceHelper.customPrefs(this, Constants.SHERED_PREFERENCE_USER) prefs[Constants.USER_UID] = "UserId"
I haven't been actively working on Android. If you can make the necessary changes, it will be helpful. Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't work, I can't call this form
val prefs = PreferenceHelper.customPrefs(this, Constants.SHERED_PREFERENCE_USER) prefs[Constants.USER_UID] = "UserId"