Skip to content

Commit

Permalink
Add .defaultValue and .isDefaultValue to @Default (sindresorhus#76
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sindresorhus authored and dezinezync committed Dec 17, 2022
1 parent 6557be8 commit 9691a7c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Sources/Defaults/SwiftUI.swift
Expand Up @@ -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) }

Expand Down Expand Up @@ -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 {
/**
Expand Down
2 changes: 2 additions & 0 deletions Tests/DefaultsTests/DefaultsSwiftUITests.swift
Expand Up @@ -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)
}
}

0 comments on commit 9691a7c

Please sign in to comment.