KVault
KVault copied to clipboard
kotPref like property delegates abstraction
I really like your library, so i thought it would be nice to have property delegates as in kotPref library, it makes much less code writing for accessing properties. Example from my project:
class SettingsProvider : KVaultPref() {
override val kvault = KvaultProvider().provide()
private var pinCode: String by stringPref(PIN_CODE_KEY)
var isPinCodeSet: Boolean by booleanPref(IS_PIN_CODE_SET_KEY)
}
Sorry for the late reply, @NightGoat! I like the property delegate approach, I'll review your PR tomorrow :)
Generally I'm fine with your implementation, though I was thinking of how we could improve it:
- I'd implement the property delegates by creating extension functions on the KVault class.
- It would be nice if it is possible to resolve to null
- We could make the key optional and use the property name if it is null
val pinCode: String? by kvault.string()
val legacyPinCode: String? by kvault.string(key = "pin_code")
val hasPin: Boolean by kvault.bool(defaultValue = false)
We could also add a property delegate for existsObject()
:
val hasPin: Boolean by kvault.exists()
Okay, I updated my PR: I added extensions, keys nullability and exists() extension. Unfortunately I could not find a solution for a change nullability on the fly, but i added nullable extensions as well
will someone merge it? i cant
Mostly looking good to me. I'm hesitant to use the suffix "pref", since we're not only using SharedPreferences (also doesn't feel in line with the rest of the library). The separate abstract class delegate is redundant imo, I guess it's meant to be able to drop "kvault" in the delegate call, though I'd prefer the verboseness (you could create the class in your code base if you prefer it the other way).
ReadWriteProperty needs to know what class is makes the changes in the delegate property, so if you use only the extension you need to write not the only kvault in the beginning but add the class that uses that. For example:
class Foo() {
val kvault = Kvault()
val pin = kvault.stringPref<Foo>("", "")
}
The abstraction takes it boilerplate from you.
class Foo(): KvaultPref {
override val kvault = Kvault()
val pin = stringPref("", "")
}
As about the name - i just used name that would be familiar to a lot of people, since kotPref is well known library. You can use whatever you want
You could also just use Any? as the delegate owner type.
Okay, i will delete abstraction and use Any as owner type, i can change prefix to vault: stringVault
. But thats it, take it or leave it :)
Is any alive here? @gaebel @benjohnde