Files
swift-composable-architectu…/Sources/ComposableArchitecture/SwiftUI/ConfirmationDialog.swift
2023-11-02 12:25:37 -07:00

69 lines
2.7 KiB
Swift

import SwiftUI
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
extension View {
/// Displays a dialog when then store's state becomes non-`nil`, and dismisses it when it becomes
/// `nil`.
///
/// - Parameters:
/// - store: A store that is focused on ``PresentationState`` and ``PresentationAction`` for a
/// dialog.
public func confirmationDialog<ButtonAction>(
store: Store<
PresentationState<ConfirmationDialogState<ButtonAction>>,
PresentationAction<ButtonAction>
>
) -> some View {
self.confirmationDialog(store: store, state: { $0 }, action: { $0 })
}
/// Displays a dialog when then store's state becomes non-`nil`, and dismisses it when it becomes
/// `nil`.
///
/// - Parameters:
/// - store: A store that is focused on ``PresentationState`` and ``PresentationAction`` for a
/// dialog.
/// - toDestinationState: A transformation to extract dialog state from the presentation state.
/// - fromDestinationAction: A transformation to embed dialog actions into the presentation
/// action.
public func confirmationDialog<State, Action, ButtonAction>(
store: Store<PresentationState<State>, PresentationAction<Action>>,
state toDestinationState: @escaping (_ state: State) -> ConfirmationDialogState<ButtonAction>?,
action fromDestinationAction: @escaping (_ confirmationDialogAction: ButtonAction) -> Action
) -> some View {
self.presentation(
store: store, state: toDestinationState, action: fromDestinationAction
) { `self`, $isPresented, destination in
let confirmationDialogState = store.withState { $0.wrappedValue.flatMap(toDestinationState) }
self.confirmationDialog(
(confirmationDialogState?.title).map(Text.init) ?? Text(verbatim: ""),
isPresented: $isPresented,
titleVisibility: (confirmationDialogState?.titleVisibility).map(Visibility.init)
?? .automatic,
presenting: confirmationDialogState,
actions: { confirmationDialogState in
ForEach(confirmationDialogState.buttons) { button in
Button(role: button.role.map(ButtonRole.init)) {
switch button.action.type {
case let .send(action):
if let action = action {
store.send(.presented(fromDestinationAction(action)))
}
case let .animatedSend(action, animation):
if let action = action {
store.send(.presented(fromDestinationAction(action)), animation: animation)
}
}
} label: {
Text(button.label)
}
}
},
message: {
$0.message.map(Text.init)
}
)
}
}
}