Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add 'projectedValue' to allow access to 'key' and 'defaultValue' #4

Open
rob-secondstage opened this issue Oct 6, 2020 · 1 comment

Comments

@rob-secondstage
Copy link
Contributor

First, thanks for letting me contribute (in a small way) to your project. This is the first pull request I've ever done and the first time I've contributed anything to open source!

A suggested feature: add projectedValue to the property wrapper to allow easy access to the key and defaultValue properties. This can be useful when needing to specify these values later, for example, when using @AppStorage in SwiftUI.

Here is an example based on how I'm using this:

In the property wrapper:

    public var projectedValue: Self {
        return self
    }

Then, I use a struct to have easy access to all my persisted values:

struct Settings {
    @Persisted(key: "isFirstRun", defaultValue: true)
    static var isFirstRun

    @Persisted(key: "plantNameOrder", defaultValue: Plant.DisplayNameOrder.botanicalNameFirst)
    static var plantNameOrder: Plant.DisplayNameOrder
}

While that makes it easy to access settings throughout the app: Settings.plantNameOrder, it also, along with projectedValue makes using this in SwiftUI quite nice:

struct ContentView: View {
    @AppStorage(Settings.$plantNameOrder.key) var plantNameOrder = Settings.$plantNameOrder.defaultValue
    // ... rest of implementation 
}

Now, instead of having to make sure I get the string for key correct, and use the same default value, I can use what I've already done.

I just thought I'd suggest this based on my own usage. If you don't think this fits here, I totally get it. If you do, I'm happy to do another pull request and add something to the readme that shows this usage.

Thanks again.

@AndrewBennet
Copy link
Owner

Thank you! I'm happy to be involved in your first open source contribution!

This sounds like a very sensible addition; it seems like a neat way to expose the key & default value. Can't see any reason to not do it 👍 I'd welcome another pull request.

Cheers,
Andrew

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants