Add `.defaultValue` and `.isDefaultValue` to `@Default` (#76)

This commit is contained in:
Sindre Sorhus 2021-06-18 13:15:13 +07:00 committed by GitHub
parent 6ff23ff1f8
commit 6e42795824
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -87,6 +87,9 @@ public struct Default<Value: Defaults.Serializable>: DynamicProperty {
public var projectedValue: Binding<Value> { $observable.value }
/// The default value of the key.
public var defaultValue: Value { key.defaultValue }
/// Combine publisher that publishes values when the `Defaults` item changes.
public var publisher: Publisher { Defaults.publisher(key) }
@ -118,6 +121,12 @@ public struct Default<Value: Defaults.Serializable>: DynamicProperty {
}
}
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Default where Value: Equatable {
/// Indicates whether the value is the same as the default value.
public var isDefaultValue: Bool { wrappedValue == defaultValue }
}
@available(macOS 11, iOS 14, tvOS 14, watchOS 7, *)
extension Defaults {
/**

View File

@ -44,5 +44,7 @@ final class DefaultsSwiftUITests: XCTestCase {
XCTAssertTrue(view.hasUnicorn)
XCTAssertEqual(view.user.username, "Chen")
XCTAssertEqual(view.setInt, Set(1...4))
XCTAssertFalse(Default(.hasUnicorn).defaultValue)
XCTAssertFalse(Default(.hasUnicorn).isDefaultValue)
}
}