FLEXNavigationController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // FLEXNavigationController.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 1/30/20.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXNavigationController.h"
  9. #import "FLEXExplorerViewController.h"
  10. #import "FLEXTabList.h"
  11. #import "FLEXColor.h"
  12. @interface UINavigationController (Private) <UIGestureRecognizerDelegate>
  13. - (void)_gestureRecognizedInteractiveHide:(UIGestureRecognizer *)sender;
  14. @end
  15. @interface UIPanGestureRecognizer (Private)
  16. - (void)_setDelegate:(id)delegate;
  17. @end
  18. @interface FLEXNavigationController ()
  19. @property (nonatomic, readonly) BOOL toolbarWasHidden;
  20. @property (nonatomic) BOOL waitingToAddTab;
  21. @property (nonatomic) BOOL didSetupPendingDismissButtons;
  22. @property (nonatomic) UISwipeGestureRecognizer *navigationBarSwipeGesture;
  23. @end
  24. @implementation FLEXNavigationController
  25. + (instancetype)withRootViewController:(UIViewController *)rootVC {
  26. return [[self alloc] initWithRootViewController:rootVC];
  27. }
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. self.waitingToAddTab = YES;
  31. // Add gesture to reveal toolbar if hidden
  32. self.navigationBar.userInteractionEnabled = YES;
  33. [self.navigationBar addGestureRecognizer:[[UITapGestureRecognizer alloc]
  34. initWithTarget:self action:@selector(handleNavigationBarTap:)
  35. ]];
  36. // Add gesture to dismiss if not presented with a sheet style
  37. if (@available(iOS 13, *)) {
  38. switch (self.modalPresentationStyle) {
  39. case UIModalPresentationAutomatic:
  40. #if !TARGET_OS_TV
  41. case UIModalPresentationPageSheet:
  42. case UIModalPresentationFormSheet:
  43. #endif
  44. break;
  45. default:
  46. [self addNavigationBarSwipeGesture];
  47. break;
  48. }
  49. } else {
  50. [self addNavigationBarSwipeGesture];
  51. }
  52. }
  53. - (void)viewWillAppear:(BOOL)animated {
  54. [super viewWillAppear:animated];
  55. if (self.beingPresented && !self.didSetupPendingDismissButtons) {
  56. for (UIViewController *vc in self.viewControllers) {
  57. [self addNavigationBarItemsToViewController:vc.navigationItem];
  58. }
  59. self.didSetupPendingDismissButtons = YES;
  60. }
  61. }
  62. - (void)viewDidAppear:(BOOL)animated {
  63. [super viewDidAppear:animated];
  64. if (self.waitingToAddTab) {
  65. // Only add new tab if we're presented properly
  66. if ([self.presentingViewController isKindOfClass:[FLEXExplorerViewController class]]) {
  67. // New navigation controllers always add themselves as new tabs,
  68. // tabs are closed by FLEXExplorerViewController
  69. [FLEXTabList.sharedList addTab:self];
  70. self.waitingToAddTab = NO;
  71. }
  72. }
  73. //the timing is janky here but its better than nothing for now.
  74. #if TARGET_OS_TV
  75. if ([[self view] darkMode]){
  76. self.view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.8];
  77. } else {
  78. self.view.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
  79. }
  80. #endif
  81. }
  82. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
  83. [super pushViewController:viewController animated:animated];
  84. [self addNavigationBarItemsToViewController:viewController.navigationItem];
  85. }
  86. - (void)dismissAnimated {
  87. // Tabs are only closed if the done button is pressed; this
  88. // allows you to leave a tab open by dragging down to dismiss
  89. [FLEXTabList.sharedList closeTab:self];
  90. [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  91. }
  92. - (void)addNavigationBarItemsToViewController:(UINavigationItem *)navigationItem {
  93. if (!self.presentingViewController) {
  94. return;
  95. }
  96. // Check if a done item already exists
  97. for (UIBarButtonItem *item in navigationItem.rightBarButtonItems) {
  98. if (item.style == UIBarButtonItemStyleDone) {
  99. return;
  100. }
  101. }
  102. // Give root view controllers a Done button if it does not already have one
  103. UIBarButtonItem *done = [[UIBarButtonItem alloc]
  104. initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  105. target:self
  106. action:@selector(dismissAnimated)
  107. ];
  108. // Prepend the button if other buttons exist already
  109. NSArray *existingItems = navigationItem.rightBarButtonItems;
  110. if (existingItems.count) {
  111. navigationItem.rightBarButtonItems = [@[done] arrayByAddingObjectsFromArray:existingItems];
  112. } else {
  113. navigationItem.rightBarButtonItem = done;
  114. }
  115. // Keeps us from calling this method again on
  116. // the same view controllers in -viewWillAppear:
  117. self.didSetupPendingDismissButtons = YES;
  118. }
  119. - (void)addNavigationBarSwipeGesture {
  120. UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]
  121. initWithTarget:self action:@selector(handleNavigationBarSwipe:)
  122. ];
  123. swipe.direction = UISwipeGestureRecognizerDirectionDown;
  124. swipe.delegate = self;
  125. self.navigationBarSwipeGesture = swipe;
  126. [self.navigationBar addGestureRecognizer:swipe];
  127. }
  128. - (void)handleNavigationBarSwipe:(UISwipeGestureRecognizer *)sender {
  129. if (sender.state == UIGestureRecognizerStateRecognized) {
  130. [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
  131. }
  132. }
  133. - (void)handleNavigationBarTap:(UIGestureRecognizer *)sender {
  134. if (sender.state == UIGestureRecognizerStateRecognized) {
  135. #if !TARGET_OS_TV
  136. if (self.toolbarHidden) {
  137. [self setToolbarHidden:NO animated:YES];
  138. }
  139. #endif
  140. }
  141. }
  142. #if !TARGET_OS_TV
  143. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)g1 shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)g2 {
  144. if (g1 == self.navigationBarSwipeGesture && g2 == self.barHideOnSwipeGestureRecognizer) {
  145. return YES;
  146. }
  147. return NO;
  148. }
  149. #endif
  150. - (void)_gestureRecognizedInteractiveHide:(UIPanGestureRecognizer *)sender {
  151. #if !TARGET_OS_TV
  152. if (sender.state == UIGestureRecognizerStateRecognized) {
  153. BOOL show = self.topViewController.toolbarItems.count;
  154. CGFloat yTranslation = [sender translationInView:self.view].y;
  155. CGFloat yVelocity = [sender velocityInView:self.view].y;
  156. if (yVelocity > 2000) {
  157. [self setToolbarHidden:YES animated:YES];
  158. } else if (show && yTranslation > 20 && yVelocity > 250) {
  159. [self setToolbarHidden:NO animated:YES];
  160. } else if (yTranslation < -20) {
  161. [self setToolbarHidden:YES animated:YES];
  162. }
  163. }
  164. #endif
  165. }
  166. @end