|
1 | 1 | #if canImport(SwiftUI) |
2 | | - import SwiftUI |
| 2 | +import SwiftUI |
3 | 3 |
|
4 | 4 | @available(iOS 13, macOS 10.15, macCatalyst 13, tvOS 13, watchOS 6, *) |
5 | | - extension View { |
6 | | - /// Displays a dialog when the store's state becomes non-`nil`, and dismisses it when it becomes |
7 | | - /// `nil`. |
8 | | - /// |
9 | | - /// - Parameters: |
10 | | - /// - store: A store that describes if the dialog is shown or dismissed. |
11 | | - /// - dismissal: An action to send when the dialog is dismissed through non-user actions, such |
12 | | - /// as when a dialog is automatically dismissed by the system. Use this action to `nil` out |
13 | | - /// the associated dialog state. |
14 | | - @available(iOS 13, *) |
15 | | - @available(macOS 12, *) |
16 | | - @available(tvOS 13, *) |
17 | | - @available(watchOS 6, *) |
18 | | - @ViewBuilder public func confirmationDialog<Action>( |
19 | | - _ store: Store<ConfirmationDialogState<Action>?, Action>, |
20 | | - dismiss: Action |
21 | | - ) -> some View { |
22 | | - if #available(iOS 15, tvOS 15, watchOS 8, *) { |
| 5 | +extension View { |
| 6 | + /// Displays a dialog when the store's state becomes non-`nil`, and dismisses it when it becomes |
| 7 | + /// `nil`. |
| 8 | + /// |
| 9 | + /// - Parameters: |
| 10 | + /// - store: A store that describes if the dialog is shown or dismissed. |
| 11 | + /// - dismissal: An action to send when the dialog is dismissed through non-user actions, such |
| 12 | + /// as when a dialog is automatically dismissed by the system. Use this action to `nil` out |
| 13 | + /// the associated dialog state. |
| 14 | + @available(iOS 13, *) |
| 15 | + @available(macOS 12, *) |
| 16 | + @available(tvOS 13, *) |
| 17 | + @available(watchOS 6, *) |
| 18 | + @ViewBuilder public func confirmationDialog<Action>( |
| 19 | + _ store: Store<ConfirmationDialogState<Action>?, Action>, |
| 20 | + dismiss: Action |
| 21 | + ) -> some View { |
| 22 | + if #available(iOS 15, tvOS 15, watchOS 8, *) { |
| 23 | + self.modifier( |
| 24 | + NewConfirmationDialogModifier( |
| 25 | + viewStore: ViewStore(store, removeDuplicates: { $0?.id == $1?.id }), |
| 26 | + dismiss: dismiss |
| 27 | + ) |
| 28 | + ) |
| 29 | + } else { |
| 30 | + #if !os(macOS) |
23 | 31 | self.modifier( |
24 | | - NewConfirmationDialogModifier( |
| 32 | + OldConfirmationDialogModifier( |
25 | 33 | viewStore: ViewStore(store, removeDuplicates: { $0?.id == $1?.id }), |
26 | 34 | dismiss: dismiss |
27 | 35 | ) |
28 | 36 | ) |
29 | | - } else { |
30 | | - #if !os(macOS) |
31 | | - self.modifier( |
32 | | - OldConfirmationDialogModifier( |
33 | | - viewStore: ViewStore(store, removeDuplicates: { $0?.id == $1?.id }), |
34 | | - dismiss: dismiss |
35 | | - ) |
36 | | - ) |
37 | | - #endif |
38 | | - } |
| 37 | + #endif |
39 | 38 | } |
40 | 39 | } |
| 40 | +} |
41 | 41 |
|
42 | | - // NB: Workaround for iOS 14 runtime crashes during iOS 15 availability checks. |
43 | | - @available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) |
44 | | - private struct NewConfirmationDialogModifier<Action>: ViewModifier { |
45 | | - @ObservedObject var viewStore: ViewStore<ConfirmationDialogState<Action>?, Action> |
46 | | - let dismiss: Action |
| 42 | +// NB: Workaround for iOS 14 runtime crashes during iOS 15 availability checks. |
| 43 | +@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) |
| 44 | +private struct NewConfirmationDialogModifier<Action>: ViewModifier { |
| 45 | + @StateObject var viewStore: ViewStore<ConfirmationDialogState<Action>?, Action> |
| 46 | + let dismiss: Action |
47 | 47 |
|
48 | | - func body(content: Content) -> some View { |
49 | | - content.confirmationDialog( |
50 | | - (viewStore.state?.title).map { Text($0) } ?? Text(""), |
51 | | - isPresented: viewStore.binding(send: dismiss).isPresent(), |
52 | | - titleVisibility: viewStore.state.map { .init($0.titleVisibility) } ?? .automatic, |
53 | | - presenting: viewStore.state, |
54 | | - actions: { |
55 | | - ForEach($0.buttons) { |
56 | | - Button($0, action: { viewStore.send($0) }) |
57 | | - } |
58 | | - }, |
59 | | - message: { $0.message.map { Text($0) } } |
60 | | - ) |
61 | | - } |
| 48 | + func body(content: Content) -> some View { |
| 49 | + content.confirmationDialog( |
| 50 | + (viewStore.state?.title).map { Text($0) } ?? Text(""), |
| 51 | + isPresented: viewStore.binding(send: dismiss).isPresent(), |
| 52 | + titleVisibility: viewStore.state.map { .init($0.titleVisibility) } ?? .automatic, |
| 53 | + presenting: viewStore.state, |
| 54 | + actions: { |
| 55 | + ForEach($0.buttons) { |
| 56 | + Button($0, action: { viewStore.send($0) }) |
| 57 | + } |
| 58 | + }, |
| 59 | + message: { $0.message.map { Text($0) } } |
| 60 | + ) |
62 | 61 | } |
| 62 | +} |
63 | 63 |
|
64 | | - @available(iOS 13, *) |
65 | | - @available(macOS 12, *) |
66 | | - @available(tvOS 13, *) |
67 | | - @available(watchOS 6, *) |
68 | | - private struct OldConfirmationDialogModifier<Action>: ViewModifier { |
69 | | - @ObservedObject var viewStore: ViewStore<ConfirmationDialogState<Action>?, Action> |
70 | | - let dismiss: Action |
| 64 | +@available(iOS 13, *) |
| 65 | +@available(macOS 12, *) |
| 66 | +@available(tvOS 13, *) |
| 67 | +@available(watchOS 6, *) |
| 68 | +private struct OldConfirmationDialogModifier<Action>: ViewModifier { |
| 69 | + @ObservedObject var viewStore: ViewStore<ConfirmationDialogState<Action>?, Action> |
| 70 | + let dismiss: Action |
71 | 71 |
|
72 | | - func body(content: Content) -> some View { |
73 | | - #if !os(macOS) |
74 | | - return content.actionSheet(item: viewStore.binding(send: dismiss)) { |
75 | | - ActionSheet($0) { viewStore.send($0) } |
76 | | - } |
77 | | - #else |
78 | | - return EmptyView() |
79 | | - #endif |
80 | | - } |
| 72 | + func body(content: Content) -> some View { |
| 73 | + #if !os(macOS) |
| 74 | + return content.actionSheet(item: viewStore.binding(send: dismiss)) { |
| 75 | + ActionSheet($0) { viewStore.send($0) } |
| 76 | + } |
| 77 | + #else |
| 78 | + return EmptyView() |
| 79 | + #endif |
81 | 80 | } |
| 81 | +} |
82 | 82 | #endif |
0 commit comments