|
| 1 | +import ComposableArchitecture |
| 2 | +import Foundation |
| 3 | +import Preferences |
| 4 | +import SwiftUI |
| 5 | + |
| 6 | +struct CustomCommandFeature: ReducerProtocol { |
| 7 | + struct State: Equatable { |
| 8 | + var editCustomCommand: EditCustomCommand.State? |
| 9 | + } |
| 10 | + |
| 11 | + let settings: CustomCommandView.Settings |
| 12 | + let toast: (Text, ToastType) -> Void |
| 13 | + |
| 14 | + enum Action: Equatable { |
| 15 | + case createNewCommand |
| 16 | + case editCommand(CustomCommand) |
| 17 | + case editCustomCommand(EditCustomCommand.Action) |
| 18 | + case deleteCommand(CustomCommand) |
| 19 | + } |
| 20 | + |
| 21 | + var body: some ReducerProtocol<State, Action> { |
| 22 | + Reduce { state, action in |
| 23 | + switch action { |
| 24 | + case .createNewCommand: |
| 25 | + state.editCustomCommand = EditCustomCommand.State(nil) |
| 26 | + return .none |
| 27 | + case let .editCommand(command): |
| 28 | + state.editCustomCommand = EditCustomCommand.State(command) |
| 29 | + return .none |
| 30 | + case .editCustomCommand(.close): |
| 31 | + state.editCustomCommand = nil |
| 32 | + return .none |
| 33 | + case let .deleteCommand(command): |
| 34 | + settings.customCommands.removeAll( |
| 35 | + where: { $0.id == command.id } |
| 36 | + ) |
| 37 | + if state.editCustomCommand?.commandId == command.id { |
| 38 | + state.editCustomCommand = nil |
| 39 | + } |
| 40 | + return .none |
| 41 | + case .editCustomCommand: |
| 42 | + return .none |
| 43 | + |
| 44 | + } |
| 45 | + }.ifLet(\.editCustomCommand, action: /Action.editCustomCommand) { |
| 46 | + EditCustomCommand(settings: settings, toast: toast) |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments