summaryrefslogtreecommitdiffhomepage
path: root/ios/Operations/AlertPresenter.swift
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2022-09-25 16:34:23 +0200
committerAndrej Mihajlov <and@mullvad.net>2022-09-26 16:35:38 +0200
commitf389956c2cd884df142adbf00ff2ac7e2f69c2b2 (patch)
tree5f7d4869324760eb4ba0107c0356edc89efd1457 /ios/Operations/AlertPresenter.swift
parent2e83b1ca27ff243a615ff10c94c20840b38dfd45 (diff)
downloadmullvadvpn-f389956c2cd884df142adbf00ff2ac7e2f69c2b2.tar.xz
mullvadvpn-f389956c2cd884df142adbf00ff2ac7e2f69c2b2.zip
Move AsyncOperation into Operations static library and add separate tests
Diffstat (limited to 'ios/Operations/AlertPresenter.swift')
-rw-r--r--ios/Operations/AlertPresenter.swift72
1 files changed, 72 insertions, 0 deletions
diff --git a/ios/Operations/AlertPresenter.swift b/ios/Operations/AlertPresenter.swift
new file mode 100644
index 0000000000..6bf4470e23
--- /dev/null
+++ b/ios/Operations/AlertPresenter.swift
@@ -0,0 +1,72 @@
+//
+// AlertPresenter.swift
+// MullvadVPN
+//
+// Created by pronebird on 04/06/2020.
+// Copyright © 2020 Mullvad VPN AB. All rights reserved.
+//
+
+#if canImport(UIKit)
+
+import UIKit
+
+public final class AlertPresenter {
+ static let alertControllerDidDismissNotification = Notification
+ .Name("UIAlertControllerDidDismiss")
+
+ private let operationQueue: OperationQueue = {
+ let operationQueue = AsyncOperationQueue()
+ operationQueue.name = "AlertPresenterQueue"
+ operationQueue.maxConcurrentOperationCount = 1
+ return operationQueue
+ }()
+
+ private static let initClass: Void = {
+ /// Swizzle `viewDidDisappear` on `UIAlertController` in order to be able to
+ /// detect when the controller disappears.
+ /// The event is broadcasted via `AlertPresenter.alertControllerDidDismissNotification` notification.
+ swizzleMethod(
+ aClass: UIAlertController.self,
+ originalSelector: #selector(UIAlertController.viewDidDisappear(_:)),
+ newSelector: #selector(UIAlertController.alertPresenter_viewDidDisappear(_:))
+ )
+ }()
+
+ public init() {
+ _ = Self.initClass
+ }
+
+ public func enqueue(
+ _ alertController: UIAlertController,
+ presentingController: UIViewController,
+ presentCompletion: (() -> Void)? = nil
+ ) {
+ let operation = PresentAlertOperation(
+ alertController: alertController,
+ presentingController: presentingController,
+ presentCompletion: presentCompletion
+ )
+
+ operationQueue.addOperation(operation)
+ }
+
+ public func cancelAll() {
+ operationQueue.cancelAllOperations()
+ }
+}
+
+private extension UIAlertController {
+ @objc dynamic func alertPresenter_viewDidDisappear(_ animated: Bool) {
+ // Call super implementation
+ alertPresenter_viewDidDisappear(animated)
+
+ if presentingViewController == nil {
+ NotificationCenter.default.post(
+ name: AlertPresenter.alertControllerDidDismissNotification,
+ object: self
+ )
+ }
+ }
+}
+
+#endif