Skip to content
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

AppStorage: add support for Date values #3470

Merged
merged 2 commits into from
Nov 12, 2024
Merged
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
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
Loading