Skip to content

Commit

Permalink
FEAT: UserNotificationClient 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
devMinseok committed Jul 20, 2024
1 parent a302e97 commit bbc9799
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 4 deletions.
Binary file modified DependencyGraph/pomonyang_dev_graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified DependencyGraph/pomonyang_prod_graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ import Foundation
public enum Core: String, Modulable {
case APIClient
case KeychainClient
case UserNotificationClient
}
1 change: 0 additions & 1 deletion Projects/Core/APIClient/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ let project: Project = .makeTMABasedProject(
.testing
],
dependencies: [
.sources: [],
.interface: [
.dependency(rootModule: Shared.self)
]
Expand Down
1 change: 0 additions & 1 deletion Projects/Core/KeychainClient/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ let project: Project = .makeTMABasedProject(
.testing
],
dependencies: [
.sources: [],
.interface: [
.dependency(rootModule: Shared.self)
]
Expand Down
77 changes: 77 additions & 0 deletions Projects/Core/UserNotificationClient/Interface/Interface.swift
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()
}
20 changes: 20 additions & 0 deletions Projects/Core/UserNotificationClient/Project.swift
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 Projects/Core/UserNotificationClient/Sources/Implementation.swift
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) }
)
}
}
}
1 change: 0 additions & 1 deletion Projects/Domain/AppService/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ let project: Project = .makeTMABasedProject(
.testing
],
dependencies: [
.sources: [],
.interface: [
.dependency(rootModule: Core.self)
]
Expand Down
1 change: 0 additions & 1 deletion Projects/Feature/AppFeature/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ let project: Project = .makeTMABasedProject(
.testing
],
dependencies: [
.sources: [],
.interface: [
.dependency(rootModule: Domain.self)
]
Expand Down

0 comments on commit bbc9799

Please sign in to comment.