This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct ContentView: View { | |
| @ObservedObject var store: Store | |
| @State private var newItemText: String = "" | |
| var body: some View { | |
| VStack { | |
| TextField("New Item", text: $newItemText) | |
| Button(action: { | |
| loggerMiddleware(action: .addTodoItem(text: newItemText)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func loggerMiddleware(action: Action) { | |
| print("Dispatching action: \(action)") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Store: ObservableObject { | |
| @Published private(set) var state: [String] = [] | |
| private let reducer: (Action) -> Void | |
| init(reducer: @escaping (Action) -> Void) { | |
| self.reducer = reducer | |
| } | |
| func dispatch(action: Action) { | |
| state = todoReducer(state: state, action: action) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func todoReducer(state: [String], action: Action) -> [String] { | |
| var newState = state | |
| switch action { | |
| case .addTodoItem(let text): | |
| newState.append(text) | |
| case .removeTodoItem(let index): | |
| newState.remove(at: index) | |
| } | |
| return newState | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum Action { | |
| case addTodoItem(text: String) | |
| case removeTodoItem(index: Int) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct ContentView: View { | |
| @ObservedObject var store = Store.shared | |
| @State private var newItemText: String = "" | |
| var body: some View { | |
| VStack { | |
| TextField("New Item", text: $newItemText) | |
| Button(action: { | |
| Dispatcher.shared.dispatch(action: .addTodoItem(text: newItemText)) | |
| newItemText = "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Store: ObservableObject { | |
| static let shared = Store() | |
| @Published private(set) var todoItems: [String] = [] | |
| func handleAction(action: Action) { | |
| switch action { | |
| case .addTodoItem(let text): | |
| todoItems.append(text) | |
| case .removeTodoItem(let index): | |
| todoItems.remove(at: index) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Dispatcher { | |
| static let shared = Dispatcher() | |
| private init() {} | |
| func dispatch(action: Action) { | |
| Store.shared.handleAction(action: action) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum Action { | |
| case addTodoItem(text: String) | |
| case removeTodoItem(index: Int) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Generated code | |
| struct ExistContDrawable { | |
| var valueBuffer: (Int, Int, Int) | |
| var vwt: ValueWitnessTable | |
| var pwt: DrawableProtocolWitnessTable | |
| } |