Skip to content

Add support for string arrays to AppStorageKey #107

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
40 changes: 36 additions & 4 deletions Sources/Sharing/SharedKeys/AppStorageKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,19 @@
where Self == AppStorageKey<String> {
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:
Expand Down Expand Up @@ -174,7 +186,19 @@
where Self == AppStorageKey<String?> {
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:
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 8 additions & 1 deletion Tests/SharingTests/AppStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down