|
|
@@ -9,15 +9,29 @@ import SwiftUI
|
|
|
|
|
|
public final class AlertPresenter: ObservableObject {
|
|
|
|
|
|
- private struct AlertConfiguration {
|
|
|
+ public struct AlertConfiguration {
|
|
|
let title: String
|
|
|
let buttons: [AlertButton]
|
|
|
}
|
|
|
|
|
|
- public struct AlertButton {
|
|
|
+ public struct AlertButton: Hashable {
|
|
|
let title: String
|
|
|
- let role: ButtonRole
|
|
|
+ let role: ButtonRole?
|
|
|
let action: Action
|
|
|
+
|
|
|
+ public init(title: String, role: ButtonRole? = nil, action: @escaping Action) {
|
|
|
+ self.title = title
|
|
|
+ self.role = role
|
|
|
+ self.action = action
|
|
|
+ }
|
|
|
+
|
|
|
+ public static func == (lhs: AlertButton, rhs: AlertButton) -> Bool {
|
|
|
+ lhs.title == rhs.title
|
|
|
+ }
|
|
|
+
|
|
|
+ public func hash(into hasher: inout Hasher) {
|
|
|
+ hasher.combine(title)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private var alertConfiguration = AlertConfiguration(title: "", buttons: [])
|
|
|
@@ -33,17 +47,20 @@ public final class AlertPresenter: ObservableObject {
|
|
|
|
|
|
@ViewBuilder
|
|
|
public func buildButtons() -> some View {
|
|
|
- ForEach(alertConfiguration.buttons.indices) { index in
|
|
|
- let button = self.alertConfiguration.buttons[index]
|
|
|
- Button(button.title, role: button.role, action: button.action)
|
|
|
+ ForEach(alertConfiguration.buttons, id: \.self) {
|
|
|
+ Button($0.title, role: $0.role, action: $0.action)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public func showAlert(_ title: String, buttons: [AlertButton]) {
|
|
|
- self.alertConfiguration = AlertConfiguration(title: title, buttons: buttons)
|
|
|
+ public func showAlert(with configuration: AlertConfiguration) {
|
|
|
+ self.alertConfiguration = configuration
|
|
|
self.isPresenting = true
|
|
|
}
|
|
|
|
|
|
+ public func showAlert(_ title: String, buttons: [AlertButton]) {
|
|
|
+ showAlert(with: AlertConfiguration(title: title, buttons: buttons))
|
|
|
+ }
|
|
|
+
|
|
|
public func showSimpleAlert(_ title: String, action: @escaping Action) {
|
|
|
showAlert(title, buttons: [AlertButton(title: "OK", role: .cancel, action: action)])
|
|
|
}
|