From 34fa65d498bad3f9df90cb656f4433667db436dd Mon Sep 17 00:00:00 2001 From: "Hedden, Kyle Matthew" Date: Wed, 12 Feb 2020 09:24:52 -0500 Subject: [PATCH] Add methods that will cancel pending or executing alert operations. --- .../AlertHandling/MVMCoreAlertHandler.h | 37 +++++++++++++------ .../AlertHandling/MVMCoreAlertHandler.m | 29 +++++++++++++++ .../AlertHandling/MVMCoreAlertOperation.h | 15 +++++--- 3 files changed, 63 insertions(+), 18 deletions(-) diff --git a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.h b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.h index b6454c0..a4c44ec 100644 --- a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.h +++ b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.h @@ -16,15 +16,15 @@ @interface MVMCoreAlertHandler : NSObject -// Returns the shared instance of this singleton +/// Returns the shared instance of this singleton + (nullable instancetype)sharedAlertHandler; #pragma mark - Popup Alert Functions -// Returns if any alert is currently showing (even if supressed). +/// Returns if any alert is currently showing (even if supressed). - (BOOL)alertCurrentlyShowing; -// Returns if a greedy alert is currently showing (even if supressed). +/// Returns if a greedy alert is currently showing (even if supressed). - (BOOL)greedyAlertShowing; /** Shows the popup with the passed in parameter. @@ -53,38 +53,51 @@ */ - (nonnull UIAlertController *)showAlertWithAlertObject:(nonnull MVMCoreAlertObject *)alertObject; -// Removes all alerts. +/** Cancels and removes an alert operation for the given alertObject. + * @param alertObject The alertObject scheduled to be shown. + */ +- (void)removeAlertViewForObject:(nonnull MVMCoreAlertObject *)alertObject; + +/** Iterates through all scheduled alerts and cancels any that match the provided predicate. +* @param predicate The predicate block to decide whether to cancel an alert. +*/ +- (void)removeAlertViewUsingPredicate:(BOOL(^_Nonnull)(MVMCoreAlertObject * _Nonnull obj))predicate; + +/// Removes all alerts. - (void)removeAllAlertViews; #pragma mark - Supression Functions -// Returns true if alerts are supressed. +/// Returns true if alerts are supressed. - (BOOL)mfAlertsSupressed; -// Supresses the alerts (Used by other "apps" in our app). +/// Supresses the alerts (Used by other "apps" in our app). - (void)supressMFAlerts; -// Unsupresses the alerts (Used by other "apps" in our app). +/// Unsupresses the alerts (Used by other "apps" in our app). - (void)unSupressMFAlerts; #pragma mark - Top Alert Functions -// Show based on the object. Will be used by the architecture. +/// Show based on the object. Will be used by the architecture. - (void)showTopAlertWithObject:(nullable MVMCoreTopAlertObject *)topAlertObject; -// Convenience functions +/// Convenience functions - (void)showTopAlertErrorWithMessage:(nullable NSString *)message; - (void)showTopAlertConfirmationWithMessage:(nullable NSString *)message; - (void)showTopAlertWithType:(nullable NSString *)type message:(nullable NSString *)message subMessage:(nullable NSString *)subMessage persistent:(BOOL)persistent actionMap:(nullable NSDictionary *)actionMap additionalData:(nullable NSDictionary *)additionalData; - (void)showTopAlertWithType:(nullable NSString *)type message:(nullable NSString *)message subMessage:(nullable NSString *)subMessage persistent:(BOOL)persistent buttonTitle:(nullable NSString *)buttonTitle userActionHandler:(nullable void (^)(id _Nonnull sender))userActionHandler; -// Hides the current alert view. +/// Hides the current alert view. - (void)hideTopAlertView; -// Hides a persistent alert based on the type string. +/// Hides a persistent alert based on the type string. - (void)hidePersistentTopAlertViewOfType:(nullable NSString *)type; -// Removes all top alerts. +/// Removes a scheduled top alert given its top alert object. +- (void)removeTopAlertForObject:(nonnull MVMCoreTopAlertObject *)topAlertObject; + +/// Removes all top alerts. - (void)removeAllTopAlerts; /// Returns YES if the persistent type is already registered in the alert queue. diff --git a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.m b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.m index 5d5f93b..0c9393a 100644 --- a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.m +++ b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertHandler.m @@ -107,6 +107,25 @@ return controller; } +- (void)removeAlertViewForObject:(MVMCoreAlertObject *)alertObject { + for (MVMCoreAlertOperation *operation in self.popupAlertQueue.operations) { + if ([operation.currentAlertView isKindOfClass:[MVMCoreAlertController class]] && [(MVMCoreAlertController *)operation.currentAlertView alertObject] == alertObject) { + [operation cancel]; + } + } +} + +- (void)removeAlertViewUsingPredicate:(BOOL(^)(MVMCoreAlertObject *obj))predicate { + for (MVMCoreAlertOperation *operation in self.popupAlertQueue.operations) { + if ([operation.currentAlertView isKindOfClass:[MVMCoreAlertController class]]) { + MVMCoreAlertObject *alertObject = [(MVMCoreAlertController *)operation.currentAlertView alertObject]; + if (alertObject && predicate(alertObject)) { + [operation cancel]; + } + } + } +} + - (void)removeAllAlertViews { [self.popupAlertQueue cancelAllOperations]; } @@ -225,6 +244,16 @@ } } +- (void)removeTopAlertForObject:(MVMCoreTopAlertObject *)topAlertObject { + for (MVMCoreTopAlertOperation *operation in self.topAlertQueue.operations) { + // Finds an cancels top alerts associated with the object. + if (operation.topAlertObject == topAlertObject) { + operation.reAddAfterCancel = NO; + [operation cancel]; + } + } +} + - (void)removeAllTopAlerts { [self.topAlertQueue cancelAllOperations]; } diff --git a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertOperation.h b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertOperation.h index e83ef07..c48fd86 100644 --- a/MVMCore/MVMCore/AlertHandling/MVMCoreAlertOperation.h +++ b/MVMCore/MVMCore/AlertHandling/MVMCoreAlertOperation.h @@ -14,23 +14,26 @@ @interface MVMCoreAlertOperation : MVMCoreOperation -// If this operation is temporarily paused. +/// Alert controller to be displayed. +@property (nonnull, readonly) UIAlertController *currentAlertView; + +/// If this operation is temporarily paused. @property (readonly, getter=isPaused) BOOL paused; -// If this alert is a greedy alert (See MVMCoreAlertHandler). +/// If this alert is a greedy alert (See MVMCoreAlertHandler). @property (readonly, getter=isGreedy) BOOL greedy; -// The alert delegate if needed. +/// The alert delegate if needed. @property (readonly, nullable, nonatomic, weak) NSObject *alertDelegate; -// Initializes the operation with the alert to display and if it is greedy or not. +/// Initializes the operation with the alert to display and if it is greedy or not. - (nullable instancetype)initWithAlert:(nonnull UIAlertController *)alert isGreedy:(BOOL)isGreedy; - (nullable instancetype)initWithAlert:(nonnull UIAlertController *)alert isGreedy:(BOOL)isGreedy alertDelegate:(nullable id )alertDelegate; -// Pauses the operation. Temporarily removes any alert. +/// Pauses the operation. Temporarily removes any alert. - (void)pause; -// Unpauses the operation, resuming any alert. +/// Unpauses the operation, resuming any alert. - (void)unpause; @end