From ce45591c09cb421835cfcf9bec5f586e3a19d551 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sat, 20 Apr 2024 16:12:50 +0700 Subject: [PATCH] Add support for serializing `Color.Resolved` (#171) --- Sources/Defaults/Defaults+Extensions.swift | 3 +++ Tests/DefaultsTests/DefaultsColorTests.swift | 22 +++++++++++++++++++- readme.md | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Sources/Defaults/Defaults+Extensions.swift b/Sources/Defaults/Defaults+Extensions.swift index 38e73aa..61caa69 100644 --- a/Sources/Defaults/Defaults+Extensions.swift +++ b/Sources/Defaults/Defaults+Extensions.swift @@ -145,6 +145,9 @@ extension Color: Defaults.Serializable { public static let bridge = Defaults.ColorBridge() } +@available(macOS 14.0, iOS 17.0, tvOS 17.0, watchOS 9.0, visionOS 1.0, *) +extension Color.Resolved: Defaults.Serializable {} + extension Range: Defaults.RangeSerializable where Bound: Defaults.Serializable { public static var bridge: Defaults.RangeBridge { Defaults.RangeBridge() } } diff --git a/Tests/DefaultsTests/DefaultsColorTests.swift b/Tests/DefaultsTests/DefaultsColorTests.swift index 83a9502..1ea8952 100644 --- a/Tests/DefaultsTests/DefaultsColorTests.swift +++ b/Tests/DefaultsTests/DefaultsColorTests.swift @@ -2,7 +2,7 @@ import SwiftUI import Defaults import XCTest -@available(iOS 15, tvOS 15, watchOS 8, visionOS 1.0, *) +@available(macOS 12, iOS 15, tvOS 15, watchOS 8, visionOS 1.0, *) final class DefaultsColorTests: XCTestCase { override func setUp() { super.setUp() @@ -22,3 +22,23 @@ final class DefaultsColorTests: XCTestCase { XCTAssertEqual(Defaults[key]?.cgColor, fixture.cgColor) } } + +@available(macOS 14, iOS 17, tvOS 17, watchOS 9, visionOS 1.0, *) +final class DefaultsColorResolvedTests: XCTestCase { + override func setUp() { + super.setUp() + Defaults.removeAll() + } + + override func tearDown() { + super.tearDown() + Defaults.removeAll() + } + + func test() { + let fixture = Color(.displayP3, red: 1, green: 0.3, blue: 0.7, opacity: 1).resolve(in: .init()) + let key = Defaults.Key("independentColorResolvedKey") + Defaults[key] = fixture + XCTAssertEqual(Defaults[key]?.cgColor, fixture.cgColor) + } +} diff --git a/readme.md b/readme.md index 42ed01f..7566f17 100644 --- a/readme.md +++ b/readme.md @@ -57,6 +57,7 @@ Add `https://github.com/sindresorhus/Defaults` in the [“Swift Package Manager - `NSColor` (macOS) - `UIColor` (iOS) - `Color` [^1] (SwiftUI) +- `Color.Resolved` [^1] (SwiftUI) - `Codable` - `NSSecureCoding` - `Range`, `ClosedRange`