Skip to content

Instantly share code, notes, and snippets.

@mornin-ystIUBqR
Created October 15, 2023 10:06
Show Gist options
  • Save mornin-ystIUBqR/dd95bbd073c0f7df98dac6b76bc7b3c7 to your computer and use it in GitHub Desktop.
Save mornin-ystIUBqR/dd95bbd073c0f7df98dac6b76bc7b3c7 to your computer and use it in GitHub Desktop.
import SwiftUI
import ComposableArchitecture
struct AppReducer: Reducer {
struct State: Equatable {
@PresentationState var alert: AlertState<Action.Alert>?
}
enum Action {
case alertButtonTapped
case alert(PresentationAction<Alert>)
enum Alert: Equatable {
case confirmButtonTapped
}
}
var body: some Reducer<State, Action> {
Reduce { state, action in
switch action {
case .alertButtonTapped:
state.alert = AlertState {
TextState("Alert")
} actions: {
ButtonState(action: .send(.confirmButtonTapped)) {
TextState("OK")
}
ButtonState(role: .cancel) {
TextState("やめとく")
}
}
return .none
case .alert:
return .none
}
}
.ifLet(\.alert, action: /Action.alert)
}
}
struct ContentView: View {
let store: StoreOf<AppReducer> = .init(initialState: AppReducer.State(), reducer: { AppReducer() })
var body: some View {
WithViewStore(store, observe: { $0 }) {viewStore in
VStack {
Button("Alert") {
viewStore.send(.alertButtonTapped)
}
}
.alert(store: store.scope(state: \.$alert, action: { .alert($0) }))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment