-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushNotificationRepositoryImpl.swift
More file actions
224 lines (198 loc) · 7.39 KB
/
Copy pathPushNotificationRepositoryImpl.swift
File metadata and controls
224 lines (198 loc) · 7.39 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// PushNotificationRepositoryImpl.swift
// DevLogData
//
// Created by 최윤진 on 1/18/26.
//
import Foundation
import Combine
import DevLogCore
import DevLogDomain
final class PushNotificationRepositoryImpl: PushNotificationRepository {
private enum Key {
static let preferences = "TodoCategory.preferences"
}
private let pushNotificationService: PushNotificationService
private let todoCategoryService: TodoCategoryService
private let store: MemoryCacheStore
init(
pushNotificationService: PushNotificationService,
todoCategoryService: TodoCategoryService,
store: MemoryCacheStore
) {
self.pushNotificationService = pushNotificationService
self.todoCategoryService = todoCategoryService
self.store = store
}
/// 푸시 알림 On/Off 설정
func fetchPushNotificationEnabled() async throws -> Bool {
do {
return try await pushNotificationService.fetchPushNotificationEnabled()
} catch {
throw error.toDomain()
}
}
/// 푸시 알림 시간 설정
func fetchPushNotificationTime() async throws -> DateComponents {
do {
return try await pushNotificationService.fetchPushNotificationTime()
} catch {
throw error.toDomain()
}
}
/// 푸시 알림 설정 업데이트
func updatePushNotificationSettings(_ settings: PushNotificationSettings) async throws {
do {
try await pushNotificationService.updatePushNotificationSettings(
isEnabled: settings.isEnabled, components: settings.scheduledTime
)
} catch {
throw error.toDomain()
}
}
/// 푸시 알림 기록 요청
func requestNotifications(
_ query: PushNotificationQuery,
cursor: PushNotificationCursor?
) async throws -> PushNotificationPage {
do {
let cursorDTO = cursor.map { PushNotificationCursorDTO.fromDomain($0) }
async let responseTask = pushNotificationService.requestNotifications(query, cursor: cursorDTO)
async let preferencesTask = todoCategoryPreferenceResponses()
let (response, preferenceResponses) = try await (responseTask, preferencesTask)
return try resolvePage(from: response, with: preferenceResponses.toDomain())
} catch {
throw error.toDomain()
}
}
func observeNotifications(
_ query: PushNotificationQuery,
limit: Int
) throws -> AnyPublisher<PushNotificationPage, Error> {
let subject = PassthroughSubject<PushNotificationPage, Error>()
var cancellable: AnyCancellable?
do {
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.toDomain()))
}
},
receiveValue: { [weak self] response in
guard let self else { return }
Task {
do {
let preferences = try await self.todoCategoryPreferenceResponses()
.toDomain()
let page = try self.resolvePage(from: response, with: preferences)
subject.send(page)
} catch {
subject.send(completion: .failure(error.toDomain()))
}
}
}
)
} catch {
throw error.toDomain()
}
return subject
.handleEvents(receiveCancel: { cancellable?.cancel() })
.eraseToAnyPublisher()
}
func observeUnreadPushCount() throws -> AnyPublisher<Int, Error> {
do {
return try pushNotificationService.observeUnreadPushCount()
.mapError { $0.toDomain() }
.eraseToAnyPublisher()
} catch {
throw error.toDomain()
}
}
// 푸시 알림 기록 삭제
func deleteNotification(_ notificationID: String) async throws {
do {
try await pushNotificationService.deleteNotification(notificationID)
} catch {
throw error.toDomain()
}
}
func undoDeleteNotification(_ notificationID: String) async throws {
do {
try await pushNotificationService.undoDeleteNotification(notificationID)
} catch {
throw error.toDomain()
}
}
// 푸시 알림 읽음/안읽음 토글
func toggleNotificationRead(_ todoId: String) async throws {
do {
try await pushNotificationService.toggleNotificationRead(todoId)
} catch {
throw error.toDomain()
}
}
}
private extension PushNotificationRepositoryImpl {
func todoCategoryPreferenceResponses() async throws -> [TodoCategoryPreferenceResponse] {
if let preferences: [TodoCategoryPreferenceResponse] = store.value(forKey: Key.preferences) {
return preferences
}
let preferences = try await todoCategoryService.fetchCategoryPreferences()
store.setValue(preferences, forKey: Key.preferences)
return preferences
}
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)
)
}
}