diff --git a/DevLog/Presentation/ViewModel/PushNotificationListViewModel.swift b/DevLog/Presentation/ViewModel/PushNotificationListViewModel.swift index a745180..9ae123c 100644 --- a/DevLog/Presentation/ViewModel/PushNotificationListViewModel.swift +++ b/DevLog/Presentation/ViewModel/PushNotificationListViewModel.swift @@ -13,10 +13,8 @@ final class PushNotificationListViewModel: Store { 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 hasMore: Bool = false var nextCursor: PushNotificationCursor? @@ -32,8 +30,8 @@ final class PushNotificationListViewModel: Store { case toggleRead(PushNotification) case undoDelete case confirmDelete - case setAlert(isPresented: Bool, type: AlertType? = nil) - case setToast(isPresented: Bool, type: ToastType? = nil) + case setAlert(isPresented: Bool) + case setToast(isPresented: Bool) case setLoading(Bool) case appendNotifications([PushNotification], nextCursor: PushNotificationCursor?) case resetPagination @@ -52,14 +50,6 @@ final class PushNotificationListViewModel: Store { case toggleRead(String) } - enum AlertType { - case error - } - - enum ToastType { - case delete - } - @Published private(set) var state: State private let fetchUseCase: FetchPushNotificationsUseCase private let deleteUseCase: DeletePushNotificationUseCase @@ -128,7 +118,7 @@ final class PushNotificationListViewModel: Store { let hasMore = page.items.count == query.pageSize && page.nextCursor != nil send(.setHasMore(hasMore)) } catch { - send(.setAlert(isPresented: true, type: .error)) + send(.setAlert(isPresented: true)) } } @@ -139,7 +129,7 @@ final class PushNotificationListViewModel: Store { send(.setLoading(true)) try await deleteUseCase.execute(notification.id) } catch { - send(.setAlert(isPresented: true, type: .error)) + send(.setAlert(isPresented: true)) } } case .toggleRead(let todoID): @@ -149,7 +139,7 @@ final class PushNotificationListViewModel: Store { send(.setLoading(true)) try await toggleReadUseCase.execute(todoID) } catch { - send(.setAlert(isPresented: true, type: .error)) + send(.setAlert(isPresented: true)) } } } @@ -161,11 +151,18 @@ private extension PushNotificationListViewModel { func reduceByUser(_ action: Action, state: inout State) -> [SideEffect] { switch action { case .deleteNotification(let item): + var effects: [SideEffect] = [] + if let (pendingItem, _) = state.pendingTask { + effects = [.delete(pendingItem)] + } + if let index = state.notifications.firstIndex(where: { $0.id == item.id }) { state.pendingTask = (item, index) state.notifications.remove(at: index) - setToast(&state, isPresented: true, for: .delete) + setToast(&state, isPresented: true) } + + return effects case .toggleRead(let item): if let index = state.notifications.firstIndex(where: { $0.id == item.id }) { state.notifications[index].isRead.toggle() @@ -175,8 +172,8 @@ private extension PushNotificationListViewModel { guard let (item, index) = state.pendingTask else { return [] } state.notifications.insert(item, at: index) state.pendingTask = nil - case .setAlert(let isPresented, let type): - setAlert(&state, isPresented: isPresented, for: type) + case .setAlert(let isPresented): + setAlert(&state, isPresented: isPresented) case .toggleSortOption: state.query.sortOrder = state.query.sortOrder == .latest ? .oldest : .latest updateQueryUseCase.execute(state.query) @@ -221,8 +218,8 @@ private extension PushNotificationListViewModel { guard let (item, _) = state.pendingTask else { return [] } state.pendingTask = nil return [.delete(item)] - case .setToast(let isPresented, let type): - setToast(&state, isPresented: isPresented, for: type) + case .setToast(let isPresented): + setToast(&state, isPresented: isPresented) case .setSelectedTodoID(let todoID): state.selectedTodoID = todoID default: @@ -259,32 +256,18 @@ private extension PushNotificationListViewModel { private extension PushNotificationListViewModel { func setAlert( _ state: inout State, - isPresented: Bool, - for type: AlertType? + isPresented: Bool ) { - switch type { - case .error: - state.alertTitle = "오류" - state.alertMessage = "문제가 발생했습니다. 잠시 후 다시 시도해주세요." - case .none: - state.alertTitle = "" - state.alertMessage = "" - } - state.alertType = type + state.alertTitle = "오류" + state.alertMessage = "문제가 발생했습니다. 잠시 후 다시 시도해주세요." state.showAlert = isPresented } func setToast( _ state: inout State, - isPresented: Bool, - for type: ToastType? + isPresented: Bool ) { - switch type { - case .delete: - state.toastMessage = "실행 취소" - case .none: - state.toastMessage = "" - } + state.toastMessage = "실행 취소" state.showToast = isPresented } } diff --git a/DevLog/Presentation/ViewModel/TodoListViewModel.swift b/DevLog/Presentation/ViewModel/TodoListViewModel.swift index 73e5746..0da5a01 100644 --- a/DevLog/Presentation/ViewModel/TodoListViewModel.swift +++ b/DevLog/Presentation/ViewModel/TodoListViewModel.swift @@ -185,12 +185,13 @@ private extension TodoListViewModel { if let (pendingItem, _) = state.pendingTask { effects = [.delete(pendingItem.id)] } - guard let index = state.todos.firstIndex(where: { $0.id == todo.id }) else { - return [] + + if let index = state.todos.firstIndex(where: { $0.id == todo.id }) { + state.pendingTask = (todo, index) + state.todos.remove(at: index) + setToast(&state, isPresented: true) } - state.pendingTask = (todo, index) - state.todos.remove(at: index) - setToast(&state, isPresented: true) + return effects case .tapFilterOption(let option): state.filterOption = option