Add support for serializing `Color.Resolved` (#171)

This commit is contained in:
Sindre Sorhus 2024-04-20 16:12:50 +07:00 committed by GitHub
parent 17fddec4d9
commit ce45591c09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 1 deletions

View File

@ -145,6 +145,9 @@ extension Color: Defaults.Serializable {
public static let bridge = Defaults.ColorBridge() 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 { extension Range: Defaults.RangeSerializable where Bound: Defaults.Serializable {
public static var bridge: Defaults.RangeBridge<Range> { Defaults.RangeBridge() } public static var bridge: Defaults.RangeBridge<Range> { Defaults.RangeBridge() }
} }

View File

@ -2,7 +2,7 @@ import SwiftUI
import Defaults import Defaults
import XCTest 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 { final class DefaultsColorTests: XCTestCase {
override func setUp() { override func setUp() {
super.setUp() super.setUp()
@ -22,3 +22,23 @@ final class DefaultsColorTests: XCTestCase {
XCTAssertEqual(Defaults[key]?.cgColor, fixture.cgColor) 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<Color.Resolved?>("independentColorResolvedKey")
Defaults[key] = fixture
XCTAssertEqual(Defaults[key]?.cgColor, fixture.cgColor)
}
}

View File

@ -57,6 +57,7 @@ Add `https://github.com/sindresorhus/Defaults` in the [“Swift Package Manager
- `NSColor` (macOS) - `NSColor` (macOS)
- `UIColor` (iOS) - `UIColor` (iOS)
- `Color` [^1] (SwiftUI) - `Color` [^1] (SwiftUI)
- `Color.Resolved` [^1] (SwiftUI)
- `Codable` - `Codable`
- `NSSecureCoding` - `NSSecureCoding`
- `Range`, `ClosedRange` - `Range`, `ClosedRange`