-
Notifications
You must be signed in to change notification settings - Fork 563
Generic set & get method for SharedPreferences.Editor #435
Comments
Most extensions are inline, also, set would be better if you use a key and a value, looks like an indexer prefs["key"] = 5
prefs["key", 5] inline operator fun <reified T> SharedPreferences.Editor.set(key: String, value: T) {
when (T::class.java) {
Int::class.java -> putInt(key, value as Int)
Long::class.java -> putLong(key, value as Long)
}
}
inline operator fun <reified T> SharedPreferences.get(key: String, defValue: T): T = when (T::class.java) {
Int::class.java -> getInt(key, defValue as Int) as T
Long::class.java -> getLong(key, defValue as Long) as T
else -> 0 as T
} |
I don't think we want to encourage a reduction in type safety inside of a lambda block where we're not forced to. The current preference interaction is fairly concise. I'm not sure what we gain from changing it? |
We can make getting and setting values easier. |
It's not any easier, you're just code golfing character count and reducing type safety. |
It's easier because you no longer need to think which method to use: |
@JakeWharton |
There is an issue to replace the bundle factory with a type safe one once
inline classes are available in Kotlin 1.3. They are not equivalent use
cases anyway so the absence of safety in one does not compel the other to
throw it away.
…On Thu, Mar 29, 2018 at 2:58 AM Pinakin Kansara ***@***.***> wrote:
@JakeWharton <https://github.com/JakeWharton>
While looking at android-ktx code base, I found out that Bundle
<https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/os/Bundle.kt>
are also implemented as Feature I demanded for.
If type safety is the only concern for SharePreference to apply generic
set & get method then it should also be concern for Bundle as well.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#435 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEEEU_nxyKRN8PPvQZp_B6ZZ7-0USnNks5tjIYDgaJpZM4Sp6Cc>
.
|
In light of a more "generic" get & set, one thing I did was a delegation pattern much like that of the bundle. The implementation essentially allows us to do something like: object MyPrefs : KPref() {
var textColor: Int by kpref("TEXT_COLOR", Color.WHITE)
var bgColor: Int by kpref("BG_COLOR", Color.BLACK)
var isFirstLaunch: Boolean by kpref("IS_FIRST_LAUNCH", true)
...
} Where kpref is defined for the supported types and backed by a general class that checks instance types. You would then be able to get and set your preferences much as you would get and set a normal variable: MyPrefs.textColor = Color.BLACK |
Is it possible to have generic set & get method for SharedPreferences.Editor?
Instead of that can we have some thing like below to set preference.
Idea is to get ride of putBoolean, putString, putLong instead only use set.
Similar way for getting preferences instead of getString, getBoolean, getLong we should call something like below.
prefs.get("Test" to null)
Also my proposal is as below.
For setting values in preference.
For getting values from preference
The text was updated successfully, but these errors were encountered: