diff --git a/Sources/Defaults/Observation+Combine.swift b/Sources/Defaults/Observation+Combine.swift index c889ef4..d1463ca 100644 --- a/Sources/Defaults/Observation+Combine.swift +++ b/Sources/Defaults/Observation+Combine.swift @@ -132,59 +132,15 @@ extension Defaults { Publisher for multiple `Key` observation, but without specific information about changes. */ @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *) - public static func publisher( - keys: Key..., + public static func publisher( + keys: _DefaultsBaseKey..., options: ObservationOptions = [.initial] ) -> AnyPublisher { let initial = Empty(completeImmediately: false).eraseToAnyPublisher() let combinedPublisher = keys.map { key in - Defaults.publisher(key, options: options) - .map { _ in () } - .eraseToAnyPublisher() - }.reduce(initial) { (combined, keyPublisher) in - combined.merge(with: keyPublisher).eraseToAnyPublisher() - } - - return combinedPublisher - } - - /** - Publisher for multiple `NSSecureCodingKey` observation, but without specific information about changes. - */ - @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *) - public static func publisher( - keys: NSSecureCodingKey..., - options: ObservationOptions = [.initial] - ) -> AnyPublisher { - let initial = Empty(completeImmediately: false).eraseToAnyPublisher() - - let combinedPublisher = - keys.map { key in - Defaults.publisher(key, options: options) - .map { _ in () } - .eraseToAnyPublisher() - }.reduce(initial) { (combined, keyPublisher) in - combined.merge(with: keyPublisher).eraseToAnyPublisher() - } - - return combinedPublisher - } - - /** - Publisher for multiple `NSSecureCodingOptionalKey` observation, but without specific information about changes. - */ - @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *) - public static func publisher( - keys: NSSecureCodingOptionalKey..., - options: ObservationOptions = [.initial] - ) -> AnyPublisher { - let initial = Empty(completeImmediately: false).eraseToAnyPublisher() - - let combinedPublisher = - keys.map { key in - Defaults.publisher(key, options: options) + DefaultsPublisher(suite: key.suite, key: key.name, options: options) .map { _ in () } .eraseToAnyPublisher() }.reduce(initial) { (combined, keyPublisher) in diff --git a/Tests/DefaultsTests/DefaultsTests.swift b/Tests/DefaultsTests/DefaultsTests.swift index e6d2906..56e98e9 100644 --- a/Tests/DefaultsTests/DefaultsTests.swift +++ b/Tests/DefaultsTests/DefaultsTests.swift @@ -305,7 +305,7 @@ final class DefaultsTests: XCTestCase { @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *) func testObserveMultipleKeysCombine() { - let key1 = Defaults.Key("observeKey1", default: false) + let key1 = Defaults.Key("observeKey1", default: "x") let key2 = Defaults.Key("observeKey2", default: true) let expect = expectation(description: "Observation closure being called") @@ -315,14 +315,13 @@ final class DefaultsTests: XCTestCase { expect.fulfill() } - Defaults[key1] = true + Defaults[key1] = "y" Defaults[key2] = false cancellable.cancel() waitForExpectations(timeout: 10) } - @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *) func testObserveMultipleNSSecureKeysCombine() { let key1 = Defaults.NSSecureCodingKey("observeNSSecureCodingKey1", default: ExamplePersistentHistory(value: "TestValue")) @@ -345,7 +344,7 @@ final class DefaultsTests: XCTestCase { @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *) func testObserveMultipleOptionalKeysCombine() { - let key1 = Defaults.Key("observeOptionalKey1") + let key1 = Defaults.Key("observeOptionalKey1") let key2 = Defaults.Key("observeOptionalKey2") let expect = expectation(description: "Observation closure being called") @@ -355,7 +354,7 @@ final class DefaultsTests: XCTestCase { expect.fulfill() } - Defaults[key1] = true + Defaults[key1] = "x" Defaults[key2] = false cancellable.cancel() diff --git a/readme.md b/readme.md index 529d6aa..54c7ae2 100644 --- a/readme.md +++ b/readme.md @@ -318,7 +318,7 @@ Type: `class` Create a NSSecureCoding key with an optional value. -#### `Defaults.reset(key, …)` +#### `Defaults.reset(keys…)` Type: `func` @@ -360,7 +360,7 @@ Observe changes to a key or an optional key. By default, it will also trigger an initial event on creation. This can be useful for setting default values on controls. You can override this behavior with the `options` argument. -#### `Defaults.publisher` +#### `Defaults.publisher(_ key:, options:)` ```swift Defaults.publisher( @@ -389,28 +389,7 @@ Observation API using [Publisher](https://developer.apple.com/documentation/comb Available on macOS 10.15+, iOS 13.0+, tvOS 13.0+, and watchOS 6.0+. -#### `Defaults.publisher(keys:)` - -```swift -Defaults.publisher( - keys: Defaults.Key..., - options: ObservationOptions = [.initial] -) -> AnyPublisher { -``` - -```swift -Defaults.publisher( - keys: Defaults.NSSecureCodingKey..., - options: ObservationOptions = [.initial] -) -> AnyPublisher { -``` - -```swift -Defaults.publisher( - keys: Defaults.NSSecureCodingOptionalKey..., - options: ObservationOptions = [.initial] -) -> AnyPublisher { -``` +#### `Defaults.publisher(keys: keys…, options:)` Type: `func`