Created
October 15, 2023 10:06
-
-
Save mornin-ystIUBqR/dd95bbd073c0f7df98dac6b76bc7b3c7 to your computer and use it in GitHub Desktop.
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 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