浏览代码

Moving AlertPresenter to the CoordinatorUI package

Pavel Yurchenko 1 年之前
父节点
当前提交
bfe07ff2bd
共有 1 个文件被更改,包括 48 次插入0 次删除
  1. 48 0
      CoordinatorSUI/Sources/CoordinatorSUI/AlertPresenter.swift

+ 48 - 0
CoordinatorSUI/Sources/CoordinatorSUI/AlertPresenter.swift

@@ -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)])
+    }
+}