FLEXNavigationController.m 6.5 KB

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