Skip to content

Commit e924f69

Browse files
committed
feat: 토스트를 탭 하면 알람 리스트에서 제거를 취소하도록 추가
1 parent 62297cd commit e924f69

2 files changed

Lines changed: 56 additions & 7 deletions

File tree

DevLog/Presentation/ViewModel/PushNotificationViewModel.swift

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,23 @@ final class PushNotificationViewModel: Store {
1111
struct State {
1212
var notifications: [PushNotification] = []
1313
var showAlert: Bool = false
14+
var showToast: Bool = false
1415
var alertTitle: String = ""
1516
var alertType: AlertType?
1617
var alertMessage: String = ""
18+
var toastMessage: String = ""
19+
var toastType: ToastType?
1720
var isLoading: Bool = false
21+
var pendingTask: (PushNotification, Int)?
1822
}
1923

2024
enum Action {
2125
case fetchNotifications
22-
case deleteNotification(PushNotification, fromEffect: Bool = false)
26+
case deleteNotification(PushNotification)
27+
case undoDelete
28+
case confirmDelete
2329
case setAlert(isPresented: Bool, type: AlertType? = nil)
30+
case setToast(isPresented: Bool, type: ToastType? = nil)
2431
case setLoading(Bool)
2532
case setNotifications([PushNotification])
2633
}
@@ -34,6 +41,10 @@ final class PushNotificationViewModel: Store {
3441
case error
3542
}
3643

44+
enum ToastType {
45+
case delete
46+
}
47+
3748
@Published private(set) var state: State = .init()
3849
private let fetchUseCase: FetchPushNotificationsUseCase
3950
private let deleteUseCase: DeletePushNotificationUseCase
@@ -52,12 +63,27 @@ final class PushNotificationViewModel: Store {
5263
switch action {
5364
case .fetchNotifications:
5465
return [.fetch]
55-
case .deleteNotification(let item, let fromEffect):
56-
if !fromEffect { return [.delete(item)] }
57-
state.notifications.removeAll { $0.id == item.id }
66+
case .deleteNotification(let item):
67+
guard let index = state.notifications.firstIndex(where: { $0.id == item.id }) else {
68+
return []
69+
}
70+
state.pendingTask = (item, index)
71+
state.notifications.remove(at: index)
72+
setToast(&state, isPresented: true, for: .delete)
73+
case .undoDelete:
74+
guard let (item, index) = state.pendingTask else { return [] }
75+
state.notifications.insert(item, at: index)
76+
state.pendingTask = nil
77+
case .confirmDelete:
78+
guard let (item, _ ) = state.pendingTask else {
79+
return []
80+
}
81+
return [.delete(item)]
5882
case .setAlert(let isPresented, let type):
5983
setAlert(isPresented: isPresented, for: type)
6084
return []
85+
case .setToast(let isPresented, let type):
86+
setToast(&state, isPresented: isPresented, for: type)
6187
case .setLoading(let value):
6288
state.isLoading = value
6389
case .setNotifications(let notifications):
@@ -84,10 +110,7 @@ final class PushNotificationViewModel: Store {
84110
case .delete(let notification):
85111
Task {
86112
do {
87-
defer { send(.setLoading(false)) }
88-
send(.setLoading(true))
89113
try await deleteUseCase.execute(notification.id)
90-
send(.deleteNotification(notification, fromEffect: true))
91114
} catch {
92115
send(.setAlert(isPresented: true, type: .error))
93116
}
@@ -109,4 +132,18 @@ private extension PushNotificationViewModel {
109132
state.alertType = type
110133
state.showAlert = isPresented
111134
}
135+
136+
func setToast(
137+
_ state: inout State,
138+
isPresented: Bool,
139+
for type: ToastType?
140+
) {
141+
switch type {
142+
case .delete:
143+
state.toastMessage = "실행 취소"
144+
case .none:
145+
state.toastMessage = ""
146+
}
147+
state.showToast = isPresented
148+
}
112149
}

DevLog/UI/PushNotification/PushNotificationView.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ struct PushNotificationView: View {
3939
} message: {
4040
Text(viewModel.state.alertMessage)
4141
}
42+
.toast(
43+
isPresented: Binding(
44+
get: { viewModel.state.showToast },
45+
set: { viewModel.send(.setToast(isPresented: $0)) }),
46+
duration: 5,
47+
message: viewModel.state.toastMessage,
48+
action: {
49+
viewModel.send(.undoDelete)
50+
}
51+
) {
52+
viewModel.send(.confirmDelete)
53+
}
4254
.onAppear {
4355
viewModel.send(.fetchNotifications)
4456
}

0 commit comments

Comments
 (0)