|
|
@@ -0,0 +1,48 @@
|
|
|
+//
|
|
|
+// AlertPresenter.swift
|
|
|
+// CoordinatorSUIExamples
|
|
|
+//
|
|
|
+// Created by Pavel Yurchenko on 04.12.2024.
|
|
|
+//
|
|
|
+
|
|
|
+import SwiftUI
|
|
|
+
|
|
|
+final class AlertPresenter: ObservableObject {
|
|
|
+
|
|
|
+ private struct AlertConfiguration {
|
|
|
+ let title: String
|
|
|
+ let buttons: [AlertButton]
|
|
|
+ }
|
|
|
+
|
|
|
+ struct AlertButton {
|
|
|
+ let title: String
|
|
|
+ let role: ButtonRole
|
|
|
+ let action: Action
|
|
|
+ }
|
|
|
+
|
|
|
+ private var alertConfiguration = AlertConfiguration(title: "", buttons: [])
|
|
|
+
|
|
|
+ @Published
|
|
|
+ var isPresenting: Bool = false
|
|
|
+
|
|
|
+ var title: String {
|
|
|
+ alertConfiguration.title
|
|
|
+ }
|
|
|
+
|
|
|
+ @ViewBuilder
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func showAlert(_ title: String, buttons: [AlertButton]) {
|
|
|
+ self.alertConfiguration = AlertConfiguration(title: title, buttons: buttons)
|
|
|
+ self.isPresenting = true
|
|
|
+ }
|
|
|
+
|
|
|
+ func showSimpleAlert(_ title: String, action: @escaping Action) {
|
|
|
+ showAlert(title, buttons: [AlertButton(title: "OK", role: .cancel, action: action)])
|
|
|
+ }
|
|
|
+}
|