Review Changes

This commit is contained in:
Pfeil, Scott Robert 2020-11-03 15:55:43 -05:00
parent 987c178c35
commit 4a47ec13c1
5 changed files with 45 additions and 30 deletions

View File

@ -9,22 +9,20 @@
import Foundation
public extension MVMCoreAlertHandler {
/// Re-evaluates the queue operations
@objc func reevaluteQueue() {
var higherPriorityOperationIsReady = false
var highestReadyOperation: MVMCoreTopAlertOperation?
var executingOperation: MVMCoreTopAlertOperation?
let pageType = (MVMCoreUISplitViewController.main()?.getCurrentDetailViewController() as? MVMCoreViewControllerProtocol)?.pageType
let sortedOperations = topAlertQueue.operations.sorted { $0.queuePriority.rawValue > $1.queuePriority.rawValue }
for case let operation as MVMCoreTopAlertOperation in sortedOperations {
for case let operation as MVMCoreTopAlertOperation in topAlertQueue.operations {
guard !operation.isCancelled,
!operation.isFinished else { continue }
updatePageReadiness(for: operation, with: pageType)
if operation.isReady,
highestReadyOperation == nil || operation.queuePriority.rawValue > highestReadyOperation!.queuePriority.rawValue {
highestReadyOperation = operation
}
if operation.isExecuting {
executingOperation = operation
} else if operation.isReady,
executingOperation == nil {
higherPriorityOperationIsReady = true
}
}
guard let currentOperation = executingOperation else { return }
@ -37,34 +35,30 @@ public extension MVMCoreAlertHandler {
}
// If the highest priority operation is not executing, and the executing operation is persistent, cancel it.
if higherPriorityOperationIsReady,
if let newOperation = highestReadyOperation,
currentOperation != newOperation,
currentOperation.topAlertObject.persistent {
currentOperation.reAddAfterCancel = true
currentOperation.cancel()
}
}
/// Registers with the notification center to know when pages change.
@objc func registerWithNotificationCenter() {
NotificationCenter.default.addObserver(self, selector: #selector(viewControllerChanged(notification:)), name: NSNotification.Name(rawValue: MVMCoreNotificationViewControllerChanged), object: nil)
/// Registers to know when pages change.
@objc func registerForPageChanges() {
MVMCoreNavigationHandler.shared()?.addDelegate(self)
}
/// Updates the operation isDisplayable based on the page type.
private func updatePageReadiness(for topOperation: MVMCoreTopAlertOperation, with pageType: String?) {
guard let pages = topOperation.topAlertObject.json?.optionalArrayForKey("pages") as? [String],
pages.count != 0 else {
topOperation.isDisplayable = true
return
}
extension MVMCoreAlertHandler: MVMCorePresentationDelegateProtocol {
// Update displayable for each top alert operation when page type changes, in top queue priority order.
public func navigationController(_ navigationController: UINavigationController, displayedViewController viewController: UIViewController) {
guard navigationController == MVMCoreUISplitViewController.main()?.navigationController else { return }
let pageType = (viewController as? MVMCoreViewControllerProtocol)?.pageType
let sortedOperations = topAlertQueue.operations.sorted { $0.queuePriority.rawValue > $1.queuePriority.rawValue }
for case let operation as MVMCoreTopAlertOperation in sortedOperations {
operation.updateDisplayable(byPageType: pageType)
}
guard let pageType = pageType else {
topOperation.isDisplayable = false
return
}
topOperation.isDisplayable = pages.contains(pageType)
}
/// When a detail page changes, check top alerts.
@objc private func viewControllerChanged(notification: Notification) {
reevaluteQueue()
}
}

View File

@ -42,7 +42,7 @@
self.popupAlertQueue.maxConcurrentOperationCount = 1;
self.topAlertQueue = [[NSOperationQueue alloc] init];
self.topAlertQueue.maxConcurrentOperationCount = 1;
[self registerWithNotificationCenter];
[self registerForPageChanges];
}
return self;
}
@ -172,6 +172,9 @@
alertOperation = nil;
}];
NSString *currentPageType = ((UIViewController<MVMCoreViewControllerProtocol> *)[[MVMCoreUISplitViewController mainSplitViewController] getCurrentDetailViewController]).pageType;
[alertOperation updateDisplayableByPageType:currentPageType];
[self.topAlertQueue addOperation:alertOperation];
[self reevaluteQueue];
}

View File

@ -27,6 +27,9 @@
/// Updates the operation with a new object
- (void)updateWithTopAlertObject:(nonnull MVMCoreTopAlertObject *)topAlertObject;
/// Updates the operation isDisplayable based on the page type.
- (void)updateDisplayableByPageType:(nullable NSString *)pageType;
// Pauses the operation. Temporarily removes any alert.
- (void)pause;

View File

@ -71,6 +71,19 @@
}
}
- (void)updateDisplayableByPageType:(nullable NSString *)pageType {
NSArray <NSString *>*pages = [self.topAlertObject.json array:@"pages"];
if (pages.count == 0) {
self.displayable = YES;
return;
}
if (pageType.length == 0) {
self.displayable = NO;
return;
}
self.displayable = [pages containsObject:pageType];
}
- (BOOL)isPaused {
__block BOOL isPaused;
dispatch_sync(self.pausedQueue, ^{

View File

@ -68,6 +68,8 @@ public extension MVMCoreUITopAlertView {
guard topAlertObject.json != nil,
operation.topAlertObject.type == topAlertObject.type else { continue }
operation.update(with: topAlertObject)
let pageType = (MVMCoreUISplitViewController.main()?.getCurrentDetailViewController() as? MVMCoreViewControllerProtocol)?.pageType
operation.updateDisplayable(byPageType: pageType)
MVMCoreAlertHandler.shared()?.reevaluteQueue()
return true
}