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

[CAT-49] UserNotificationClient 모듈 추가 #10

Merged
merged 2 commits into from
Jul 21, 2024
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
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
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@
- ```make clean```

### 스크립트
- 그래프 생성 (Example, ThirdParty, Dev, Tests 타겟 제외)
- 그래프 생성
- ```make graph```
- Xcode 파일 생성 템플릿 추가
- ```make template```

### 의존성 그래프 (외부의존성 제외)
- 운영 환경
![PomoNyang](DependencyGraph/pomonyang_prod_graph.png)
- 개발 환경
![PomoNyang](DependencyGraph/pomonyang_dev_graph.png)
<details>
<summary>운영 환경</summary>

![PROD](DependencyGraph/pomonyang_prod_graph.png)
</details>

<details>
<summary>개발 환경</summary>

![DEV](DependencyGraph/pomonyang_dev_graph.png)
</details>
Loading