From 01e78072f2ad285eb455ae7c5b5e2edc24153c8f Mon Sep 17 00:00:00 2001 From: Philipp Gabriel Date: Mon, 10 Feb 2025 15:09:26 +0100 Subject: [PATCH] Add support for string arrays to AppStorage --- .../Sharing/SharedKeys/AppStorageKey.swift | 40 +++++++++++++++++-- Tests/SharingTests/AppStorageTests.swift | 9 ++++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Sources/Sharing/SharedKeys/AppStorageKey.swift b/Sources/Sharing/SharedKeys/AppStorageKey.swift index f0c4059..fd3a522 100644 --- a/Sources/Sharing/SharedKeys/AppStorageKey.swift +++ b/Sources/Sharing/SharedKeys/AppStorageKey.swift @@ -60,7 +60,19 @@ where Self == AppStorageKey { AppStorageKey(key, store: store) } - + + /// Creates a shared key that can read and write to a string array user default. + /// + /// - Parameters: + /// - key: The key to read and write the value to in the user defaults store. + /// - store: The user defaults store to read and write to. A value of `nil` will use the user + /// default store from dependencies. + /// - Returns: A user defaults shared key. + public static func appStorage(_ key: String, store: UserDefaults? = nil) -> Self + where Self == AppStorageKey<[String]> { + AppStorageKey(key, store: store) + } + /// Creates a shared key that can read and write to a URL user default. /// /// - Parameters: @@ -174,7 +186,19 @@ where Self == AppStorageKey { AppStorageKey(key, store: store) } - + + /// Creates a shared key that can read and write to an optional string array user default. + /// + /// - Parameters: + /// - key: The key to read and write the value to in the user defaults store. + /// - store: The user defaults store to read and write to. A value of `nil` will use the user + /// default store from dependencies. + /// - Returns: A user defaults shared key. + public static func appStorage(_ key: String, store: UserDefaults? = nil) -> Self + where Self == AppStorageKey<[String]?> { + AppStorageKey(key, store: store) + } + /// Creates a shared key that can read and write to an optional URL user default. /// /// - Parameters: @@ -305,7 +329,11 @@ fileprivate init(_ key: String, store: UserDefaults?) where Value == String { self.init(lookup: CastableLookup(), key: key, store: store) } - + + fileprivate init(_ key: String, store: UserDefaults?) where Value == [String] { + self.init(lookup: CastableLookup(), key: key, store: store) + } + fileprivate init(_ key: String, store: UserDefaults?) where Value == URL { self.init(lookup: URLLookup(), key: key, store: store) } @@ -341,7 +369,11 @@ fileprivate init(_ key: String, store: UserDefaults?) where Value == String? { self.init(lookup: OptionalLookup(base: CastableLookup()), key: key, store: store) } - + + fileprivate init(_ key: String, store: UserDefaults?) where Value == [String]? { + self.init(lookup: OptionalLookup(base: CastableLookup()), key: key, store: store) + } + fileprivate init(_ key: String, store: UserDefaults?) where Value == URL? { self.init(lookup: OptionalLookup(base: CastableLookup()), key: key, store: store) } diff --git a/Tests/SharingTests/AppStorageTests.swift b/Tests/SharingTests/AppStorageTests.swift index b5c0748..725f146 100644 --- a/Tests/SharingTests/AppStorageTests.swift +++ b/Tests/SharingTests/AppStorageTests.swift @@ -34,7 +34,14 @@ store.set("Blob, Jr.", forKey: "string") #expect(string == "Blob, Jr.") } - + + @Test func stringArray() { + @Shared(.appStorage("stringArray")) var stringArray = ["Blob"] + #expect(store.stringArray(forKey: "stringArray") == ["Blob"]) + store.set(["Blob", "Blob, Jr."], forKey: "stringArray") + #expect(stringArray == ["Blob", "Blob, Jr."]) + } + @Test func url() { @Shared(.appStorage("url")) var url = URL(fileURLWithPath: "/dev") #expect(store.url(forKey: "url") == URL(fileURLWithPath: "/dev"))