Skip to content

Instantly share code, notes, and snippets.

@krupalshah
Last active September 28, 2019 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krupalshah/67eb1155aef745c8a188ff17f35e3c49 to your computer and use it in GitHub Desktop.
Save krupalshah/67eb1155aef745c8a188ff17f35e3c49 to your computer and use it in GitHub Desktop.
helper for shared prefs - kotlin version - Refactoring step 4
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")
}
}
@RonivaldoRoner
Copy link

Don't work, I can't call this form

val prefs = PreferenceHelper.customPrefs(this, Constants.SHERED_PREFERENCE_USER) prefs[Constants.USER_UID] = "UserId"

@krupalshah
Copy link
Author

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