TCA Map
Add swift composable architecture
https://github.com/pointfreeco/swift-composable-architecture"
実装 - import
import ComposableArchitecture
実装 - code
struct State: Equatable {
var counter = 0
}
enum Action: Equatable {
case increaseCounter
case decreaseCounter
}
struct Enviroment {
}
let reducer = Reducer< State, Action, Enviroment> { state, action, enviroment 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(8)
.background(.red)
.foregoundColor(.white)
.cornerRadius(8)
}
.buttonStyle(.plain)
Text(viewStore.counter.description)
.padding(10)
Button {
viewStore.send(.increaseCounter)
} label: {
Text("+")
.padding(8)
.background(.red)
.foregoundColor(.white)
.cornerRadius(8)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(
store: Store(
initialState: State(),
reducer: reducer,
enviroment: Enviroment()
)
)
}
}