Skip to content

Commit

Permalink
Merge pull request #6 from tgeisse/cloudStorageRawRepFix
Browse files Browse the repository at this point in the history
Updates to treatment of RawRepresentable
  • Loading branch information
tgeisse authored Mar 27, 2024
2 parents 6185d4e + e43856d commit 15604c3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct UbiquitousKeyValueStoreWrapper<Value> {
private init() {}

public static nonisolated func castAnyValue(_ anyValue: Any?, defaultValue: Value) -> Value
where Value: RawRepresentable
where Value: RawRepresentable, Value.RawValue: UserDefaultsPropertyListValue
{
guard let anyValue = anyValue, let rawValue = anyValue as? Value.RawValue else {
return defaultValue
Expand All @@ -21,7 +21,7 @@ public struct UbiquitousKeyValueStoreWrapper<Value> {
}

public static nonisolated func castAnyValue<R>(_ anyValue: Any?, defaultValue: Value) -> Value
where Value == R?, R: RawRepresentable
where Value == R?, R: RawRepresentable, R.RawValue: UserDefaultsPropertyListValue
{
guard let anyValue = anyValue, let rawValue = anyValue as? R.RawValue else {
return defaultValue
Expand All @@ -42,5 +42,31 @@ public struct UbiquitousKeyValueStoreWrapper<Value> {
guard let anyValue = anyValue else { return defaultValue }
return (anyValue as? R) ?? defaultValue
}


// MARK: - Set values on NSUbiquitousKeyValueStore
public static nonisolated func set(_ newValue: Value, forKey key: String)
where Value: RawRepresentable, Value.RawValue: UserDefaultsPropertyListValue
{
NSUbiquitousKeyValueStore.default.set(newValue.rawValue, forKey: key)
}

public static nonisolated func set<R>(_ newValue: Value, forKey key: String)
where Value == R?, R: RawRepresentable, R.RawValue: UserDefaultsPropertyListValue
{
NSUbiquitousKeyValueStore.default.set(newValue?.rawValue, forKey: key)
}

public static nonisolated func set(_ newValue: Value, forKey key: String)
where Value: UserDefaultsPropertyListValue
{
NSUbiquitousKeyValueStore.default.set(newValue, forKey: key)
}

public static nonisolated func set<R>(_ newValue: Value, forKey key: String)
where Value == R?, R: UserDefaultsPropertyListValue
{
NSUbiquitousKeyValueStore.default.set(newValue, forKey: key)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import Foundation
public struct UserDefaultsWrapper<Value> {
private init() {}

// MARK: - Get Values
public static nonisolated func getValue(_ key: String, _ defaultValue: Value, _ store: UserDefaults) -> Value
where Value: RawRepresentable
where Value: RawRepresentable, Value.RawValue: UserDefaultsPropertyListValue
{
guard let rawValue = store.object(forKey: key) as? Value.RawValue else {
return defaultValue
Expand All @@ -21,7 +22,7 @@ public struct UserDefaultsWrapper<Value> {
}

public static nonisolated func getValue<R>(_ key: String, _ defaultValue: Value, _ store: UserDefaults) -> Value
where Value == R?, R: RawRepresentable
where Value == R?, R: RawRepresentable, R.RawValue: UserDefaultsPropertyListValue
{
guard let rawValue = store.object(forKey: key) as? R.RawValue else {
return defaultValue
Expand All @@ -41,14 +42,15 @@ public struct UserDefaultsWrapper<Value> {
return store.object(forKey: key) as? R ?? defaultValue
}

// MARK: - Set Values
public static nonisolated func setValue(_ key: String, _ newValue: Value, _ store: UserDefaults)
where Value: RawRepresentable
where Value: RawRepresentable, Value.RawValue: UserDefaultsPropertyListValue
{
store.set(newValue.rawValue, forKey: key)
}

public static nonisolated func setValue<R>(_ key: String, _ newValue: Value, _ store: UserDefaults)
where Value == R?, R: RawRepresentable
where Value == R?, R: RawRepresentable, R.RawValue: UserDefaultsPropertyListValue
{
store.set(newValue?.rawValue, forKey: key)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ public struct UbiquitousKeyValueStoreBackedMacro: AccessorMacro {
set {
withMutation(keyPath: \\.\(identifier)) {
UserDefaultsWrapper.setValue(\"\(userDefaultKey)\", newValue, _$userDefaultStore)
if let rawRep = newValue as? any RawRepresentable {
NSUbiquitousKeyValueStore.default.set(rawRep.rawValue, forKey: \"\(key)\")
} else {
NSUbiquitousKeyValueStore.default.set(newValue, forKey: \"\(key)\")
}
UbiquitousKeyValueStoreWrapper.set(newValue, forKey: \"\(key)\")
}
}
"""
Expand Down

0 comments on commit 15604c3

Please sign in to comment.