|
1 | 1 | import SwiftUI |
2 | 2 |
|
| 3 | +struct AlertContent: View { |
| 4 | + @Binding var isPresented: Bool |
| 5 | + let model: AlertVM |
| 6 | + let primaryAction: (() -> Void)? |
| 7 | + let secondaryAction: (() -> Void)? |
| 8 | + |
| 9 | + var body: some View { |
| 10 | + SUCenterModal( |
| 11 | + isVisible: self.$isPresented, |
| 12 | + model: self.model.modalVM, |
| 13 | + header: { |
| 14 | + if self.model.message.isNotNil, |
| 15 | + let text = self.model.title { |
| 16 | + self.title(text) |
| 17 | + } |
| 18 | + }, |
| 19 | + body: { |
| 20 | + if let text = self.model.message { |
| 21 | + self.message(text) |
| 22 | + } else if let text = self.model.title { |
| 23 | + self.title(text) |
| 24 | + } |
| 25 | + }, |
| 26 | + footer: { |
| 27 | + switch AlertButtonsOrientationCalculator.preferredOrientation(model: model) { |
| 28 | + case .horizontal: |
| 29 | + HStack(spacing: AlertVM.buttonsSpacing) { |
| 30 | + self.button( |
| 31 | + model: self.model.secondaryButtonVM, |
| 32 | + action: self.secondaryAction |
| 33 | + ) |
| 34 | + self.button( |
| 35 | + model: self.model.primaryButtonVM, |
| 36 | + action: self.primaryAction |
| 37 | + ) |
| 38 | + } |
| 39 | + case .vertical: |
| 40 | + VStack(spacing: AlertVM.buttonsSpacing) { |
| 41 | + self.button( |
| 42 | + model: self.model.primaryButtonVM, |
| 43 | + action: self.primaryAction |
| 44 | + ) |
| 45 | + self.button( |
| 46 | + model: self.model.secondaryButtonVM, |
| 47 | + action: self.secondaryAction |
| 48 | + ) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + // MARK: - Helpers |
| 56 | + |
| 57 | + func title(_ text: String) -> some View { |
| 58 | + Text(text) |
| 59 | + .font(UniversalFont.mdHeadline.font) |
| 60 | + .foregroundStyle(UniversalColor.foreground.color) |
| 61 | + .multilineTextAlignment(.center) |
| 62 | + .frame(maxWidth: .infinity) |
| 63 | + } |
| 64 | + |
| 65 | + func message(_ text: String) -> some View { |
| 66 | + Text(text) |
| 67 | + .font(UniversalFont.mdBody.font) |
| 68 | + .foregroundStyle(UniversalColor.secondaryForeground.color) |
| 69 | + .multilineTextAlignment(.center) |
| 70 | + .frame(maxWidth: .infinity) |
| 71 | + } |
| 72 | + |
| 73 | + func button( |
| 74 | + model: ButtonVM?, |
| 75 | + action: (() -> Void)? |
| 76 | + ) -> some View { |
| 77 | + Group { |
| 78 | + if let model { |
| 79 | + SUButton(model: model) { |
| 80 | + action?() |
| 81 | + self.isPresented = false |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// MARK: - Presentation Helpers |
| 89 | + |
3 | 90 | extension View { |
4 | 91 | /// A SwiftUI view modifier that presents an alert with a title, message, and up to two action buttons. |
5 | 92 | /// |
@@ -53,95 +140,95 @@ extension View { |
53 | 140 | transitionDuration: model.transition.value, |
54 | 141 | onDismiss: onDismiss, |
55 | 142 | content: { |
56 | | - SUCenterModal( |
57 | | - isVisible: isPresented, |
58 | | - model: model.modalVM, |
59 | | - header: { |
60 | | - if model.message.isNotNil, |
61 | | - let title = model.title { |
62 | | - AlertTitle(text: title) |
63 | | - } |
64 | | - }, |
65 | | - body: { |
66 | | - if let message = model.message { |
67 | | - AlertMessage(text: message) |
68 | | - } else if let title = model.title { |
69 | | - AlertTitle(text: title) |
70 | | - } |
71 | | - }, |
72 | | - footer: { |
73 | | - switch AlertButtonsOrientationCalculator.preferredOrientation(model: model) { |
74 | | - case .horizontal: |
75 | | - HStack(spacing: AlertVM.buttonsSpacing) { |
76 | | - AlertButton( |
77 | | - isAlertPresented: isPresented, |
78 | | - model: model.secondaryButtonVM, |
79 | | - action: secondaryAction |
80 | | - ) |
81 | | - AlertButton( |
82 | | - isAlertPresented: isPresented, |
83 | | - model: model.primaryButtonVM, |
84 | | - action: primaryAction |
85 | | - ) |
86 | | - } |
87 | | - case .vertical: |
88 | | - VStack(spacing: AlertVM.buttonsSpacing) { |
89 | | - AlertButton( |
90 | | - isAlertPresented: isPresented, |
91 | | - model: model.primaryButtonVM, |
92 | | - action: primaryAction |
93 | | - ) |
94 | | - AlertButton( |
95 | | - isAlertPresented: isPresented, |
96 | | - model: model.secondaryButtonVM, |
97 | | - action: secondaryAction |
98 | | - ) |
99 | | - } |
100 | | - } |
101 | | - } |
| 143 | + AlertContent( |
| 144 | + isPresented: isPresented, |
| 145 | + model: model, |
| 146 | + primaryAction: primaryAction, |
| 147 | + secondaryAction: secondaryAction |
102 | 148 | ) |
103 | 149 | } |
104 | 150 | ) |
105 | 151 | } |
106 | | -} |
107 | | - |
108 | | -// MARK: - Helpers |
109 | | - |
110 | | -private struct AlertTitle: View { |
111 | | - let text: String |
112 | | - |
113 | | - var body: some View { |
114 | | - Text(self.text) |
115 | | - .font(UniversalFont.mdHeadline.font) |
116 | | - .foregroundStyle(UniversalColor.foreground.color) |
117 | | - .multilineTextAlignment(.center) |
118 | | - .frame(maxWidth: .infinity) |
119 | | - } |
120 | | -} |
121 | 152 |
|
122 | | -private struct AlertMessage: View { |
123 | | - let text: String |
124 | | - |
125 | | - var body: some View { |
126 | | - Text(self.text) |
127 | | - .font(UniversalFont.mdBody.font) |
128 | | - .foregroundStyle(UniversalColor.secondaryForeground.color) |
129 | | - .multilineTextAlignment(.center) |
130 | | - .frame(maxWidth: .infinity) |
131 | | - } |
132 | | -} |
133 | | - |
134 | | -private struct AlertButton: View { |
135 | | - @Binding var isAlertPresented: Bool |
136 | | - let model: ButtonVM? |
137 | | - let action: (() -> Void)? |
138 | | - |
139 | | - var body: some View { |
140 | | - if let model { |
141 | | - SUButton(model: model) { |
142 | | - self.action?() |
143 | | - self.isAlertPresented = false |
| 153 | + /// A SwiftUI view modifier that presents an alert with a title, message, and up to two action buttons. |
| 154 | + /// |
| 155 | + /// All actions in an alert dismiss the alert after the action runs. If no actions are present, a standard “OK” action is included. |
| 156 | + /// |
| 157 | + /// - Parameters: |
| 158 | + /// - isPresented: A binding that determines whether the alert is presented. |
| 159 | + /// - item: A binding to an optional `Item` that determines whether the alert is presented. |
| 160 | + /// When `item` is `nil`, the alert is hidden. |
| 161 | + /// - primaryAction: An optional closure executed when the primary button is tapped. |
| 162 | + /// - secondaryAction: An optional closure executed when the secondary button is tapped. |
| 163 | + /// - onDismiss: An optional closure executed when the alert is dismissed. |
| 164 | + /// |
| 165 | + /// - Example: |
| 166 | + /// ```swift |
| 167 | + /// struct ContentView: View { |
| 168 | + /// struct AlertData: Identifiable { |
| 169 | + /// var id: String { |
| 170 | + /// return text |
| 171 | + /// } |
| 172 | + /// let text: String |
| 173 | + /// } |
| 174 | + /// |
| 175 | + /// @State private var selectedItem: AlertData? |
| 176 | + /// private let items: [AlertData] = [ |
| 177 | + /// AlertData(text: "data 1"), |
| 178 | + /// AlertData(text: "data 2") |
| 179 | + /// ] |
| 180 | + /// |
| 181 | + /// var body: some View { |
| 182 | + /// List(items) { item in |
| 183 | + /// Button("Show Alert") { |
| 184 | + /// selectedItem = item |
| 185 | + /// } |
| 186 | + /// } |
| 187 | + /// .suAlert( |
| 188 | + /// item: $selectedItem, |
| 189 | + /// model: { data in |
| 190 | + /// return AlertVM { |
| 191 | + /// $0.title = "Data Preview" |
| 192 | + /// $0.message = data.text |
| 193 | + /// } |
| 194 | + /// }, |
| 195 | + /// onDismiss: { |
| 196 | + /// print("Alert dismissed") |
| 197 | + /// } |
| 198 | + /// ) |
| 199 | + /// } |
| 200 | + /// } |
| 201 | + /// ``` |
| 202 | + public func suAlert<Item: Identifiable>( |
| 203 | + item: Binding<Item?>, |
| 204 | + model: @escaping (Item) -> AlertVM, |
| 205 | + primaryAction: ((Item) -> Void)? = nil, |
| 206 | + secondaryAction: ((Item) -> Void)? = nil, |
| 207 | + onDismiss: (() -> Void)? = nil |
| 208 | + ) -> some View { |
| 209 | + return self.modal( |
| 210 | + item: item, |
| 211 | + transitionDuration: { model($0).transition.value }, |
| 212 | + onDismiss: onDismiss, |
| 213 | + content: { unwrappedItem in |
| 214 | + AlertContent( |
| 215 | + isPresented: .init( |
| 216 | + get: { |
| 217 | + return item.wrappedValue.isNotNil |
| 218 | + }, |
| 219 | + set: { isPresented in |
| 220 | + if isPresented { |
| 221 | + item.wrappedValue = unwrappedItem |
| 222 | + } else { |
| 223 | + item.wrappedValue = nil |
| 224 | + } |
| 225 | + } |
| 226 | + ), |
| 227 | + model: model(unwrappedItem), |
| 228 | + primaryAction: { primaryAction?(unwrappedItem) }, |
| 229 | + secondaryAction: { secondaryAction?(unwrappedItem) } |
| 230 | + ) |
144 | 231 | } |
145 | | - } |
| 232 | + ) |
146 | 233 | } |
147 | 234 | } |
0 commit comments