-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushNotificationViewModel.swift
More file actions
149 lines (135 loc) · 4.32 KB
/
PushNotificationViewModel.swift
File metadata and controls
149 lines (135 loc) · 4.32 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
//
// NotificationViewModel.swift
// DevLog
//
// Created by 최윤진 on 11/22/25.
//
import Foundation
final class PushNotificationViewModel: Store {
struct State {
var notifications: [PushNotification] = []
var showAlert: Bool = false
var showToast: Bool = false
var alertTitle: String = ""
var alertType: AlertType?
var alertMessage: String = ""
var toastMessage: String = ""
var toastType: ToastType?
var isLoading: Bool = false
var pendingTask: (PushNotification, Int)?
}
enum Action {
case fetchNotifications
case deleteNotification(PushNotification)
case undoDelete
case confirmDelete
case setAlert(isPresented: Bool, type: AlertType? = nil)
case setToast(isPresented: Bool, type: ToastType? = nil)
case setLoading(Bool)
case setNotifications([PushNotification])
}
enum SideEffect {
case fetch
case delete(PushNotification)
}
enum AlertType {
case error
}
enum ToastType {
case delete
}
@Published private(set) var state: State = .init()
private let fetchUseCase: FetchPushNotificationsUseCase
private let deleteUseCase: DeletePushNotificationUseCase
init(
fetchUseCase: FetchPushNotificationsUseCase,
deleteUseCase: DeletePushNotificationUseCase
) {
self.fetchUseCase = fetchUseCase
self.deleteUseCase = deleteUseCase
}
func reduce(with action: Action) -> [SideEffect] {
var state = self.state
switch action {
case .fetchNotifications:
return [.fetch]
case .deleteNotification(let item):
guard let index = state.notifications.firstIndex(where: { $0.id == item.id }) else {
return []
}
state.pendingTask = (item, index)
state.notifications.remove(at: index)
setToast(&state, isPresented: true, for: .delete)
case .undoDelete:
guard let (item, index) = state.pendingTask else { return [] }
state.notifications.insert(item, at: index)
state.pendingTask = nil
case .confirmDelete:
guard let (item, _ ) = state.pendingTask else {
return []
}
return [.delete(item)]
case .setAlert(let isPresented, let type):
setAlert(isPresented: isPresented, for: type)
return []
case .setToast(let isPresented, let type):
setToast(&state, isPresented: isPresented, for: type)
case .setLoading(let value):
state.isLoading = value
case .setNotifications(let notifications):
state.notifications = notifications
}
self.state = state
return []
}
func run(_ effect: SideEffect) {
switch effect {
case .fetch:
Task {
do {
defer { send(.setLoading(false)) }
send(.setLoading(true))
let notifications = try await fetchUseCase.execute()
send(.setNotifications(notifications))
} catch {
send(.setAlert(isPresented: true, type: .error))
}
}
case .delete(let notification):
Task {
do {
try await deleteUseCase.execute(notification.id)
} catch {
send(.setAlert(isPresented: true, type: .error))
}
}
}
}
}
private extension PushNotificationViewModel {
func setAlert(isPresented: Bool, for type: AlertType?) {
switch type {
case .error:
state.alertTitle = "오류"
state.alertMessage = "문제가 발생했습니다. 잠시 후 다시 시도해주세요."
case .none:
state.alertTitle = ""
state.alertMessage = ""
}
state.alertType = type
state.showAlert = isPresented
}
func setToast(
_ state: inout State,
isPresented: Bool,
for type: ToastType?
) {
switch type {
case .delete:
state.toastMessage = "실행 취소"
case .none:
state.toastMessage = ""
}
state.showToast = isPresented
}
}