Skip to content

Commit

Permalink
AppStorage: add support for Date values (#3470)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Celis <[email protected]>
  • Loading branch information
rudedogdhc and stephencelis authored Nov 12, 2024
1 parent fdad435 commit a952dde
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ extension PersistenceReaderKey {
AppStorageKey(key)
}

/// Creates a persistence key that can read and write to a Date user default.
///
/// - Parameter key: The key to read and write the value to in the user defaults store.
/// - Returns: A user defaults persistence key.
public static func appStorage(_ key: String) -> Self
where Self == AppStorageKey<Date> {
AppStorageKey(key)
}

/// Creates a persistence key that can read and write to a user default as data.
///
/// - Parameter key: The key to read and write the value to in the user defaults store.
Expand Down Expand Up @@ -121,6 +130,15 @@ extension PersistenceReaderKey {
AppStorageKey(key)
}

/// Creates a persistence key that can read and write to an optional Date user default.
///
/// - Parameter key: The key to read and write the value to in the user defaults store.
/// - Returns: A user defaults persistence key.
public static func appStorage(_ key: String) -> Self
where Self == AppStorageKey<Date?> {
AppStorageKey(key)
}

/// Creates a persistence key that can read and write to a user default as optional data.
///
/// - Parameter key: The key to read and write the value to in the user defaults store.
Expand Down Expand Up @@ -198,6 +216,13 @@ public struct AppStorageKey<Value: Sendable>: Sendable {
self.store = UncheckedSendable(store)
}

fileprivate init(_ key: String) where Value == Date {
@Dependency(\.defaultAppStorage) var store
self.lookup = CastableLookup()
self.key = key
self.store = UncheckedSendable(store)
}

fileprivate init(_ key: String) where Value == Data {
@Dependency(\.defaultAppStorage) var store
self.lookup = CastableLookup()
Expand Down Expand Up @@ -254,6 +279,13 @@ public struct AppStorageKey<Value: Sendable>: Sendable {
self.store = UncheckedSendable(store)
}

fileprivate init(_ key: String) where Value == Date? {
@Dependency(\.defaultAppStorage) var store
self.lookup = OptionalLookup(base: CastableLookup())
self.key = key
self.store = UncheckedSendable(store)
}

fileprivate init(_ key: String) where Value == Data? {
@Dependency(\.defaultAppStorage) var store
self.lookup = OptionalLookup(base: CastableLookup())
Expand Down
39 changes: 39 additions & 0 deletions Tests/ComposableArchitectureTests/AppStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,38 @@ final class AppStorageTests: XCTestCase {
XCTAssertEqual(defaults.url(forKey: "url"), URL(string: "https://example.com"))
}

func testDefaultsReadDate() {
let expectedDate = Date()
@Dependency(\.defaultAppStorage) var defaults
defaults.set(expectedDate, forKey: "date")
@Shared(.appStorage("date")) var date: Date?
XCTAssertEqual(date, expectedDate)
}

func testDefaultsRegistered_Date() {
let expectedDate = Date()
@Dependency(\.defaultAppStorage) var defaults
@Shared(.appStorage("date")) var date: Date = expectedDate
XCTAssertEqual(defaults.object(forKey: "date") as? Date, expectedDate)

let newDate = Date().addingTimeInterval(60)
date = newDate
XCTAssertEqual(date, newDate)
XCTAssertEqual(defaults.object(forKey: "date") as? Date, newDate)
}

func testDefaultsRegistered_Optional_Date() {
let initialDate: Date? = Date()
@Dependency(\.defaultAppStorage) var defaults
@Shared(.appStorage("date")) var date: Date? = initialDate
XCTAssertEqual(defaults.object(forKey: "date") as? Date, initialDate)

let newDate = Date().addingTimeInterval(60)
date = newDate
XCTAssertEqual(date, newDate)
XCTAssertEqual(defaults.object(forKey: "date") as? Date, newDate)
}

func testDefaultsRegistered_Optional() {
@Dependency(\.defaultAppStorage) var defaults
@Shared(.appStorage("data")) var data: Data?
Expand Down Expand Up @@ -184,6 +216,13 @@ final class AppStorageTests: XCTestCase {
XCTAssertEqual(url2, nil)
}

func testOptionalInitializers_Date() {
@Shared(.appStorage("date1")) var date1: Date?
XCTAssertEqual(date1, nil)
@Shared(.appStorage("date2")) var date2: Date? = nil
XCTAssertEqual(date2, nil)
}

func testRemoveDuplicates() {
@Dependency(\.defaultAppStorage) var store
@Shared(.appStorage("count")) var count = 0
Expand Down

0 comments on commit a952dde

Please sign in to comment.