diff --git a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.h b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.h index 32789d0..2810b3c 100644 --- a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.h +++ b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.h @@ -14,7 +14,7 @@ @class MVMCoreNavigationObject; @class MVMCoreLoadObject; -@interface MVMCoreNavigationHandler : NSObject +@interface MVMCoreNavigationHandler : NSObject // Returns the shared instance of this singleton + (nullable instancetype)sharedNavigationHandler; @@ -25,6 +25,8 @@ // reference to main navigation controller @property (nullable, weak, nonatomic) UINavigationController *navigationController; +/// A list of possible delegates looking for information. +@property (nonnull, strong, nonatomic) NSHashTable *delegates; // Will navigate appropriately based on the load style - (void)navigateWithLoadObject:(nullable MVMCoreLoadObject *)loadObject viewController:(nonnull UIViewController *)viewController delegate:(nullable NSObject*)delegate completionHandler:(nullable void (^)(void))completionBlock; @@ -35,6 +37,14 @@ // pops or dimisses as needed - (void)removeCurrentViewController; +#pragma mark - Delegate Handling + +/// Adds a listener for navigation delegate functions +- (void)addDelegate:(nonnull id )delegate; + +/// Removes a listener for navigation delegate functions +- (void)removeDelegate:(nullable id )delegate; + #pragma mark - Navigation Simple // Uses the default navigation controller in app delegate diff --git a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.m b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.m index 260a34f..5849d9f 100644 --- a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.m +++ b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationHandler.m @@ -47,10 +47,24 @@ self.presentationQueue = [[NSOperationQueue alloc] init]; self.presentationQueue.maxConcurrentOperationCount = 1; + + self.delegates = (NSHashTable *)[NSHashTable weakObjectsHashTable]; } return self; } +#pragma mark - Delegate Handling + +- (void)addDelegate:(nonnull id )delegate { + [self.delegates addObject:delegate]; +} + +- (void)removeDelegate:(nullable id )delegate { + [self.delegates removeObject:delegate]; +} + +#pragma mark - Navigation Helpers + - (void)navigateWithLoadObject:(nullable MVMCoreLoadObject *)loadObject viewController:(nonnull UIViewController *)viewController delegate:(nullable NSObject*)delegate completionHandler:(nullable void (^)(void))completionBlock { BOOL shouldAnimate = !loadObject.requestParameters.shouldNotAnimatePush; diff --git a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m index 5a39b73..bdd32eb 100644 --- a/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m +++ b/MVMCore/MVMCore/PresentationHandling/MVMCoreNavigationOperation.m @@ -13,6 +13,7 @@ #import "MVMCoreLoggingHandler.h" #import "MVMCoreLoadingOverlayHandler.h" #import "MVMCoreViewControllerAnimatedTransitioning.h" +#import "MVMCoreNavigationHandler.h" @interface MVMCoreNavigationOperation () @@ -225,6 +226,8 @@ [super markAsFinished]; } +#pragma mark - Delegate + - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { if (self.navigationObject.stopLoadingOverlay) { [[MVMCoreLoadingOverlayHandler sharedLoadingOverlay] stopLoading:YES]; @@ -234,6 +237,11 @@ if (self.delegate && [self.delegate respondsToSelector:@selector(navigationController:willDisplayViewController:)]) { [self.delegate navigationController:navigationController willDisplayViewController:viewController]; } + for (NSObject *delegate in [MVMCoreNavigationHandler sharedNavigationHandler].delegates.allObjects) { + if (delegate && [delegate respondsToSelector:@selector(navigationController:willDisplayViewController:)]) { + [delegate navigationController:navigationController willDisplayViewController:viewController]; + } + } } - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { @@ -241,8 +249,13 @@ if (self.delegate && [self.delegate respondsToSelector:@selector(navigationController:displayedViewController:)]) { [self.delegate navigationController:navigationController displayedViewController:viewController]; } + for (NSObject *delegate in [MVMCoreNavigationHandler sharedNavigationHandler].delegates.allObjects) { + 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]; }); diff --git a/MVMCore/MVMCore/PresentationHandling/MVMCorePresentAnimationOperation.m b/MVMCore/MVMCore/PresentationHandling/MVMCorePresentAnimationOperation.m index 797346c..206dd95 100644 --- a/MVMCore/MVMCore/PresentationHandling/MVMCorePresentAnimationOperation.m +++ b/MVMCore/MVMCore/PresentationHandling/MVMCorePresentAnimationOperation.m @@ -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 *delegate in [MVMCoreNavigationHandler sharedNavigationHandler].delegates.allObjects) { + 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 *delegate in [MVMCoreNavigationHandler sharedNavigationHandler].delegates.allObjects) { + if (delegate && [delegate respondsToSelector:@selector(viewController:didPresentViewController:)]) { + [delegate viewController:self.presentingViewController didPresentViewController:self.presentedViewController]; + } + } [self markAsFinished]; }]; }