More context about this demo here: https://youtu.be/SfFDj6qT-xg
This file contains 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
import SwiftUI | |
import ComposableArchitecture | |
struct State: Equatable { | |
var counter = 0 | |
} | |
enum Action: Equatable { | |
case increaseCounter | |
case decreaseCounter | |
} | |
struct Environment { | |
// Future Dependencies... | |
} | |
let reducer = Reducer< | |
State, Action, Environment | |
> { state, action, environment in | |
switch action { | |
case .increaseCounter: | |
state.counter += 1 | |
return Effect.none | |
case .decreaseCounter: | |
state.counter -= 1 | |
return Effect.none | |
} | |
} | |
struct ContentView: View { | |
let store: Store<State, Action> | |
var body: some View { | |
WithViewStore(self.store) { viewStore in | |
HStack { | |
Button { | |
viewStore.send(.decreaseCounter) | |
} label: { | |
Text("-") | |
.padding(10) | |
.background(.blue) | |
.foregroundColor(.white) | |
.cornerRadius(10) | |
} | |
.buttonStyle(.plain) | |
Text(viewStore.counter.description) | |
.padding(5) | |
Button { | |
viewStore.send(.increaseCounter) | |
} label: { | |
Text("+") | |
.padding(10) | |
.background(.blue) | |
.foregroundColor(.white) | |
.cornerRadius(10) | |
} | |
.buttonStyle(.plain) | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView( | |
store: Store( | |
initialState: State(), | |
reducer: reducer, | |
environment: Environment() | |
) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment