115 lines
3.6 KiB
Swift
115 lines
3.6 KiB
Swift
//
|
|
// AlertOperation.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Scott Pfeil on 4/11/23.
|
|
// Copyright © 2023 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import MVMCore
|
|
import Dispatch
|
|
import Combine
|
|
|
|
public class AlertOperation: MVMCoreOperation {
|
|
|
|
private actor Properties {
|
|
private var isDisplayed: Bool = false
|
|
|
|
func set(displayed: Bool) {
|
|
isDisplayed = displayed
|
|
}
|
|
|
|
func getIsDisplayed() -> Bool {
|
|
return isDisplayed
|
|
}
|
|
}
|
|
private var properties = Properties()
|
|
|
|
//private var observer: NSKeyValueObservation?
|
|
private var cancellable: Cancellable?
|
|
|
|
public let alertController: AlertController
|
|
|
|
public let alertObject: AlertObject
|
|
|
|
public init(with alert: AlertController, alertObject: AlertObject) {
|
|
self.alertController = alert
|
|
self.alertObject = alertObject
|
|
}
|
|
|
|
deinit {
|
|
stopObservingAlertView()
|
|
}
|
|
|
|
public override func main() {
|
|
guard !checkAndHandleForCancellation() else { return }
|
|
|
|
// Observe for when it is removed.
|
|
observeForCurrentAlertViewDismissal()
|
|
|
|
// Adds the presentation to the animation queue.
|
|
MVMCoreNavigationHandler.shared()?.present(alertController, animated: true, delegate: nil) { [weak self] in
|
|
guard let self = self else { return }
|
|
Task {
|
|
// We finished but it was not displayed yet. It's possible that it was cancelled. Finish this task
|
|
if await !self.properties.getIsDisplayed() {
|
|
self.markAsFinished()
|
|
} else {
|
|
(CoreUIObject.sharedInstance()?.loggingDelegate as? MVMCoreUILoggingDelegateProtocol)?.logAlert(with: self.alertObject)
|
|
if self.isCancelled {
|
|
await self.dismissAlertView()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public override func cancel() {
|
|
super.cancel()
|
|
Task { @MainActor in
|
|
self.alertObject.alertDelegate?.alertCancelled(self.alertController)
|
|
await self.dismissAlertView()
|
|
}
|
|
}
|
|
|
|
private func dismissAlertView() async {
|
|
guard await properties.getIsDisplayed() else { return }
|
|
await withCheckedContinuation { continuation in
|
|
Task { @MainActor in
|
|
MVMCoreNavigationHandler.shared()?.dismiss(alertController, animated: true, delegate: nil) {
|
|
continuation.resume()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: Observer Functions
|
|
|
|
private func observeForCurrentAlertViewDismissal() {
|
|
stopObservingAlertView()
|
|
cancellable = alertController.publisher(for: \AlertController.visible).sink() { [weak self] visible in
|
|
guard let self = self else { return }
|
|
Task { @MainActor in
|
|
await self.properties.set(displayed: visible)
|
|
if visible {
|
|
self.alertObject.alertDelegate?.alertShown(self.alertController)
|
|
} else {
|
|
self.alertObject.alertDelegate?.alertDismissed(self.alertController)
|
|
|
|
// Is visible was set to NO, meaning that the alertview is no longer visible.
|
|
self.stopObservingAlertView()
|
|
self.markAsFinished()
|
|
}
|
|
}
|
|
}
|
|
// observer = alertController.observe(\AlertController.visible, options: [.old, .new]) { [weak self] (object, change) in
|
|
//
|
|
// }
|
|
}
|
|
|
|
private func stopObservingAlertView() {
|
|
//observer?.invalidate()
|
|
cancellable?.cancel()
|
|
}
|
|
}
|