@@ -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}
0 commit comments