Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,26 @@ Or better support for optional values:
var nickname: String? = nil // or "Cool Guy"
```

You can also migrate from a `UserDefaults` instance to `TinyStorage` with a handy helper function:
And, if you want to get all values of a specific key type in your storage, you can use the `@TinyStorageItemSet` property wrapper. As opposed to `@TinyStorageItem`, you specify your storage then initialize a dictionary having the key and value types of your choosing on the condition that: 1. The key conforms to `TinyStorageBuildableKey`, and 2. The value conforms to Equatable. Default values are supported the same as before, keys without a default would default to the value of the first key if available.

Note that `String` is not supported as a key type for this property wrapper.

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

var body: some View {
VStack {
ForEach(Array(platformStates.keys), id: \.rawValue) { platform in
Text("Platform \(platform.rawValue) is \(platformStates[platform]?.rawValue ?? "unset")")
}
}
}
```

## Migrating from UserDefaults

You can migrate from a `UserDefaults` instance to `TinyStorage` with a handy helper function:

```swift
let nonBoolKeysToMigrate = ["favoriteIceCream", "appFontSize", "lastFetchDate"]
Expand Down
58 changes: 58 additions & 0 deletions Sources/TinyStorage/TinyStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,11 @@ public protocol TinyStorageKey: Hashable, Sendable {
var rawValue: String { get }
}

/// A ``TinyStorageKey`` that could be initialized from a String raw value
public protocol TinyStorageBuildableKey: TinyStorageKey {
init?(rawValue: String)
}

extension String: TinyStorageKey {
public var rawValue: String { self }
}
Expand Down Expand Up @@ -779,3 +784,56 @@ public struct TinyStorageItem<T: Codable & Sendable>: DynamicProperty, Sendable
)
}
}

@propertyWrapper
public struct TinyStorageItemSet<K: TinyStorageBuildableKey, T: Codable & Sendable & Equatable>: DynamicProperty, Sendable {
@State private var storage: TinyStorage

private let defaultValue: [K: T]

public init(wrappedValue: [K: T], storage: TinyStorage) {
self.defaultValue = wrappedValue
self.storage = storage
}

public var wrappedValue: [K: T] {
get {
var itemDictionary: [K: T] = [:]
for key in storage.allKeys.compactMap({ K(rawValue: $0.rawValue) }) {
let retrieved = storage.retrieve(type: T.self, forKey: key)
itemDictionary[key] = retrieved ?? (defaultValue[key] ?? defaultValue.values.first)
}
if itemDictionary.isEmpty {
itemDictionary = defaultValue
}
return itemDictionary
}
nonmutating set { didSet(newValue) }
}

public var projectedValue: Binding<[K: T]> {
Binding(
get: { wrappedValue },
set: { didSet($0) }
)
}

private func didSet(_ newValue: [K: T]) {
let oldValue = wrappedValue
guard !(oldValue.isEmpty && newValue.isEmpty) else { return }

let allKeys = Set(oldValue.keys).union(newValue.keys)

let changedKeys = allKeys.filter { oldValue[$0] != newValue[$0] }

let changedDict = newValue.filter({ changedKeys.contains($0.key) })
for (key, value) in changedDict {
storage.store(value, forKey: key)
}

let removedKeys = changedKeys.filter({ !changedDict.keys.contains($0) })
for key in removedKeys {
storage.remove(key: key)
}
}
}