diff --git a/MVMCore/MVMCore.xcodeproj/project.pbxproj b/MVMCore/MVMCore.xcodeproj/project.pbxproj index 28dcf0f..a0778b2 100644 --- a/MVMCore/MVMCore.xcodeproj/project.pbxproj +++ b/MVMCore/MVMCore.xcodeproj/project.pbxproj @@ -158,6 +158,7 @@ BB780ADF250F8C890030BD2F /* ActionNoopModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB780ADE250F8C890030BD2F /* ActionNoopModel.swift */; }; D282AAB62240085300C46919 /* MVMCoreGetterUtility+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D282AAB52240085300C46919 /* MVMCoreGetterUtility+Extension.swift */; }; D282AAB82240342D00C46919 /* NSNumber+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D282AAB72240342D00C46919 /* NSNumber+Extension.swift */; }; + D2CAC7C82510F0C500C75681 /* MVMCoreAlertHandler+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2CAC7C72510F0C500C75681 /* MVMCoreAlertHandler+Extension.swift */; }; D2DEDCB723C63F3B00C44CC4 /* Clamping.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2DEDCB623C63F3B00C44CC4 /* Clamping.swift */; }; D2DEDCB923C6400600C44CC4 /* UnitInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2DEDCB823C6400600C44CC4 /* UnitInterval.swift */; }; D2DEDCBB23C65BC300C44CC4 /* Percent.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2DEDCBA23C65BC300C44CC4 /* Percent.swift */; }; @@ -310,6 +311,7 @@ BB780ADE250F8C890030BD2F /* ActionNoopModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActionNoopModel.swift; sourceTree = ""; }; D282AAB52240085300C46919 /* MVMCoreGetterUtility+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MVMCoreGetterUtility+Extension.swift"; sourceTree = ""; }; D282AAB72240342D00C46919 /* NSNumber+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSNumber+Extension.swift"; sourceTree = ""; }; + D2CAC7C72510F0C500C75681 /* MVMCoreAlertHandler+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MVMCoreAlertHandler+Extension.swift"; sourceTree = ""; }; D2DEDCB623C63F3B00C44CC4 /* Clamping.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Clamping.swift; sourceTree = ""; }; D2DEDCB823C6400600C44CC4 /* UnitInterval.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnitInterval.swift; sourceTree = ""; }; D2DEDCBA23C65BC300C44CC4 /* Percent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Percent.swift; sourceTree = ""; }; @@ -597,6 +599,7 @@ AFBB967F1FBA3A9A0008D868 /* MVMCoreAlertHandler.m */, AFBB96801FBA3A9A0008D868 /* MVMCoreAlertObject.h */, AFBB96811FBA3A9A0008D868 /* MVMCoreAlertObject.m */, + D2CAC7C72510F0C500C75681 /* MVMCoreAlertHandler+Extension.swift */, 0184D3E224B8D0C600A05369 /* MVMCoreAlertObject+Swift.swift */, AFBB96821FBA3A9A0008D868 /* MVMCoreAlertOperation.h */, AFBB96831FBA3A9A0008D868 /* MVMCoreAlertOperation.m */, @@ -906,6 +909,7 @@ AF43A5781FBA5B7C008E9347 /* MVMCoreJSONConstants.m in Sources */, AFBB96691FBA3A570008D868 /* MVMCoreRequestParameters.m in Sources */, AFED77A31FCCA29400BAE689 /* MVMCoreViewControllerNibMappingObject.m in Sources */, + D2CAC7C82510F0C500C75681 /* MVMCoreAlertHandler+Extension.swift in Sources */, 8876D5EB1FB50AB000EB2E3D /* NSDecimalNumber+MFConvenience.m in Sources */, 0184D3DB24B7D5A600A05369 /* ActionAlertModel.swift in Sources */, AFBB96A41FBA3A9A0008D868 /* MVMCoreTopAlertObject.m in Sources */, diff --git a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler+Extension.swift b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler+Extension.swift new file mode 100644 index 0000000..44e8a08 --- /dev/null +++ b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler+Extension.swift @@ -0,0 +1,65 @@ +// +// MVMCoreAlertHandler+Extension.swift +// MVMCore +// +// Created by Scott Pfeil on 9/15/20. +// Copyright © 2020 myverizon. All rights reserved. +// + +import Foundation + +// Temporary, until we can move page checking logic into isReady of topAlertOperation. +@objcMembers public class NotificationPagesOperation: Operation { + public let pages: [String] + + public init(with pages: [String]) { + self.pages = pages + } + + public override var isReady: Bool { + get { + return super.isReady && isCancelled + } + } +} + +// TODO: Move this type of logic into isReady for the top alert operation... then can remove this dependency operation. +public extension MVMCoreAlertHandler { + + /// Adds a page type dependency to the operation + @objc func addPagesDependency(to operation: MVMCoreTopAlertOperation) { + // This notification may only show for certain pages. + guard let pages = operation.topAlertObject.json?.optionalArrayForKey("pages") as? [String], + pages.count > 0 else { return } + let pagesOperation = NotificationPagesOperation(with: pages) + pageOperations.addOperation(pagesOperation) + operation.addDependency(pagesOperation) + } + + /// checks top alerts for page dependencies with the new pageType + @objc func checkPagesDependency(for pageType: String?) { + // If the current running operation should not show on the current page, cancel it. If it's persistent, re-add it. If another operation has had the dependency remove previously, add it back. + for case let operation as MVMCoreTopAlertOperation in topAlertQueue.operations { + if !operation.isCancelled, + let pages = operation.topAlertObject.json?.optionalArrayForKey("pages") as? [String], + (pageType == nil || !pages.contains(pageType!)) { + if operation.isExecuting { + operation.reAddAfterCancel = operation.topAlertObject.persistent + operation.cancel() + } else if !operation.dependencies.contains(where: { !$0.isFinished }) { + let pagesOperation = NotificationPagesOperation(with: pages) + pageOperations.addOperation(pagesOperation) + operation.addDependency(pagesOperation) + } + } + } + + // Remove the dependency if it contains the current page. + guard let pageType = pageType else { return } + for case let operation as NotificationPagesOperation in pageOperations.operations { + if operation.pages.contains(pageType) { + operation.cancel() + } + } + } +} diff --git a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.h b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.h index a4c44ec..e41cff4 100644 --- a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.h +++ b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.h @@ -16,6 +16,15 @@ @interface MVMCoreAlertHandler : NSObject +// An operation queue for displaying popup alerts. +@property (nonnull, strong, nonatomic) NSOperationQueue *popupAlertQueue; + +// An operation queue for top alerts +@property (nonnull, strong, nonatomic) NSOperationQueue *topAlertQueue; + +/// Stores any page dependencies for top alerts +@property (nonnull, strong, nonatomic) NSOperationQueue *pageOperations; + /// Returns the shared instance of this singleton + (nullable instancetype)sharedAlertHandler; diff --git a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.m b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.m index 0c9393a..1e64663 100644 --- a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.m +++ b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.m @@ -14,18 +14,13 @@ #import "MVMCoreJSONConstants.h" #import "NSDictionary+MFConvenience.h" #import "NSArray+MFConvenience.h" +#import @interface MVMCoreAlertHandler () // Flag that keeps track of if the alerts are supressed or not. @property (assign, nonatomic) BOOL mfAlertsSupressed; -// An operation queue for displaying popup alerts. -@property (nonnull, strong, nonatomic) NSOperationQueue *popupAlertQueue; - -// An operation queue for top alerts -@property (nonnull, strong, nonatomic) NSOperationQueue *topAlertQueue; - @end @implementation MVMCoreAlertHandler @@ -47,6 +42,7 @@ self.popupAlertQueue.maxConcurrentOperationCount = 1; self.topAlertQueue = [[NSOperationQueue alloc] init]; self.topAlertQueue.maxConcurrentOperationCount = 1; + self.pageOperations = [[NSOperationQueue alloc] init]; } return self; } @@ -167,14 +163,19 @@ if (topAlertObject) { __block MVMCoreTopAlertOperation *alertOperation = [[MVMCoreTopAlertOperation alloc] initWithTopAlertObject:topAlertObject]; + __weak typeof(self) weakSelf = self; [alertOperation setCompletionBlock:^{ // If the alert was cancelled to show another with higher priority, re-add to the operation when cancelled to the queue. if (alertOperation.reAddAfterCancel) { - [self showTopAlertWithObject:alertOperation.topAlertObject]; + [weakSelf showTopAlertWithObject:alertOperation.topAlertObject]; } alertOperation = nil; }]; + + // This notification may only show for certain pages. + [self addPagesDependencyTo:alertOperation]; + [self.topAlertQueue addOperation:alertOperation]; // If the current running operation is persistent and has a lower queue priority then the added operation, cancel it and re-add it. diff --git a/MVMCore/MVMCore/AlertHandling/MVMCoreTopAlertObject.h b/MVMCore/MVMCore/AlertHandling/MVMCoreTopAlertObject.h index 5a4b587..350f6e1 100644 --- a/MVMCore/MVMCore/AlertHandling/MVMCoreTopAlertObject.h +++ b/MVMCore/MVMCore/AlertHandling/MVMCoreTopAlertObject.h @@ -15,6 +15,7 @@ extern NSUInteger const TopAlertDismissTime; @property (nonatomic) BOOL persistent; @property (nullable, nonatomic) NSString *type; +@property (nonatomic) NSOperationQueuePriority queuePriority; // The text @property (nullable, strong, nonatomic) NSString *title; @@ -49,6 +50,9 @@ extern NSUInteger const TopAlertDismissTime; @property (nullable, strong, nonatomic) UIColor *backgroundColor; @property (nullable, strong, nonatomic) UIColor *textColor; +// The full top alert json. Currently only used for molecular. +@property (nullable, strong, nonatomic) NSDictionary *json; + - (nullable instancetype)initWithResponseInfo:(nullable NSDictionary *)responseInfo; - (nullable instancetype)initWithType:(nullable NSString *)type message:(nullable NSString *)message; diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadObject.h b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadObject.h index f167ea7..92063b8 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadObject.h +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadObject.h @@ -56,6 +56,9 @@ @property (nonatomic) BOOL pageDataFromCache; @property (nonatomic) BOOL moduleDataFromCache; +// The full response json +@property (nullable, strong, nonatomic) NSDictionary *responseJSON; + - (nullable instancetype)initWithPageJSON:(nullable NSDictionary *)pageJSON modulesJSON:(nullable NSDictionary *)modulesJSON requestParameters:(nullable MVMCoreRequestParameters *)requestParameters dataForPage:(nullable NSDictionary *)dataForPage delegateObject:(nullable DelegateObject *)delegateObject; - (nullable instancetype)initWithRequestParameters:(nullable MVMCoreRequestParameters *)requestParameters dataForPage:(nullable NSDictionary *)dataForPage delegateObject:(nullable DelegateObject *)delegateObject; diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation.m b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation.m index 2f4019b..b6e0ca9 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation.m +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadRequestOperation.m @@ -354,6 +354,8 @@ return; } + loadObject.responseJSON = jsonDictionary; + // Store the new page data if we didn't load page data from the cache. NSDictionary *pageJSON = [jsonDictionary objectForKey:KeyPage ofType:[NSDictionary class]]; if (!loadObject.pageDataFromCache) {