Skip to content

Instantly share code, notes, and snippets.

View gaeng2y's full-sized avatar
🦖
Mai Paura

Kyeongmo Yang gaeng2y

🦖
Mai Paura
View GitHub Profile
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))
func loggerMiddleware(action: Action) {
print("Dispatching action: \(action)")
}
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)
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
}
enum Action {
case addTodoItem(text: String)
case removeTodoItem(index: Int)
}
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 = ""
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)
class Dispatcher {
static let shared = Dispatcher()
private init() {}
func dispatch(action: Action) {
Store.shared.handleAction(action: action)
}
}
enum Action {
case addTodoItem(text: String)
case removeTodoItem(index: Int)
}
// Generated code
struct ExistContDrawable {
var valueBuffer: (Int, Int, Int)
var vwt: ValueWitnessTable
var pwt: DrawableProtocolWitnessTable
}