@@ -9,7 +9,7 @@ import Foundation
99
1010@Observable
1111final class PushNotificationListViewModel : Store {
12- struct State {
12+ struct State : Equatable {
1313 var notifications : [ PushNotificationItem ] = [ ]
1414 var showAlert : Bool = false
1515 var showToast : Bool = false
@@ -19,7 +19,6 @@ final class PushNotificationListViewModel: Store {
1919 var isLoading : Bool = false
2020 var hasMore : Bool = false
2121 var nextCursor : PushNotificationCursor ?
22- var pendingTask : ( PushNotificationItem , Int ) ?
2322 var query : PushNotificationQuery
2423 var selectedTodoID : TodoIDItem ?
2524 }
@@ -57,6 +56,7 @@ final class PushNotificationListViewModel: Store {
5756 private let toggleReadUseCase : TogglePushNotificationReadUseCase
5857 private let fetchQueryUseCase : FetchPushNotificationQueryUseCase
5958 private let updateQueryUseCase : UpdatePushNotificationQueryUseCase
59+ private var pendingTask : ( PushNotificationItem , Int ) ?
6060
6161 init (
6262 fetchUseCase: FetchPushNotificationsUseCase ,
@@ -99,7 +99,7 @@ final class PushNotificationListViewModel: Store {
9999 effects = reduceByRun ( action, state: & state)
100100 }
101101
102- self . state = state
102+ if self . state ! = state { self . state = state }
103103 return effects
104104 }
105105
@@ -158,12 +158,12 @@ private extension PushNotificationListViewModel {
158158 switch action {
159159 case . deleteNotification( let item) :
160160 var effects : [ SideEffect ] = [ ]
161- if let ( pendingItem, _) = state . pendingTask {
161+ if let ( pendingItem, _) = pendingTask {
162162 effects = [ . delete( pendingItem) ]
163163 }
164164
165165 if let index = state. notifications. firstIndex ( where: { $0. id == item. id } ) {
166- state . pendingTask = ( item, index)
166+ pendingTask = ( item, index)
167167 state. notifications. remove ( at: index)
168168 setToast ( & state, isPresented: true )
169169 }
@@ -175,9 +175,9 @@ private extension PushNotificationListViewModel {
175175 return [ . toggleRead( item. todoID) ]
176176 }
177177 case . undoDelete:
178- guard let ( item, index) = state . pendingTask else { return [ ] }
178+ guard let ( item, index) = pendingTask else { return [ ] }
179179 state. notifications. insert ( item, at: index)
180- state . pendingTask = nil
180+ pendingTask = nil
181181 case . setAlert( let isPresented) :
182182 setAlert ( & state, isPresented: isPresented)
183183 case . toggleSortOption:
@@ -218,11 +218,11 @@ private extension PushNotificationListViewModel {
218218 state. nextCursor = nil
219219 return [ . fetchNotifications( state. query, cursor: nil ) ]
220220 case . loadNextPage:
221- guard state. hasMore, !state. isLoading, state . pendingTask == nil else { return [ ] }
221+ guard state. hasMore, !state. isLoading, pendingTask == nil else { return [ ] }
222222 return [ . fetchNotifications( state. query, cursor: state. nextCursor) ]
223223 case . confirmDelete:
224- guard let ( item, _) = state . pendingTask else { return [ ] }
225- state . pendingTask = nil
224+ guard let ( item, _) = pendingTask else { return [ ] }
225+ pendingTask = nil
226226 return [ . delete( item) ]
227227 case . setToast( let isPresented) :
228228 setToast ( & state, isPresented: isPresented)
@@ -245,7 +245,7 @@ private extension PushNotificationListViewModel {
245245 state. nextCursor = nil
246246 case . appendNotifications( let notifications, let nextCursor) :
247247 let filteredNotifications : [ PushNotificationItem ]
248- if let ( pendingItem, _) = state . pendingTask {
248+ if let ( pendingItem, _) = pendingTask {
249249 filteredNotifications = notifications. filter { $0. id != pendingItem. id }
250250 } else {
251251 filteredNotifications = notifications
0 commit comments