Skip to content

Commit

Permalink
Make return type optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Feb 19, 2024
1 parent c37f675 commit faa3385
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public protocol NotificationHandler {
///
/// - Parameter notification: The notification that is about to be delivered.
/// - Returns: The option for notifying the user. Use `[]` to silence the notification.
func receiveIncomingNotification(_ notification: UNNotification) async -> UNNotificationPresentationOptions
func receiveIncomingNotification(_ notification: UNNotification) async -> UNNotificationPresentationOptions?

#if !os(macOS)
/// Handle remote notification when the app is running in background.
Expand Down Expand Up @@ -74,9 +74,8 @@ extension NotificationHandler {
#endif

/// Empty default implementation.
func receiveIncomingNotification(_ notification: UNNotification) async -> UNNotificationPresentationOptions {
// TODO: is there are better default?
[.badge, .badge, .list, .sound] // default is to fully present the notification
func receiveIncomingNotification(_ notification: UNNotification) async -> UNNotificationPresentationOptions? {
nil
}

Check warning on line 79 in Sources/Spezi/Capabilities/Notifications/NotificationHandler.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Spezi/Capabilities/Notifications/NotificationHandler.swift#L77-L79

Added lines #L77 - L79 were not covered by tests

#if !os(macOS)
Expand Down
19 changes: 15 additions & 4 deletions Sources/Spezi/Spezi/SpeziNotificationCenterDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,28 @@ class SpeziNotificationCenterDelegate: NSObject, UNUserNotificationCenterDelegat
}


return await withTaskGroup(of: UNNotificationPresentationOptions.self) { group in
return await withTaskGroup(of: UNNotificationPresentationOptions?.self) { group in
for handler in delegate.spezi.notificationHandler {
group.addTask {
await handler.receiveIncomingNotification(notification)
}
}

// TODO: fine to just merge all options? (this doesn't work with the empty default implementation!)
return await group.reduce(into: []) { result, options in

var hasSpecified = false
let unionOptions: UNNotificationPresentationOptions = await group.reduce(into: []) { result, options in
guard let options else {
return
}

hasSpecified = true
result.formUnion(options)
}

if hasSpecified {
return unionOptions
} else {
return [.badge, .badge, .list, .sound]
}
}
}

Check warning on line 65 in Sources/Spezi/Spezi/SpeziNotificationCenterDelegate.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Spezi/Spezi/SpeziNotificationCenterDelegate.swift#L36-L65

Added lines #L36 - L65 were not covered by tests
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/UITests/TestAppUITests/LifecycleHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import XCTestExtensions

final class LifecycleHandlerTests: XCTestCase {
func testLifecycleHandler() throws {
#if os(macOS) || os(watchOS)
#if os(macOS) || os(watchOS)
throw XCTSkip("LifecycleHandler is not supported on macOS or watchOS.")
#endif

Expand Down

0 comments on commit faa3385

Please sign in to comment.