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
86 changes: 86 additions & 0 deletions Sources/Sharing/SharedKeys/AppStorageKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,36 @@
AppStorageKey(key, store: store)
}

/// Creates a shared key that can read and write to an double user default, transforming
/// that to a `RawRepresentable` data type.
///
/// - 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<Value: RawRepresentable<Double>>(
_ key: String, store: UserDefaults? = nil
) -> Self
where Self == AppStorageKey<Value> {
AppStorageKey(key, store: store)
}

/// Creates a shared key that can read and write to a float user default, transforming
/// that to a `RawRepresentable` data type.
///
/// - 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<Value: RawRepresentable<Float>>(
_ key: String, store: UserDefaults? = nil
) -> Self
where Self == AppStorageKey<Value> {
AppStorageKey(key, store: store)
}

/// Creates a shared key that can read and write to a string user default, transforming
/// that to a `RawRepresentable` data type.
///
Expand Down Expand Up @@ -280,6 +310,36 @@
AppStorageKey(key, store: store)
}

/// Creates a shared key that can read and write to an optional double user default,
/// transforming that to a `RawRepresentable` data type.
///
/// - 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<Value: RawRepresentable>(
_ key: String, store: UserDefaults? = nil
) -> Self
where Value.RawValue == Double, Self == AppStorageKey<Value?> {
AppStorageKey(key, store: store)
}

/// Creates a shared key that can read and write to an optional float user default,
/// transforming that to a `RawRepresentable` data type.
///
/// - 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<Value: RawRepresentable>(
_ key: String, store: UserDefaults? = nil
) -> Self
where Value.RawValue == Float, Self == AppStorageKey<Value?> {
AppStorageKey(key, store: store)
}

/// Creates a shared key that can read and write to an optional string user default,
/// transforming that to a `RawRepresentable` data type.
///
Expand Down Expand Up @@ -384,6 +444,14 @@
self.init(lookup: RawRepresentableLookup(base: CastableLookup()), key: key, store: store)
}

fileprivate init(_ key: String, store: UserDefaults?) where Value: RawRepresentable<Double> {
self.init(lookup: RawRepresentableLookup(base: CastableLookup()), key: key, store: store)
}

fileprivate init(_ key: String, store: UserDefaults?) where Value: RawRepresentable<Float> {
self.init(lookup: RawRepresentableLookup(base: CastableLookup()), key: key, store: store)
}

fileprivate init(_ key: String, store: UserDefaults?) where Value: RawRepresentable<String> {
self.init(lookup: RawRepresentableLookup(base: CastableLookup()), key: key, store: store)
}
Expand Down Expand Up @@ -438,6 +506,24 @@
)
}

fileprivate init<R: RawRepresentable<Double>>(_ key: String, store: UserDefaults?)
where Value == R? {
self.init(
lookup: OptionalLookup(base: RawRepresentableLookup(base: CastableLookup())),
key: key,
store: store
)
}

fileprivate init<R: RawRepresentable<Float>>(_ key: String, store: UserDefaults?)
where Value == R? {
self.init(
lookup: OptionalLookup(base: RawRepresentableLookup(base: CastableLookup())),
key: key,
store: store
)
}

fileprivate init<R: RawRepresentable<String>>(_ key: String, store: UserDefaults?)
where Value == R? {
self.init(
Expand Down
20 changes: 20 additions & 0 deletions Tests/SharingTests/AppStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@
#expect(id == ID(rawValue: 1729))
}

@Test func rawRepresentableDouble() {
struct ID: RawRepresentable {
var rawValue: Double
}
@Shared(.appStorage("raw-representable-double")) var id = ID(rawValue: 4.2)
#expect(store.double(forKey: "raw-representable-double") == 4.2)
store.set(172.9, forKey: "raw-representable-double")
#expect(id == ID(rawValue: 172.9))
}

@Test func rawRepresentableFloat() {
struct ID: RawRepresentable {
var rawValue: Float
}
@Shared(.appStorage("raw-representable-float")) var id = ID(rawValue: 4.2)
#expect(store.float(forKey: "raw-representable-float") == 4.2)
store.set(Float(172.9), forKey: "raw-representable-float")
#expect(id == ID(rawValue: 172.9))
}

@Test func rawRepresentableString() {
struct ID: RawRepresentable {
var rawValue: String
Expand Down