Notify other listeners/delegates

This commit is contained in:
Pfeil, Scott Robert 2020-06-02 14:04:44 -04:00
parent 90d2df6fb0
commit e21e5b520b
5 changed files with 55 additions and 84 deletions

View File

@ -9,12 +9,9 @@
#import "MVMCoreOperation.h"
#import <UIKit/UIKit.h>
#import <MVMCore/MVMCorePresentationDelegateProtocol.h>
@interface MVMCoreDismissViewControllerOperation : MVMCoreOperation
@property (nullable, weak, nonatomic) NSObject<MVMCorePresentationDelegateProtocol> *delegate;
// Goes up the presentation chain to only dismiss the top presented.
- (nullable instancetype)initAndDismissTopViewController:(nullable UIViewController *)viewController animated:(BOOL)animated;

View File

@ -19,7 +19,7 @@
#import "MVMCoreLoadingOverlayHandler.h"
#import "MVMCoreDispatchUtility.h"
@interface MVMCoreNavigationHandler() <MVMCorePresentationDelegateProtocol>
@interface MVMCoreNavigationHandler ()
@property (strong, nonatomic) NSOperationQueue *navigationQueue;
@property (strong, nonatomic) NSOperationQueue *presentationQueue;
@ -53,6 +53,25 @@
return self;
}
#pragma mark - Delegate Handling
- (void)addDelegate:(nonnull id <MVMCorePresentationDelegateProtocol>)delegate {
[self.delegates addPointer:(__bridge void *)(delegate)];
}
- (void)removeDelegate:(nullable id <MVMCorePresentationDelegateProtocol>)delegate {
NSUInteger index = NSNotFound;
for (NSUInteger i = 0; i < self.delegates.count; i++) {
if (delegate == [self.delegates pointerAtIndex:i]) {
index = i;
break;
}
}
if (index != NSNotFound) {
[self.delegates removePointerAtIndex:index];
}
}
#pragma mark - Navigation Helpers
- (void)navigateWithLoadObject:(nullable MVMCoreLoadObject *)loadObject viewController:(nonnull UIViewController *)viewController delegate:(nullable NSObject<MVMCorePresentationDelegateProtocol>*)delegate completionHandler:(nullable void (^)(void))completionBlock {
@ -101,8 +120,8 @@
navigationObject.stopLoadingOverlay = YES;
MVMCoreNavigationOperation *navigationOperation = [[MVMCoreNavigationOperation alloc] initWithNavigationObject:navigationObject];
navigationOperation.delegate = self;
navigationOperation.completionBlock = [self getCompletionAndHandleDelegate:delegate completionHandler:completionBlock];
navigationOperation.delegate = delegate;
navigationOperation.completionBlock = completionBlock;
[self.navigationQueue addOperation:navigationOperation];
}
@ -288,8 +307,8 @@
[MVMCoreLoggingHandler addErrorToLog:error];
} else {
MVMCorePresentViewControllerOperation *operation = [[MVMCorePresentViewControllerOperation alloc] initWithPresentingViewController:controllerToPresentOn presentedViewController:viewController animated:animated];
operation.delegate = self;
operation.completionBlock = [self getCompletionAndHandleDelegate:delegate completionHandler:completionBlock];
operation.delegate = delegate;
operation.completionBlock = completionBlock;
[self.presentationQueue addOperation:operation];
}
}];
@ -300,8 +319,7 @@
// Dismiss on the main navigation controller.
UIViewController *controllerToPresentOn = self.viewControllerToPresentOn ?: [UIApplication sharedApplication].keyWindow.rootViewController;
MVMCoreDismissViewControllerOperation *operation = [[MVMCoreDismissViewControllerOperation alloc] initAndDismissTopViewController:controllerToPresentOn animated:animated];
operation.delegate = self;
operation.completionBlock = [self getCompletionAndHandleDelegate:delegate completionHandler:completionBlock];
operation.completionBlock = completionBlock;
[[NSOperationQueue mainQueue] addOperation:operation];
}];
}
@ -309,8 +327,7 @@
- (void)dismissViewController:(nonnull UIViewController *)viewController animated:(BOOL)animated delegate:(nullable NSObject<MVMCorePresentationDelegateProtocol>*)delegate completionHandler:(nullable void (^)(void))completionBlock {
MVMCoreDismissViewControllerOperation *operation = [[MVMCoreDismissViewControllerOperation alloc] initAndDismissViewController:viewController animated:animated];
operation.delegate = self;
operation.completionBlock = [self getCompletionAndHandleDelegate:delegate completionHandler:completionBlock];
operation.completionBlock = completionBlock;
[[NSOperationQueue mainQueue] addOperation:operation];
}
@ -319,78 +336,9 @@
// Dismiss on the main navigation controller.
UIViewController *controllerToPresentOn = self.viewControllerToPresentOn ?: [UIApplication sharedApplication].keyWindow.rootViewController;
MVMCoreDismissViewControllerOperation *operation = [[MVMCoreDismissViewControllerOperation alloc] initAndDismissViewController:controllerToPresentOn animated:animated];
operation.delegate = self;
operation.completionBlock = [self getCompletionAndHandleDelegate:delegate completionHandler:completionBlock];
operation.completionBlock = completionBlock;
[[NSOperationQueue mainQueue] addOperation:operation];
}];
}
#pragma mark - Delegate Handling
- (void)addDelegate:(nonnull id <MVMCorePresentationDelegateProtocol>)delegate {
[self.delegates addPointer:(__bridge void *)(delegate)];
}
- (void)removeDelegate:(nullable id <MVMCorePresentationDelegateProtocol>)delegate {
NSUInteger index = NSNotFound;
for (NSUInteger i = 0; i < self.delegates.count; i++) {
if (delegate == [self.delegates pointerAtIndex:i]) {
index = i;
break;
}
}
if (index != NSNotFound) {
[self.delegates removePointerAtIndex:index];
}
}
- (nullable void (^)(void))getCompletionAndHandleDelegate:(nullable NSObject<MVMCorePresentationDelegateProtocol>*)delegate completionHandler:(nullable void (^)(void))completionBlock {
if (!delegate) {
return completionBlock;
}
[self addDelegate:delegate];
__weak typeof(delegate) weakDelegate = delegate;
__weak typeof(self) weakSelf = self;
return ^void() {
[weakSelf removeDelegate:weakDelegate];
completionBlock();
};
}
// Below functions forward to all delegates.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
for (id <MVMCorePresentationDelegateProtocol>delegate in self.delegates) {
if (delegate && [delegate respondsToSelector:@selector(navigationController:willDisplayViewController:)]) {
[delegate navigationController:navigationController willDisplayViewController:viewController];
}
}
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
for (id <MVMCorePresentationDelegateProtocol>delegate in self.delegates) {
if (delegate && [delegate respondsToSelector:@selector(navigationController:displayedViewController:)]) {
[delegate navigationController:navigationController displayedViewController:viewController];
}
}
// Legacy Notifier: Notify that page has changed
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:MVMCoreNotificationViewControllerChanged object:nil];
});
}
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
if (delegate && [delegate respondsToSelector:@selector(navigationController:animationControllerForOperation:fromViewController:toViewController:)]) {
id animatedTransitioning = [self.delegate navigationController:navigationController animationControllerForOperation:operation fromViewController:fromVC toViewController:toVC];
if ([animatedTransitioning conformsToProtocol:@protocol(MVMCoreViewControllerAnimatedTransitioning)]) {
[animatedTransitioning addObserver:self forKeyPath:@"interactiveTransitionCanceled" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
self.animatedTransitioning = animatedTransitioning;
}
return animatedTransitioning;
} else {
return nil;
}
}
@end

