-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CAT-49] UserNotificationClient 모듈 추가 (#10)
- Loading branch information
1 parent
9269c57
commit fb6aad7
Showing
11 changed files
with
211 additions
and
9 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
Projects/Core/UserNotificationClient/Interface/Interface.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// Interface.swift | ||
// UserNotificationClient | ||
// | ||
// Created by devMinseok on 7/20/24. | ||
// Copyright © 2024 PomoNyang. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import UserNotifications | ||
|
||
import ComposableArchitecture | ||
|
||
@DependencyClient | ||
public struct UserNotificationClient { | ||
public var add: @Sendable (UNNotificationRequest) async throws -> Void | ||
public var delegate: @Sendable () -> AsyncStream<DelegateEvent> = { .finished } | ||
public var getNotificationSettings: @Sendable () async -> Notification.Settings = { | ||
Notification.Settings(authorizationStatus: .notDetermined) | ||
} | ||
public var removeDeliveredNotificationsWithIdentifiers: @Sendable ([String]) async -> Void | ||
public var removePendingNotificationRequestsWithIdentifiers: @Sendable ([String]) async -> Void | ||
public var requestAuthorization: @Sendable (UNAuthorizationOptions) async throws -> Bool | ||
|
||
@CasePathable | ||
public enum DelegateEvent { | ||
case didReceiveResponse(Notification.Response, completionHandler: @Sendable () -> Void) | ||
case openSettingsForNotification(Notification?) | ||
case willPresentNotification( | ||
Notification, completionHandler: @Sendable (UNNotificationPresentationOptions) -> Void | ||
) | ||
} | ||
|
||
public struct Notification: Equatable { | ||
public var date: Date | ||
public var request: UNNotificationRequest | ||
|
||
public init( | ||
date: Date, | ||
request: UNNotificationRequest | ||
) { | ||
self.date = date | ||
self.request = request | ||
} | ||
|
||
public struct Response: Equatable { | ||
public var notification: Notification | ||
|
||
public init(notification: Notification) { | ||
self.notification = notification | ||
} | ||
} | ||
|
||
public struct Settings: Equatable { | ||
public var authorizationStatus: UNAuthorizationStatus | ||
|
||
public init(authorizationStatus: UNAuthorizationStatus) { | ||
self.authorizationStatus = authorizationStatus | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
// MARK: - DependencyValues | ||
|
||
extension DependencyValues { | ||
public var userNotificationClient: UserNotificationClient { | ||
get { self[UserNotificationClient.self] } | ||
set { self[UserNotificationClient.self] = newValue } | ||
} | ||
} | ||
|
||
extension UserNotificationClient: TestDependencyKey { | ||
public static let previewValue = Self() | ||
public static let testValue = Self() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import ProjectDescription | ||
import ProjectDescriptionHelpers | ||
|
||
@_spi(Core) | ||
@_spi(Shared) | ||
import DependencyPlugin | ||
|
||
let project: Project = .makeTMABasedProject( | ||
module: Core.UserNotificationClient, | ||
scripts: [], | ||
targets: [ | ||
.sources, | ||
.interface | ||
], | ||
dependencies: [ | ||
.interface: [ | ||
.dependency(rootModule: Shared.self) | ||
] | ||
] | ||
) |
101 changes: 101 additions & 0 deletions
101
Projects/Core/UserNotificationClient/Sources/Implementation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// | ||
// Implementation.swift | ||
// UserNotificationClient | ||
// | ||
// Created by devMinseok on 7/20/24. | ||
// Copyright © 2024 PomoNyang. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import UserNotifications | ||
|
||
import UserNotificationClientInterface | ||
|
||
import Dependencies | ||
|
||
extension UserNotificationClient: DependencyKey { | ||
public static let liveValue = Self( | ||
add: { try await UNUserNotificationCenter.current().add($0) }, | ||
delegate: { | ||
AsyncStream { continuation in | ||
let delegate = Delegate(continuation: continuation) | ||
UNUserNotificationCenter.current().delegate = delegate | ||
continuation.onTermination = { _ in | ||
_ = delegate | ||
} | ||
} | ||
}, | ||
getNotificationSettings: { | ||
await Notification.Settings( | ||
rawValue: UNUserNotificationCenter.current().notificationSettings() | ||
) | ||
}, | ||
removeDeliveredNotificationsWithIdentifiers: { | ||
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: $0) | ||
}, | ||
removePendingNotificationRequestsWithIdentifiers: { | ||
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: $0) | ||
}, | ||
requestAuthorization: { | ||
try await UNUserNotificationCenter.current().requestAuthorization(options: $0) | ||
} | ||
) | ||
} | ||
|
||
extension UserNotificationClient.Notification { | ||
public init(rawValue: UNNotification) { | ||
self.init(date: rawValue.date, request: rawValue.request) | ||
} | ||
} | ||
|
||
extension UserNotificationClient.Notification.Response { | ||
public init(rawValue: UNNotificationResponse) { | ||
self.init(notification: .init(rawValue: rawValue.notification)) | ||
} | ||
} | ||
|
||
extension UserNotificationClient.Notification.Settings { | ||
public init(rawValue: UNNotificationSettings) { | ||
self.init(authorizationStatus: rawValue.authorizationStatus) | ||
} | ||
} | ||
|
||
extension UserNotificationClient { | ||
fileprivate class Delegate: NSObject, UNUserNotificationCenterDelegate { | ||
let continuation: AsyncStream<UserNotificationClient.DelegateEvent>.Continuation | ||
|
||
init(continuation: AsyncStream<UserNotificationClient.DelegateEvent>.Continuation) { | ||
self.continuation = continuation | ||
} | ||
|
||
func userNotificationCenter( | ||
_ center: UNUserNotificationCenter, | ||
didReceive response: UNNotificationResponse, | ||
withCompletionHandler completionHandler: @escaping () -> Void | ||
) { | ||
self.continuation.yield( | ||
.didReceiveResponse(.init(rawValue: response)) { completionHandler() } | ||
) | ||
} | ||
|
||
func userNotificationCenter( | ||
_ center: UNUserNotificationCenter, | ||
openSettingsFor notification: UNNotification? | ||
) { | ||
self.continuation.yield( | ||
.openSettingsForNotification(notification.map(Notification.init(rawValue:))) | ||
) | ||
} | ||
|
||
func userNotificationCenter( | ||
_ center: UNUserNotificationCenter, | ||
willPresent notification: UNNotification, | ||
withCompletionHandler completionHandler: | ||
@escaping (UNNotificationPresentationOptions) -> Void | ||
) { | ||
self.continuation.yield( | ||
.willPresentNotification(.init(rawValue: notification)) { completionHandler($0) } | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters