FLEXNavigationController.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // FLEXNavigationController.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 1/30/20.
  6. // Copyright © 2020 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXNavigationController.h"
  9. #import "FLEXExplorerViewController.h"
  10. #import "FLEXTabList.h"
  11. @interface UINavigationController (Private) <UIGestureRecognizerDelegate>
  12. - (void)_gestureRecognizedInteractiveHide:(UIGestureRecognizer *)sender;
  13. @end
  14. @interface UIPanGestureRecognizer (Private)
  15. - (void)_setDelegate:(id)delegate;
  16. @end
  17. @interface FLEXNavigationController ()
  18. @property (nonatomic, readonly) BOOL toolbarWasHidden;
  19. @property (nonatomic) BOOL waitingToAddTab;
  20. @end
  21. @implementation FLEXNavigationController
  22. + (instancetype)withRootViewController:(UIViewController *)rootVC {
  23. FLEXNavigationController *instance = [[self alloc] initWithRootViewController:rootVC];
  24. // Give root view controllers a Done button
  25. UIBarButtonItem *done = [[UIBarButtonItem alloc]
  26. initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  27. target:instance
  28. action:@selector(dismissAnimated)
  29. ];
  30. // Prepend the button if other buttons exist already
  31. NSArray *existingItems = rootVC.navigationItem.rightBarButtonItems;
  32. if (existingItems.count) {
  33. rootVC.navigationItem.rightBarButtonItems = [@[done] arrayByAddingObjectsFromArray:existingItems];
  34. } else {
  35. rootVC.navigationItem.rightBarButtonItem = done;
  36. }
  37. return instance;
  38. }
  39. - (void)viewDidLoad {
  40. [super viewDidLoad];
  41. self.waitingToAddTab = YES;
  42. }
  43. - (void)viewDidAppear:(BOOL)animated {
  44. [super viewDidAppear:animated];
  45. if (self.waitingToAddTab) {
  46. // Only add new tab if we're presented properly
  47. if ([self.presentingViewController isKindOfClass:[FLEXExplorerViewController class]]) {
  48. // New navigation controllers always add themselves as new tabs,
  49. // tabs are closed by FLEXExplorerViewController
  50. [FLEXTabList.sharedList addTab:self];
  51. self.waitingToAddTab = NO;
  52. }
  53. }
  54. }
  55. - (void)dismissAnimated {
  56. // TODO tabs not closed on swipe down gesture
  57. [FLEXTabList.sharedList closeTab:self];
  58. [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  59. }
  60. - (void)_gestureRecognizedInteractiveHide:(UIPanGestureRecognizer *)sender {
  61. if (sender.state == UIGestureRecognizerStateRecognized) {
  62. BOOL show = self.topViewController.toolbarItems.count;
  63. CGFloat yTranslation = [sender translationInView:self.view].y;
  64. CGFloat yVelocity = [sender velocityInView:self.view].y;
  65. if (yVelocity > 2000) {
  66. [self setToolbarHidden:YES animated:YES];
  67. } else if (show && yTranslation > 20 && yVelocity > 250) {
  68. [self setToolbarHidden:NO animated:YES];
  69. } else if (yTranslation < -20) {
  70. [self setToolbarHidden:YES animated:YES];
  71. }
  72. }
  73. }
  74. @end