-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushNotificationRepositoryImpl.swift
More file actions
167 lines (144 loc) · 5.81 KB
/
PushNotificationRepositoryImpl.swift
File metadata and controls
167 lines (144 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// PushNotificationRepositoryImpl.swift
// DevLog
//
// Created by 최윤진 on 1/18/26.
//
import Foundation
import Combine
final class PushNotificationRepositoryImpl: PushNotificationRepository {
private let pushNotificationService: PushNotificationService
private let todoCategoryService: TodoCategoryService
init(
pushNotificationService: PushNotificationService,
todoCategoryService: TodoCategoryService
) {
self.pushNotificationService = pushNotificationService
self.todoCategoryService = todoCategoryService
}
/// 푸시 알림 On/Off 설정
func fetchPushNotificationEnabled() async throws -> Bool {
return try await pushNotificationService.fetchPushNotificationEnabled()
}
/// 푸시 알림 시간 설정
func fetchPushNotificationTime() async throws -> DateComponents {
return try await pushNotificationService.fetchPushNotificationTime()
}
/// 푸시 알림 설정 업데이트
func updatePushNotificationSettings(_ settings: PushNotificationSettings) async throws {
try await pushNotificationService.updatePushNotificationSettings(
isEnabled: settings.isEnabled, components: settings.scheduledTime
)
}
/// 푸시 알림 기록 요청
func requestNotifications(
_ query: PushNotificationQuery,
cursor: PushNotificationCursor?
) async throws -> PushNotificationPage {
let cursorDTO = cursor.map { PushNotificationCursorDTO.fromDomain($0) }
async let responseTask = pushNotificationService.requestNotifications(query, cursor: cursorDTO)
async let preferencesTask = todoCategoryService.fetchPreferences()
let (response, preferences) = try await (responseTask, preferencesTask)
return try resolvePage(from: response, with: preferences)
}
func observeNotifications(
_ query: PushNotificationQuery,
limit: Int
) throws -> AnyPublisher<PushNotificationPage, Error> {
let subject = PassthroughSubject<PushNotificationPage, Error>()
var cancellable: AnyCancellable?
cancellable = try pushNotificationService.observeNotifications(query, limit: limit)
.sink(
receiveCompletion: { completion in
switch completion {
case .finished:
subject.send(completion: .finished)
case .failure(let error):
subject.send(completion: .failure(error))
}
},
receiveValue: { [weak self] response in
guard let self else { return }
Task {
do {
let preferences = try await self.todoCategoryService.fetchPreferences()
let page = try self.resolvePage(from: response, with: preferences)
subject.send(page)
} catch {
subject.send(completion: .failure(error))
}
}
}
)
return subject
.handleEvents(receiveCancel: { cancellable?.cancel() })
.eraseToAnyPublisher()
}
func observeUnreadPushCount() throws -> AnyPublisher<Int, Error> {
try pushNotificationService.observeUnreadPushCount()
.eraseToAnyPublisher()
}
// 푸시 알림 기록 삭제
func deleteNotification(_ notificationID: String) async throws {
try await pushNotificationService.deleteNotification(notificationID)
}
func undoDeleteNotification(_ notificationID: String) async throws {
try await pushNotificationService.undoDeleteNotification(notificationID)
}
// 푸시 알림 읽음/안읽음 토글
func toggleNotificationRead(_ todoId: String) async throws {
try await pushNotificationService.toggleNotificationRead(todoId)
}
}
private extension PushNotificationRepositoryImpl {
func resolvePage(
from response: PushNotificationPageResponse,
with preferences: [TodoCategoryPreference]
) throws -> PushNotificationPage {
let userTodoCategories: [UserTodoCategory] = preferences.compactMap { preference in
guard case .user(let userTodoCategory) = preference.category else {
return nil
}
return userTodoCategory
}
let responses = try response.items.map {
try resolve($0, userTodoCategories: userTodoCategories)
}
return try PushNotificationPageResponse(
items: responses,
nextCursor: response.nextCursor
).toDomain()
}
// resolvePage() 메서드에서만 사용됨
private func resolve(
_ response: PushNotificationResponse,
userTodoCategories: [UserTodoCategory]
) throws -> PushNotificationResponse {
let id: String
switch response.todoCategory {
case .raw(let rawValue):
id = rawValue
case .decoded:
return response
}
let todoCategory: TodoCategory
if let systemTodoCategory = SystemTodoCategory(rawValue: id) {
todoCategory = .system(systemTodoCategory)
} else if let userTodoCategory = userTodoCategories.first(where: {
$0.id == id
}) {
todoCategory = .user(userTodoCategory)
} else {
throw DataError.invalidData("PushNotificationResponse.todoCategory is invalid: \(id)")
}
return PushNotificationResponse(
id: response.id,
title: response.title,
body: response.body,
receivedAt: response.receivedAt,
isRead: response.isRead,
todoId: response.todoId,
todoCategory: .decoded(todoCategory)
)
}
}