View File

@ -237,7 +237,11 @@
if (self.delegate && [self.delegate respondsToSelector:@selector(navigationController:willDisplayViewController:)]) {
[self.delegate navigationController:navigationController willDisplayViewController:viewController];
}
for (NSObject <MVMCorePresentationDelegateProtocol>*delegate in [MVMCoreNavigationHandler sharedNavigationHandler].delegates) {
if (delegate && [delegate respondsToSelector:@selector(navigationController:willDisplayViewController:)]) {
[delegate navigationController:navigationController willDisplayViewController:viewController];
}
}
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
@ -245,8 +249,13 @@
if (self.delegate && [self.delegate respondsToSelector:@selector(navigationController:displayedViewController:)]) {
[self.delegate navigationController:navigationController displayedViewController:viewController];
}
for (NSObject <MVMCorePresentationDelegateProtocol>*delegate in [MVMCoreNavigationHandler sharedNavigationHandler].delegates) {
if (delegate && [delegate respondsToSelector:@selector(navigationController:displayedViewController:)]) {
[delegate navigationController:navigationController displayedViewController:viewController];
}
}
[self markAsFinished];
// Notify that page has changed
// Legacy notify that page has changed
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:MVMCoreNotificationViewControllerChanged object:nil];
});

View File

@ -7,6 +7,7 @@
//
#import "MVMCorePresentAnimationOperation.h"
#import "MVMCoreNavigationHandler.h"
@interface MVMCorePresentAnimationOperation ()
@ -39,12 +40,22 @@
if (self.delegate && [self.delegate respondsToSelector:@selector(viewController:willPresentViewController:)]) {
[self.delegate viewController:self.presentingViewController willPresentViewController:self.presentedViewController];
}
for (NSObject <MVMCorePresentationDelegateProtocol>*delegate in [MVMCoreNavigationHandler sharedNavigationHandler].delegates) {
if (delegate && [delegate respondsToSelector:@selector(viewController:willPresentViewController:)]) {
[delegate viewController:self.presentingViewController willPresentViewController:self.presentedViewController];
}
}
[self.presentingViewController presentViewController:self.presentedViewController animated:self.animate completion:^{
if (self.delegate && [self.delegate respondsToSelector:@selector(viewController:didPresentViewController:)]) {
[self.delegate viewController:self.presentingViewController didPresentViewController:self.presentedViewController];
}
for (NSObject <MVMCorePresentationDelegateProtocol>*delegate in [MVMCoreNavigationHandler sharedNavigationHandler].delegates) {
if (delegate && [delegate respondsToSelector:@selector(viewController:didPresentViewController:)]) {
[delegate viewController:self.presentingViewController didPresentViewController:self.presentedViewController];
}
}
[self markAsFinished];
}];
}

View File

@ -37,4 +37,10 @@
// Called when a view controller did be present on another
- (void)viewController:(nonnull UIViewController *)presentingViewController didPresentViewController:(nonnull UIViewController *)presentedViewController;
// Called when a view controller will be dismissed off another view controller
- (void)willDismissViewController:(nonnull UIViewController *)viewController;
// Called when a view controller did dismiss
- (void)didDismissViewController:(nonnull UIViewController *)viewController;
@end