Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed usage of stored properties with @available annotation. #17

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/Example/Example/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ struct App: ReducerProtocol {
}

case let .userNotifications(.didReceiveResponse(response, completion)):
let userInfo = response.notification.request.content.userInfo()
let userInfo = response.notification.request.content.userInfo
let notification = UserNotification(userInfo: userInfo)
if case let .count(value) = notification {
state.count = value
Expand Down
36 changes: 6 additions & 30 deletions Examples/Example/ExampleTests/ExampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,7 @@ class ExampleTests: XCTestCase {
await requestedAuthorizationOptions.setValue(options)
return true
}
let task = await store.send(.didFinishLaunching(notification: nil))
await store.receive(.requestAuthorizationResponse(.success(true)))
await requestedAuthorizationOptions.withValue {
XCTAssertNoDifference($0, [.alert, .badge, .sound])
}
await task.cancel()
}

func testApplicationLaunchWithtNotification() async throws {
let delegate = AsyncStream<UserNotificationClient.DelegateAction>.streamWithContinuation()
let requestedAuthorizationOptions = ActorIsolated<UNAuthorizationOptions?>(nil)

let store = TestStore(
initialState: App.State(count: nil),
reducer: App()
)
store.dependencies.userNotifications.delegate = { delegate.stream }
store.dependencies.userNotifications.requestAuthorization = { options in
await requestedAuthorizationOptions.setValue(options)
return true
}

let task = await store.send(.didFinishLaunching(notification: .count(5))) {
$0.count = 5
}
let task = await store.send(.didFinishLaunching)
await store.receive(.requestAuthorizationResponse(.success(true)))
await requestedAuthorizationOptions.withValue {
XCTAssertNoDifference($0, [.alert, .badge, .sound])
Expand All @@ -60,7 +36,7 @@ class ExampleTests: XCTestCase {
store.dependencies.userNotifications.requestAuthorization = { _ in true }
store.dependencies.userNotifications.delegate = { delegate.stream }

let task = await store.send(.didFinishLaunching(notification: nil))
let task = await store.send(.didFinishLaunching)
await store.receive(.requestAuthorizationResponse(.success(true)))

var notificationPresentationOptions: UNNotificationPresentationOptions?
Expand All @@ -71,7 +47,7 @@ class ExampleTests: XCTestCase {
date: Date(timeIntervalSince1970: 0),
request: Notification.Request(
identifier: "fixture",
content: Notification.Content(rawValue: content),
content: content,
trigger: nil
)
)
Expand Down Expand Up @@ -104,7 +80,7 @@ class ExampleTests: XCTestCase {
store.dependencies.userNotifications.requestAuthorization = { _ in true }
store.dependencies.userNotifications.delegate = { delegate.stream }

let task = await store.send(.didFinishLaunching(notification: nil))
let task = await store.send(.didFinishLaunching)
await store.receive(.requestAuthorizationResponse(.success(true)))

var didReceiveResponseCompletionHandlerCalled = false
Expand All @@ -118,7 +94,7 @@ class ExampleTests: XCTestCase {
date: Date(timeIntervalSince1970: 0),
request: Notification.Request(
identifier: "fixture",
content: Notification.Content(rawValue: content),
content: content,
trigger: nil
)
)
Expand Down Expand Up @@ -227,7 +203,7 @@ class ExampleTests: XCTestCase {
XCTAssertTrue($0?.trigger is UNTimeIntervalNotificationTrigger)
XCTAssertEqual(
($0?.trigger as? UNTimeIntervalNotificationTrigger)?.timeInterval,
5
2
)
}
}
Expand Down
27 changes: 16 additions & 11 deletions Sources/ComposableUserNotifications/Interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public struct UserNotificationClient {
/// See `UNUserNotificationCenterDelegate` for more information.
public enum DelegateAction {
case willPresentNotification(
_ notification: Notification,
completionHandler: (UNNotificationPresentationOptions) -> Void)
_ notification: Notification,
completionHandler: (UNNotificationPresentationOptions) -> Void)

@available(tvOS, unavailable)
case didReceiveResponse(_ response: Notification.Response, completionHandler: () -> Void)
Expand All @@ -25,33 +25,38 @@ public struct UserNotificationClient {
}

public var add: @Sendable (UNNotificationRequest) async throws -> Void =
unimplemented("\(Self.self).add")
unimplemented("\(Self.self).add")

@available(tvOS, unavailable)
#if !os(tvOS)
public var deliveredNotifications: @Sendable () async -> [Notification] = unimplemented("\(Self.self).deliveredNotifications")
#endif

@available(tvOS, unavailable)
#if !os(tvOS)
public var notificationCategories: () async -> Set<UNNotificationCategory> = unimplemented("\(Self.self).deliveredNotifications")
#endif

public var notificationSettings: () async -> Notification.Settings = unimplemented("\(Self.self).notificationSettings")

public var pendingNotificationRequests: () async -> [Notification.Request] = unimplemented("\(Self.self).pendingNotificationRequests")

@available(tvOS, unavailable)
#if !os(tvOS)
public var removeAllDeliveredNotifications: () async -> Void = unimplemented("\(Self.self).removeAllDeliveredNotifications")
#endif

public var removeAllPendingNotificationRequests: () async -> Void = unimplemented("\(Self.self).removeAllPendingNotificationRequests")

@available(tvOS, unavailable)
#if !os(tvOS)
public var removeDeliveredNotificationsWithIdentifiers: ([String]) async -> Void = unimplemented("\(Self.self).removeDeliveredNotificationsWithIdentifiers")
#endif

public var removePendingNotificationRequestsWithIdentifiers: ([String]) async -> Void = unimplemented("\(Self.self).removePendingNotificationRequestsWithIdentifiers")

public var requestAuthorization: (UNAuthorizationOptions) async throws -> Bool =
unimplemented("\(Self.self).requestAuthorization")
unimplemented("\(Self.self).requestAuthorization")

@available(tvOS, unavailable)
#if !os(tvOS)
public var setNotificationCategories: (Set<UNNotificationCategory>) async -> Void = unimplemented("\(Self.self).setNotificationCategories")
#endif

public var supportsContentExtensions: () -> Bool = unimplemented("\(Self.self).supportsContentExtensions")

Expand All @@ -66,10 +71,10 @@ extension UserNotificationClient.DelegateAction: Equatable {
switch (lhs, rhs) {
case let (.willPresentNotification(lhs, _), .willPresentNotification(rhs, _)):
return lhs == rhs
#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
case let (.didReceiveResponse(lhs, _), .didReceiveResponse(rhs, _)):
return lhs == rhs
#endif
#endif
case let (.openSettingsForNotification(lhs), .openSettingsForNotification(rhs)):
return lhs == rhs
default:
Expand Down
32 changes: 16 additions & 16 deletions Sources/ComposableUserNotifications/LiveKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,44 @@ extension UserNotificationClient: DependencyKey {
var client = UserNotificationClient()
client.add = { try await UNUserNotificationCenter.current().add($0) }

#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
client.deliveredNotifications = {
let notifications = await center.deliveredNotifications()
return notifications.map(Notification.init(rawValue:))
}
#endif
#endif

client.notificationSettings = {
let settings = await center.notificationSettings()
return Notification.Settings(rawValue: settings)
}

#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
client.notificationCategories = {
await center.notificationCategories()
}
#endif
#endif

client.pendingNotificationRequests = {
let requests = await center.pendingNotificationRequests()
return requests.map(Notification.Request.init(rawValue:))
}

#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
client.removeAllDeliveredNotifications = {
center.removeAllDeliveredNotifications()
}
#endif
#endif

client.removeAllPendingNotificationRequests = {
center.removeAllPendingNotificationRequests()
}

#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
client.removeDeliveredNotificationsWithIdentifiers = {
center.removeDeliveredNotifications(withIdentifiers: $0)
}
#endif
#endif

client.removePendingNotificationRequestsWithIdentifiers = {
center.removePendingNotificationRequests(withIdentifiers: $0)
Expand All @@ -56,11 +56,11 @@ extension UserNotificationClient: DependencyKey {
try await center.requestAuthorization(options: $0)
}

#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
client.setNotificationCategories = {
center.setNotificationCategories($0)
}
#endif
#endif

client.supportsContentExtensions = {
center.supportsContentExtensions
Expand All @@ -75,7 +75,7 @@ extension UserNotificationClient: DependencyKey {
}
}
}

return client
}
}
Expand All @@ -94,15 +94,15 @@ private extension UserNotificationClient {
withCompletionHandler completionHandler:
@escaping (UNNotificationPresentationOptions) -> Void
) {
self.continuation.yield(
self.continuation.yield(
.willPresentNotification(
Notification(rawValue: notification),
completionHandler: completionHandler
)
)
}

#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
#if os(iOS) || os(macOS) || os(watchOS) || targetEnvironment(macCatalyst)
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
Expand All @@ -113,9 +113,9 @@ private extension UserNotificationClient {
.didReceiveResponse(wrappedResponse) { completionHandler() }
)
}
#endif
#endif

#if os(iOS) || os(macOS) || targetEnvironment(macCatalyst)
#if os(iOS) || os(macOS) || targetEnvironment(macCatalyst)
func userNotificationCenter(
_ center: UNUserNotificationCenter,
openSettingsFor notification: UNNotification?
Expand All @@ -125,6 +125,6 @@ private extension UserNotificationClient {
.openSettingsForNotification(mappedNotification)
)
}
#endif
#endif
}
}
Loading