Skip to content

@TinyStorageItemSet: New property wrapper to retrieve from all keys of a type, as a dictionary - #13

Open
Mohamad-Kredly wants to merge 4 commits into
christianselig:mainfrom
Technofossils:TinyStorageItemSet
Open

@TinyStorageItemSet: New property wrapper to retrieve from all keys of a type, as a dictionary#13
Mohamad-Kredly wants to merge 4 commits into
christianselig:mainfrom
Technofossils:TinyStorageItemSet

Conversation

@Mohamad-Kredly

Copy link
Copy Markdown

Use case

Having a String enum conforming to TinyStorageKey, I wanted to access all of the stored value for the possible keys in one place instead of having to use a TinyStorageItem wrapper for every case and possibly miss some as I update the code.

What's new

This PR introduces two main things:

  • TinyStorageBuildableKey: Protocol to initialize a conforming type from a String raw value, if possible.
  • @TinyStorageItemSet: A property wrapper for a dictionary of Key conforming to the a new protocol and Type of Codable, Sendable, and Equatable. Note that String is not supported as a key as it would include all possible keys which goes against the use case described here.

Updates to README:

  • Added the new PropertyWrapper to the Usage section.
  • "Migration from UserDefaults" has been moved to its own section.

Usage

@TinyStorageItemSet(storage: .appGroup)
var platformStates: [Platform: AuthorizationState] = [.discord: .signedOut]

var body: some View {
    VStack {
        ForEach(Array(platformStates.keys)) { platform in
            Text("Platform \(platform.rawValue) is \(platformStates[platform]?.rawValue ?? "unset")")
        }
    }
}
public enum Platform: String, Identifiable, Codable, Sendable, TinyStorageBuildableKey  {
    case discord
    case twitter
    case bluesky
    
    public var id: String {
        self.rawValue
    }
}

public enum AuthorizationState: String, Codable, Sendable {
    case notDetermined
    case restricted
    case signedIn
    case signedOut
}

…ues for the keys matching the specified key type in a specified storage, as a dictionary of [Key: ValueType]

- New protocol TinyStorageBuildableKey to specify that a type could be rebuilt from a string raw value
- Value type must be equatable as to identify what changed in the dictionary, on set
* String as a key is not supported as it would fetch all keys which goes against the use case of this propety wrapper
…igrating from UserDefaults" to its own section
@Mohamad-Kredly Mohamad-Kredly changed the title Tiny storage item set @TinyStorageItemSet: New property wrapper to get all keys of a type, as a dictionary Jul 6, 2025
@Mohamad-Kredly Mohamad-Kredly changed the title @TinyStorageItemSet: New property wrapper to get all keys of a type, as a dictionary @TinyStorageItemSet: New property wrapper to retrieve from all keys of a type, as a dictionary Jul 6, 2025
@christianselig

Copy link
Copy Markdown
Owner

Very cool idea! I'm gonna leave this up as a PR because it feels slightly out of scope of what I'm envisioning for this library, but leaving it up as I think this is a cool use case that someone else may benefit from! Great work

@Mohamad-Kredly

Mohamad-Kredly commented Oct 16, 2025

Copy link
Copy Markdown
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants