Files
swift-composable-architectu…/Sources/ComposableArchitecture/SwiftUI/ConfirmationDialog.swift
Stephen Celis c432a76b5b Navigation (#1945)
* wip

* fix

* wip

* wip

* move

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Fix

* wip

* wip

* Renamed action to onTap in NavigationLinkStore (#2043)

Renamed the `action` parameter to mirror other inits and differentiate itself from `action fromDestinationAction`

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Tie view identity to stack element identity

* Tie item identity to case

* wip

* wip

* cleanup

* fix

* fix

* Add warning to nav link

* wip

* wip

* Rename FullscreenCover.swift to FullScreenCover.swift (#2062)

* wip

* fix isDetailLink on non-iOS platforms

* Correct some comments in Effect.swift (#2081)

* add integration tests for showing alert/dialog from alert/dialog.

* copy StackElementIDGenerator dependency before running TestStore receive closure.

* Removed some unneeded delegate actions.

* wip

* clean up

* lots of clean up

* Converted voice memos back to identified array

* update deps

* update docs for DismissEffect

* wip

* Add Sendable conformance to PresentationState (#2086)

* wip

* swift-format

* wip

* wip

* docs

* wip

* wip

* Catch some typos in Articles (#2088)

* wip

* wip

* wip

* wip

* wip

* docs

* wip

* wip

* docs

* wip

* wip

* wip

* wip

* docs

* docs

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Fix invalid states count for 3 optionals and typos (#2094)

* wip

* wip

* more dismisseffect docs

* fixed some references

* navigation doc corrections

* more nav docs

* fix cancellation tests in release mode

* wrap some tests in #if DEBUG since they are testing expected failures

* update UUIDs in tests to use shorter initializer

* fixed a todo

* wip

* fix merge errors

* wip

* fix

* wip

* wip

* fixing a bunch of todos

* get rid of rawvalue in StackElementID

* more todos

* NavLinkStore docs

* fix swift 5.6 stuff

* fix some standups tests

* fix

* clean up

* docs fix

* fixes

* wip

* 5.6 fix

* wip

* wip

* dont parallelize tests

* updated demo readmes

* wip

* Use ObservedObject instead of StateObject for alert/dialog modifiers.

* integration tests for bad dismissal behavior

* check for runtime warnings in every integration test

* wip

* wip

* fix

* wip

* wip

* wip

* wip

* wip

* Drop a bunch of Hashables.

* some nav bug fixes

* wip

* wip

* wip

* fix

* fix

* wip

* wip

* Simplify recording test.

* add concurrent async test

* fix

* wip

* Refact how detail dismisses itself.

* fix

* 5.6 fix

* wip

* wip

* Add TestStore.assert.

* Revert "Add TestStore.assert."

This reverts commit a892cccc66.

* add Ukrainian Readme.md (#2121)

* Add TestStore.assert. (#2123)

* Add TestStore.assert.

* wip

* Update Sources/ComposableArchitecture/TestStore.swift

Co-authored-by: Stephen Celis <stephen@stephencelis.com>

* Update Sources/ComposableArchitecture/Documentation.docc/Extensions/TestStore.md

Co-authored-by: Stephen Celis <stephen@stephencelis.com>

* fix tests

---------

Co-authored-by: Stephen Celis <stephen@stephencelis.com>

* Run swift-format

* push for store.finish and presentation

* move docs around

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Add case subscripts

* wip

* wip

* 5.7-only

* wip

* wip

* wip

* wip

* revert store.finish task cancellation

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* add test for presentation scope

* wip

* wip

* wip

* wip

* wip

* cleanup

* updated presentation scope test

* sytnax update

* clean up

* fix test

* wip

* wip

* wip

* wip

* wip

---------

Co-authored-by: Brandon Williams <mbrandonw@hey.com>
Co-authored-by: Martin Václavík <mvaclavik96@icloud.com>
Co-authored-by: 유재호 <y73447jh@gmail.com>
Co-authored-by: Jackson Utsch <jutechs@gmail.com>
Co-authored-by: Dmytro <barabashdmyto@gmail.com>
Co-authored-by: Brandon Williams <135203+mbrandonw@users.noreply.github.com>
Co-authored-by: mbrandonw <mbrandonw@users.noreply.github.com>
2023-05-30 12:22:00 -04:00

157 lines
5.3 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) -> ConfirmationDialogState<ButtonAction>?,
action fromDestinationAction: @escaping (ButtonAction) -> Action
) -> some View {
self.presentation(
store: store, state: toDestinationState, action: fromDestinationAction
) { `self`, $isPresented, destination in
let confirmationDialogState = store.state.value.wrappedValue.flatMap(toDestinationState)
self.confirmationDialog(
(confirmationDialogState?.title).map(Text.init) ?? Text(""),
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 {
_ = withAnimation(animation) {
store.send(.presented(fromDestinationAction(action)))
}
}
}
} label: {
Text(button.label)
}
}
},
message: {
$0.message.map(Text.init)
}
)
}
}
}
extension View {
/// Displays a dialog when the store's state becomes non-`nil`, and dismisses it when it becomes
/// `nil`.
///
/// - Parameters:
/// - store: A store that describes if the dialog is shown or dismissed.
/// - dismissal: An action to send when the dialog is dismissed through non-user actions, such
/// as when a dialog is automatically dismissed by the system. Use this action to `nil` out
/// the associated dialog state.
@available(iOS 13, *)
@available(macOS 12, *)
@available(tvOS 13, *)
@available(watchOS 6, *)
@ViewBuilder public func confirmationDialog<Action>(
_ store: Store<ConfirmationDialogState<Action>?, Action>,
dismiss: Action
) -> some View {
if #available(iOS 15, tvOS 15, watchOS 8, *) {
self.modifier(
NewConfirmationDialogModifier(
viewStore: ViewStore(store, removeDuplicates: { $0?.id == $1?.id }),
dismiss: dismiss
)
)
} else {
#if !os(macOS)
self.modifier(
OldConfirmationDialogModifier(
viewStore: ViewStore(store, removeDuplicates: { $0?.id == $1?.id }),
dismiss: dismiss
)
)
#endif
}
}
}
// NB: Workaround for iOS 14 runtime crashes during iOS 15 availability checks.
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
private struct NewConfirmationDialogModifier<Action>: ViewModifier {
@StateObject var viewStore: ViewStore<ConfirmationDialogState<Action>?, Action>
let dismiss: Action
func body(content: Content) -> some View {
content.confirmationDialog(
(viewStore.state?.title).map { Text($0) } ?? Text(""),
isPresented: viewStore.binding(send: dismiss).isPresent(),
titleVisibility: viewStore.state.map { .init($0.titleVisibility) } ?? .automatic,
presenting: viewStore.state,
actions: {
ForEach($0.buttons) {
Button($0) { action in
if let action = action {
viewStore.send(action)
}
}
}
},
message: { $0.message.map { Text($0) } }
)
}
}
@available(iOS 13, *)
@available(macOS 12, *)
@available(tvOS 13, *)
@available(watchOS 6, *)
private struct OldConfirmationDialogModifier<Action>: ViewModifier {
@ObservedObject var viewStore: ViewStore<ConfirmationDialogState<Action>?, Action>
let dismiss: Action
func body(content: Content) -> some View {
#if !os(macOS)
content.actionSheet(item: viewStore.binding(send: dismiss)) {
ActionSheet($0) { action in
if let action = action {
viewStore.send(action)
}
}
}
#else
EmptyView()
#endif
}
}