Skip to content

Commit

Permalink
Add Date and RawRepresentable implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlokhorst committed Aug 31, 2024
1 parent 91c9bd9 commit 766a589
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Sources/CloudStorage/CloudStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ internal class CloudStorageObject<Value>: ObservableObject {
}
}

extension CloudStorage : Sendable where Value : Sendable {
}

extension CloudStorage where Value == Bool {
public init(wrappedValue: Value, _ key: String) {
self.init(
Expand Down Expand Up @@ -132,6 +135,15 @@ extension CloudStorage where Value == URL {
}
}

extension CloudStorage where Value == Date {
public init(wrappedValue: Value, _ key: String) {
self.init(
keyName: key,
syncGet: { sync.date(for: key) ?? wrappedValue },
syncSet: { newValue in sync.set(newValue, for: key) })
}
}

extension CloudStorage where Value == Data {
public init(wrappedValue: Value, _ key: String) {
self.init(
Expand Down Expand Up @@ -204,6 +216,15 @@ extension CloudStorage where Value == URL? {
}
}

extension CloudStorage where Value == Date? {
public init(_ key: String) {
self.init(
keyName: key,
syncGet: { sync.date(for: key) },
syncSet: { newValue in sync.set(newValue, for: key) })
}
}

extension CloudStorage where Value == Data? {
public init(_ key: String) {
self.init(
Expand All @@ -212,3 +233,21 @@ extension CloudStorage where Value == Data? {
syncSet: { newValue in sync.set(newValue, for: key) })
}
}

extension CloudStorage {
public init<R>(_ key: String) where Value == R?, R: RawRepresentable, R.RawValue == String {
self.init(
keyName: key,
syncGet: { sync.rawRepresentable(for: key) },
syncSet: { newValue in sync.set(newValue, for: key) })
}
}

extension CloudStorage {
public init<R>(_ key: String) where Value == R?, R: RawRepresentable, R.RawValue == Int {
self.init(
keyName: key,
syncGet: { sync.rawRepresentable(for: key) },
syncSet: { newValue in sync.set(newValue, for: key) })
}
}
27 changes: 27 additions & 0 deletions Sources/CloudStorage/CloudStorageSync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ extension CloudStorageSync {
ubiquitousKvs.dictionary(forKey: key)
}

public func date(for key: String) -> Date? {
guard let obj = ubiquitousKvs.object(forKey: key) else { return nil }
return obj as? Date
}

public func data(for key: String) -> Data? {
ubiquitousKvs.data(forKey: key)
}
Expand All @@ -153,6 +158,18 @@ extension CloudStorageSync {
return ubiquitousKvs.bool(forKey: key)
}

public func rawRepresentable<R>(for key: String) -> R? where R: RawRepresentable, R.RawValue == String {
guard let str = ubiquitousKvs.string(forKey: key) else { return nil }
return R(rawValue: str)
}

public func rawRepresentable<R>(for key: String) -> R? where R: RawRepresentable, R.RawValue == Int {
if ubiquitousKvs.object(forKey: key) == nil { return nil }
let int = Int(ubiquitousKvs.longLong(forKey: key))
return R(rawValue: int)
}

//

public func set(_ value: String?, for key: String) {
ubiquitousKvs.set(value, forKey: key)
Expand Down Expand Up @@ -198,6 +215,16 @@ extension CloudStorageSync {
ubiquitousKvs.set(value, forKey: key)
status = Status(date: Date(), source: .localChange, keys: [key])
}

public func set<R>(_ value: R?, for key: String) where R: RawRepresentable, R.RawValue == String {
ubiquitousKvs.set(value?.rawValue, forKey: key)
status = Status(date: Date(), source: .localChange, keys: [key])
}

public func set<R>(_ value: R?, for key: String) where R: RawRepresentable, R.RawValue == Int {
ubiquitousKvs.set(value?.rawValue, forKey: key)
status = Status(date: Date(), source: .localChange, keys: [key])
}
}

extension CloudStorageSync {
Expand Down

0 comments on commit 766a589

Please sign in to comment.