82 lines
2.8 KiB
Swift
82 lines
2.8 KiB
Swift
//
|
|
// TextFieldAlert.swift
|
|
// app
|
|
//
|
|
// Created by Sebastian Pobel on 04.01.20.
|
|
// Copyright © 2020 Marcus Handte. All rights reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct AlertFactory {
|
|
|
|
static func showAlert(alert: UIAlertController) {
|
|
if let controller = topMostViewController() {
|
|
controller.present(alert, animated: true)
|
|
}
|
|
}
|
|
|
|
private static func keyWindow() -> UIWindow? {
|
|
return UIApplication.shared.connectedScenes
|
|
.filter {$0.activationState == .foregroundActive}
|
|
.compactMap {$0 as? UIWindowScene}
|
|
.first?.windows.filter {$0.isKeyWindow}.first
|
|
}
|
|
|
|
private static func topMostViewController() -> UIViewController? {
|
|
guard let rootController = keyWindow()?.rootViewController else {
|
|
return nil
|
|
}
|
|
return topMostViewController(for: rootController)
|
|
}
|
|
|
|
private static func topMostViewController(for controller: UIViewController) -> UIViewController {
|
|
if let presentedController = controller.presentedViewController {
|
|
return topMostViewController(for: presentedController)
|
|
} else if let navigationController = controller as? UINavigationController {
|
|
guard let topController = navigationController.topViewController else {
|
|
return navigationController
|
|
}
|
|
return topMostViewController(for: topController)
|
|
} else if let tabController = controller as? UITabBarController {
|
|
guard let topController = tabController.selectedViewController else {
|
|
return tabController
|
|
}
|
|
return topMostViewController(for: topController)
|
|
}
|
|
return controller
|
|
}
|
|
}
|
|
|
|
struct MoWeStaAlert {
|
|
private let ok = NSLocalizedString("Ok", comment: "")
|
|
private let cancle = NSLocalizedString("Cancel", comment: "")
|
|
|
|
private var alert: UIAlertController
|
|
|
|
init(title: String, message: String) {
|
|
alert = UIAlertController(title: NSLocalizedString(title, comment: ""), message: NSLocalizedString(message, comment: ""), preferredStyle: .alert)
|
|
}
|
|
|
|
func addTextField(placeholder: String, inputText: String) {
|
|
alert.addTextField() { textField in
|
|
textField.placeholder = NSLocalizedString(placeholder, comment: "")
|
|
textField.text = inputText
|
|
}
|
|
}
|
|
|
|
func addOkAction(onOk: @escaping ([UITextField]?) -> Void = { _ in }) {
|
|
alert.addAction(UIAlertAction(title: ok, style: .default, handler: { _ in
|
|
return onOk(self.alert.textFields)
|
|
}))
|
|
}
|
|
|
|
func addCancelAction(onCancel: @escaping (UIAlertAction) -> Void = { _ in }) {
|
|
alert.addAction(UIAlertAction(title: cancle, style: .default, handler: onCancel))
|
|
}
|
|
|
|
func fire() {
|
|
AlertFactory.showAlert(alert: self.alert)
|
|
}
|
|
}
|