2017-08-17 18:37:21 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "OWSNavigationController.h"
|
|
|
|
|
2017-08-18 15:58:16 +02:00
|
|
|
// We use a category to expose UINavigationController's private
|
|
|
|
// UINavigationBarDelegate methods.
|
|
|
|
@interface UINavigationController (OWSNavigationController) <UINavigationBarDelegate>
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
@interface OWSNavigationController () <UIGestureRecognizerDelegate>
|
2017-08-17 18:37:21 +02:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
@implementation OWSNavigationController
|
|
|
|
|
2017-08-18 15:58:16 +02:00
|
|
|
- (void)viewDidLoad
|
2017-08-17 18:37:21 +02:00
|
|
|
{
|
2017-08-18 15:58:16 +02:00
|
|
|
[super viewDidLoad];
|
2017-08-17 18:37:21 +02:00
|
|
|
|
2017-08-18 15:58:16 +02:00
|
|
|
self.interactivePopGestureRecognizer.delegate = self;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UINavigationBarDelegate
|
|
|
|
|
|
|
|
// All UINavigationController serve as the UINavigationBarDelegate for their navbar.
|
|
|
|
// We override shouldPopItem: in order to cancel some back button presses - for example,
|
|
|
|
// if a view has unsaved changes.
|
|
|
|
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
|
|
|
|
{
|
|
|
|
OWSAssert(self.interactivePopGestureRecognizer.delegate == self);
|
2017-08-17 18:37:21 +02:00
|
|
|
UIViewController *topViewController = self.topViewController;
|
|
|
|
|
2017-08-18 16:02:45 +02:00
|
|
|
// wasBackButtonClicked is YES if the back button was pressed but not
|
|
|
|
// if a back gesture was performed or if the view is popped programmatically.
|
2017-08-18 15:58:16 +02:00
|
|
|
BOOL wasBackButtonClicked = topViewController.navigationItem == item;
|
|
|
|
BOOL result = YES;
|
2017-08-17 18:37:21 +02:00
|
|
|
if (wasBackButtonClicked) {
|
2017-08-18 15:58:16 +02:00
|
|
|
if ([topViewController conformsToProtocol:@protocol(OWSNavigationView)]) {
|
|
|
|
id<OWSNavigationView> navigationView = (id<OWSNavigationView>)topViewController;
|
|
|
|
result = ![navigationView shouldCancelNavigationBack];
|
2017-08-17 18:37:21 +02:00
|
|
|
}
|
2017-08-18 15:58:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we're not going to cancel the pop/back, we need to call the super
|
|
|
|
// implementation since it has important side effects.
|
|
|
|
if (result) {
|
2017-09-13 21:35:33 +02:00
|
|
|
// NOTE: result might end up NO if the super implementation cancels the
|
|
|
|
// the pop/back.
|
2017-08-18 15:58:16 +02:00
|
|
|
result = [super navigationBar:navigationBar shouldPopItem:item];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - UIGestureRecognizerDelegate
|
|
|
|
|
|
|
|
// We serve as the UIGestureRecognizerDelegate of the interactivePopGestureRecognizer
|
|
|
|
// in order to cancel some "back" gestures - for example,
|
|
|
|
// if a view has unsaved changes.
|
|
|
|
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
|
|
|
|
{
|
|
|
|
UIViewController *topViewController = self.topViewController;
|
|
|
|
if ([topViewController conformsToProtocol:@protocol(OWSNavigationView)]) {
|
|
|
|
id<OWSNavigationView> navigationView = (id<OWSNavigationView>)topViewController;
|
|
|
|
return ![navigationView shouldCancelNavigationBack];
|
2017-08-17 18:37:21 +02:00
|
|
|
} else {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|