Skip to content

Commit 37530ca

Browse files
committed
Added notification wrapper.
1 parent fd22fb6 commit 37530ca

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ let package = Package(
1616
)
1717
],
1818
dependencies: [
19-
.package(url: "https://github.com/sparrowcode/SwiftBoost", .upToNextMajor(from: "4.0.8")),
20-
.package(url: "https://github.com/siteline/swiftui-introspect", .upToNextMajor(from: "1.2.0"))
19+
.package(url: "https://github.com/sparrowcode/SwiftBoost", .upToNextMajor(from: "4.0.9")),
20+
.package(url: "https://github.com/siteline/swiftui-introspect", .upToNextMajor(from: "1.3.0"))
2121
],
2222
targets: [
2323
.target(
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
import SwiftUI
2+
import Combine
23

34
extension View {
45

5-
public func onReceive(notification name: Notification.Name, perform action: @escaping (NotificationCenter.Publisher.Output) -> Void) -> some View {
6-
self.onReceive(NotificationCenter.default.publisher(for: name), perform: action)
6+
public func onReceive(
7+
_ name: Notification.Name,
8+
perform action: @escaping (NotificationCenter.Publisher.Output) -> Void) -> some View {
9+
self.onReceive(NotificationCenter.default.publisher(for: name), perform: action)
10+
}
11+
12+
public func onReceive(
13+
_ names: Notification.Name...,
14+
center: NotificationCenter = .default,
15+
object: AnyObject? = nil,
16+
perform action: @escaping (Notification) -> Void
17+
) -> some View {
18+
19+
let mergedPublisher = names.map { name in
20+
center.publisher(for: name, object: object)
21+
}.reduce(Empty<Notification, Never>().eraseToAnyPublisher()) { merged, publisher in
22+
merged.merge(with: publisher).eraseToAnyPublisher()
23+
}
24+
25+
return self.onReceive(mergedPublisher, perform: action)
726
}
827
}

0 commit comments

Comments
 (0)