Skip to content

Commit da020ba

Browse files
ph1psstephencelis
andauthored
Add support for string arrays to AppStorage (#107)
Co-authored-by: Stephen Celis <[email protected]>
1 parent a541c5b commit da020ba

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

Sources/Sharing/SharedKeys/AppStorageKey.swift

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,19 @@
6060
where Self == AppStorageKey<String> {
6161
AppStorageKey(key, store: store)
6262
}
63-
63+
64+
/// Creates a shared key that can read and write to a string array user default.
65+
///
66+
/// - Parameters:
67+
/// - key: The key to read and write the value to in the user defaults store.
68+
/// - store: The user defaults store to read and write to. A value of `nil` will use the user
69+
/// default store from dependencies.
70+
/// - Returns: A user defaults shared key.
71+
public static func appStorage(_ key: String, store: UserDefaults? = nil) -> Self
72+
where Self == AppStorageKey<[String]> {
73+
AppStorageKey(key, store: store)
74+
}
75+
6476
/// Creates a shared key that can read and write to a URL user default.
6577
///
6678
/// - Parameters:
@@ -188,7 +200,19 @@
188200
where Self == AppStorageKey<String?> {
189201
AppStorageKey(key, store: store)
190202
}
191-
203+
204+
/// Creates a shared key that can read and write to an optional string array user default.
205+
///
206+
/// - Parameters:
207+
/// - key: The key to read and write the value to in the user defaults store.
208+
/// - store: The user defaults store to read and write to. A value of `nil` will use the user
209+
/// default store from dependencies.
210+
/// - Returns: A user defaults shared key.
211+
public static func appStorage(_ key: String, store: UserDefaults? = nil) -> Self
212+
where Self == AppStorageKey<[String]?> {
213+
AppStorageKey(key, store: store)
214+
}
215+
192216
/// Creates a shared key that can read and write to an optional URL user default.
193217
///
194218
/// - Parameters:
@@ -333,7 +357,11 @@
333357
fileprivate init(_ key: String, store: UserDefaults?) where Value == String {
334358
self.init(lookup: CastableLookup(), key: key, store: store)
335359
}
336-
360+
361+
fileprivate init(_ key: String, store: UserDefaults?) where Value == [String] {
362+
self.init(lookup: CastableLookup(), key: key, store: store)
363+
}
364+
337365
fileprivate init(_ key: String, store: UserDefaults?) where Value == URL {
338366
self.init(lookup: URLLookup(), key: key, store: store)
339367
}
@@ -373,7 +401,11 @@
373401
fileprivate init(_ key: String, store: UserDefaults?) where Value == String? {
374402
self.init(lookup: OptionalLookup(base: CastableLookup()), key: key, store: store)
375403
}
376-
404+
405+
fileprivate init(_ key: String, store: UserDefaults?) where Value == [String]? {
406+
self.init(lookup: OptionalLookup(base: CastableLookup()), key: key, store: store)
407+
}
408+
377409
fileprivate init(_ key: String, store: UserDefaults?) where Value == URL? {
378410
self.init(lookup: OptionalLookup(base: URLLookup()), key: key, store: store)
379411
}

Tests/SharingTests/AppStorageTests.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@
3434
store.set("Blob, Jr.", forKey: "string")
3535
#expect(string == "Blob, Jr.")
3636
}
37-
37+
38+
@Test func stringArray() {
39+
@Shared(.appStorage("stringArray")) var stringArray = ["Blob"]
40+
#expect(store.stringArray(forKey: "stringArray") == ["Blob"])
41+
store.set(["Blob", "Blob, Jr."], forKey: "stringArray")
42+
#expect(stringArray == ["Blob", "Blob, Jr."])
43+
}
44+
3845
@Test func url() {
3946
@Shared(.appStorage("url")) var url = URL(fileURLWithPath: "/dev")
4047
#expect(store.url(forKey: "url") == URL(fileURLWithPath: "/dev"))

0 commit comments

Comments
 (0)