Add `.defaultValue` and `.isDefaultValue` to `@Default` (#76)
This commit is contained in:
parent
6ff23ff1f8
commit
6e42795824
|
@ -87,6 +87,9 @@ public struct Default<Value: Defaults.Serializable>: DynamicProperty {
|
||||||
|
|
||||||
public var projectedValue: Binding<Value> { $observable.value }
|
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.
|
/// Combine publisher that publishes values when the `Defaults` item changes.
|
||||||
public var publisher: Publisher { Defaults.publisher(key) }
|
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, *)
|
@available(macOS 11, iOS 14, tvOS 14, watchOS 7, *)
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -44,5 +44,7 @@ final class DefaultsSwiftUITests: XCTestCase {
|
||||||
XCTAssertTrue(view.hasUnicorn)
|
XCTAssertTrue(view.hasUnicorn)
|
||||||
XCTAssertEqual(view.user.username, "Chen")
|
XCTAssertEqual(view.user.username, "Chen")
|
||||||
XCTAssertEqual(view.setInt, Set(1...4))
|
XCTAssertEqual(view.setInt, Set(1...4))
|
||||||
|
XCTAssertFalse(Default(.hasUnicorn).defaultValue)
|
||||||
|
XCTAssertFalse(Default(.hasUnicorn).isDefaultValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue