KVault icon indicating copy to clipboard operation
KVault copied to clipboard

kotPref like property delegates abstraction

Open NightGoat opened this issue 2 years ago • 9 comments

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)
}

NightGoat avatar Jun 15 '22 08:06 NightGoat

Sorry for the late reply, @NightGoat! I like the property delegate approach, I'll review your PR tomorrow :)

gaebel avatar Jul 25 '22 17:07 gaebel

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)

gaebel avatar Jul 26 '22 11:07 gaebel

We could also add a property delegate for existsObject():

val hasPin: Boolean by kvault.exists()

gaebel avatar Jul 26 '22 12:07 gaebel

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

NightGoat avatar Jul 29 '22 17:07 NightGoat

will someone merge it? i cant

NightGoat avatar Jul 31 '22 14:07 NightGoat

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).

gaebel avatar Aug 01 '22 09:08 gaebel

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

NightGoat avatar Aug 01 '22 09:08 NightGoat

You could also just use Any? as the delegate owner type.

gaebel avatar Aug 01 '22 09:08 gaebel

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 :)

NightGoat avatar Aug 01 '22 10:08 NightGoat

Is any alive here? @gaebel @benjohnde

NightGoat avatar Sep 24 '22 08:09 NightGoat