react-native-navigation的迁移库

RCCNavigationController.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #import "RCCNavigationController.h"
  2. #import "RCCViewController.h"
  3. #import "RCCManager.h"
  4. #import <React/RCTEventDispatcher.h>
  5. #import <React/RCTConvert.h>
  6. #import <objc/runtime.h>
  7. #import "RCCTitleViewHelper.h"
  8. #import "UIViewController+Rotation.h"
  9. #import "RCTHelpers.h"
  10. @implementation RCCNavigationController
  11. {
  12. BOOL _transitioning;
  13. NSMutableArray *_queuedViewControllers;
  14. }
  15. NSString const *CALLBACK_ASSOCIATED_KEY = @"RCCNavigationController.CALLBACK_ASSOCIATED_KEY";
  16. NSString const *CALLBACK_ASSOCIATED_ID = @"RCCNavigationController.CALLBACK_ASSOCIATED_ID";
  17. -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
  18. return [self supportedControllerOrientations];
  19. }
  20. - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary*)globalProps bridge:(RCTBridge *)bridge
  21. {
  22. _queuedViewControllers = [NSMutableArray new];
  23. NSString *component = props[@"component"];
  24. if (!component) return nil;
  25. NSDictionary *passProps = props[@"passProps"];
  26. NSDictionary *navigatorStyle = props[@"style"];
  27. RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:globalProps bridge:bridge];
  28. if (!viewController) return nil;
  29. NSArray *leftButtons = props[@"leftButtons"];
  30. if (leftButtons)
  31. {
  32. [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
  33. }
  34. NSArray *rightButtons = props[@"rightButtons"];
  35. if (rightButtons)
  36. {
  37. [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
  38. }
  39. self = [super initWithRootViewController:viewController];
  40. if (!self) return nil;
  41. self.delegate = self;
  42. self.navigationBar.translucent = NO; // default
  43. [self processTitleView:viewController
  44. props:props
  45. style:navigatorStyle];
  46. [self setRotation:props];
  47. return self;
  48. }
  49. - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge
  50. {
  51. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  52. // push
  53. if ([performAction isEqualToString:@"push"])
  54. {
  55. NSString *component = actionParams[@"component"];
  56. if (!component) return;
  57. NSDictionary *passProps = actionParams[@"passProps"];
  58. NSDictionary *navigatorStyle = actionParams[@"style"];
  59. // merge the navigatorStyle of our parent
  60. if ([self.topViewController isKindOfClass:[RCCViewController class]])
  61. {
  62. RCCViewController *parent = (RCCViewController*)self.topViewController;
  63. NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:parent.navigatorStyle];
  64. // there are a few styles that we don't want to remember from our parent (they should be local)
  65. [mergedStyle removeObjectForKey:@"navBarHidden"];
  66. [mergedStyle removeObjectForKey:@"statusBarHidden"];
  67. [mergedStyle removeObjectForKey:@"navBarHideOnScroll"];
  68. [mergedStyle removeObjectForKey:@"drawUnderNavBar"];
  69. [mergedStyle removeObjectForKey:@"drawUnderTabBar"];
  70. [mergedStyle removeObjectForKey:@"statusBarBlur"];
  71. [mergedStyle removeObjectForKey:@"navBarBlur"];
  72. [mergedStyle removeObjectForKey:@"navBarTranslucent"];
  73. [mergedStyle removeObjectForKey:@"statusBarHideWithNavBar"];
  74. [mergedStyle removeObjectForKey:@"autoAdjustScrollViewInsets"];
  75. [mergedStyle removeObjectForKey:@"statusBarTextColorSchemeSingleScreen"];
  76. [mergedStyle removeObjectForKey:@"disabledBackGesture"];
  77. [mergedStyle addEntriesFromDictionary:navigatorStyle];
  78. navigatorStyle = mergedStyle;
  79. }
  80. RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:nil bridge:bridge];
  81. [self processTitleView:viewController
  82. props:actionParams
  83. style:navigatorStyle];
  84. NSString *backButtonTitle = actionParams[@"backButtonTitle"];
  85. if (backButtonTitle)
  86. {
  87. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:backButtonTitle
  88. style:UIBarButtonItemStylePlain
  89. target:nil
  90. action:nil];
  91. self.topViewController.navigationItem.backBarButtonItem = backItem;
  92. }
  93. else
  94. {
  95. self.topViewController.navigationItem.backBarButtonItem = nil;
  96. }
  97. NSNumber *backButtonHidden = actionParams[@"backButtonHidden"];
  98. BOOL backButtonHiddenBool = backButtonHidden ? [backButtonHidden boolValue] : NO;
  99. if (backButtonHiddenBool)
  100. {
  101. viewController.navigationItem.hidesBackButton = YES;
  102. }
  103. NSArray *leftButtons = actionParams[@"leftButtons"];
  104. if (leftButtons)
  105. {
  106. [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
  107. }
  108. NSArray *rightButtons = actionParams[@"rightButtons"];
  109. if (rightButtons)
  110. {
  111. [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
  112. }
  113. [self pushViewController:viewController animated:animated];
  114. return;
  115. }
  116. // pop
  117. if ([performAction isEqualToString:@"pop"])
  118. {
  119. [self popViewControllerAnimated:animated];
  120. return;
  121. }
  122. // popToRoot
  123. if ([performAction isEqualToString:@"popToRoot"])
  124. {
  125. [self popToRootViewControllerAnimated:animated];
  126. return;
  127. }
  128. // resetTo
  129. if ([performAction isEqualToString:@"resetTo"])
  130. {
  131. NSString *component = actionParams[@"component"];
  132. if (!component) return;
  133. NSDictionary *passProps = actionParams[@"passProps"];
  134. NSDictionary *navigatorStyle = actionParams[@"style"];
  135. RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:nil bridge:bridge];
  136. [self processTitleView:viewController
  137. props:actionParams
  138. style:navigatorStyle];
  139. NSArray *leftButtons = actionParams[@"leftButtons"];
  140. if (leftButtons)
  141. {
  142. [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
  143. }
  144. NSArray *rightButtons = actionParams[@"rightButtons"];
  145. if (rightButtons)
  146. {
  147. [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
  148. }
  149. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  150. [self setViewControllers:@[viewController] animated:animated];
  151. return;
  152. }
  153. // setButtons
  154. if ([performAction isEqualToString:@"setButtons"])
  155. {
  156. NSArray *buttons = actionParams[@"buttons"];
  157. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  158. NSString *side = actionParams[@"side"] ? actionParams[@"side"] : @"left";
  159. [self setButtons:buttons viewController:self.topViewController side:side animated:animated];
  160. return;
  161. }
  162. // setTitle
  163. if ([performAction isEqualToString:@"setTitle"] || [performAction isEqualToString:@"setTitleImage"])
  164. {
  165. NSDictionary *navigatorStyle = actionParams[@"style"];
  166. [self processTitleView:self.topViewController
  167. props:actionParams
  168. style:navigatorStyle];
  169. return;
  170. }
  171. // toggleNavBar
  172. if ([performAction isEqualToString:@"setHidden"]) {
  173. NSNumber *animated = actionParams[@"animated"];
  174. BOOL animatedBool = animated ? [animated boolValue] : YES;
  175. NSNumber *setHidden = actionParams[@"hidden"];
  176. BOOL isHiddenBool = setHidden ? [setHidden boolValue] : NO;
  177. RCCViewController *topViewController = ((RCCViewController*)self.topViewController);
  178. topViewController.navigatorStyle[@"navBarHidden"] = setHidden;
  179. [topViewController setNavBarVisibilityChange:animatedBool];
  180. }
  181. // setStyle
  182. if ([performAction isEqualToString:@"setStyle"])
  183. {
  184. NSDictionary *navigatorStyle = actionParams;
  185. // merge the navigatorStyle of our parent
  186. if ([self.topViewController isKindOfClass:[RCCViewController class]])
  187. {
  188. RCCViewController *parent = (RCCViewController*)self.topViewController;
  189. NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:parent.navigatorStyle];
  190. // there are a few styles that we don't want to remember from our parent (they should be local)
  191. [mergedStyle setValuesForKeysWithDictionary:navigatorStyle];
  192. navigatorStyle = mergedStyle;
  193. parent.navigatorStyle = navigatorStyle;
  194. [parent setStyleOnInit];
  195. [parent updateStyle];
  196. }
  197. }
  198. }
  199. -(void)onButtonPress:(UIBarButtonItem*)barButtonItem
  200. {
  201. NSString *callbackId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY);
  202. if (!callbackId) return;
  203. NSString *buttonId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID);
  204. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:callbackId body:@
  205. {
  206. @"type": @"NavBarButtonPress",
  207. @"id": buttonId ? buttonId : [NSNull null]
  208. }];
  209. }
  210. -(void)setButtons:(NSArray*)buttons viewController:(UIViewController*)viewController side:(NSString*)side animated:(BOOL)animated
  211. {
  212. NSMutableArray *barButtonItems = [NSMutableArray new];
  213. for (NSDictionary *button in buttons)
  214. {
  215. NSString *title = button[@"title"];
  216. UIImage *iconImage = nil;
  217. id icon = button[@"icon"];
  218. if (icon) iconImage = [RCTConvert UIImage:icon];
  219. UIBarButtonItem *barButtonItem;
  220. if (iconImage)
  221. {
  222. barButtonItem = [[UIBarButtonItem alloc] initWithImage:iconImage style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
  223. }
  224. else if (title)
  225. {
  226. barButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
  227. NSMutableDictionary *buttonTextAttributes = [RCTHelpers textAttributesFromDictionary:button withPrefix:@"button"];
  228. if (buttonTextAttributes.allKeys.count > 0) {
  229. [barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
  230. }
  231. }
  232. else continue;
  233. objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY, button[@"onPress"], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  234. [barButtonItems addObject:barButtonItem];
  235. NSString *buttonId = button[@"id"];
  236. if (buttonId)
  237. {
  238. objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID, buttonId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  239. }
  240. NSNumber *disabled = button[@"disabled"];
  241. BOOL disabledBool = disabled ? [disabled boolValue] : NO;
  242. if (disabledBool) {
  243. [barButtonItem setEnabled:NO];
  244. }
  245. NSNumber *disableIconTintString = button[@"disableIconTint"];
  246. BOOL disableIconTint = disableIconTintString ? [disableIconTintString boolValue] : NO;
  247. if (disableIconTint) {
  248. [barButtonItem setImage:[barButtonItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  249. }
  250. NSString *testID = button[@"testID"];
  251. if (testID)
  252. {
  253. barButtonItem.accessibilityIdentifier = testID;
  254. }
  255. }
  256. if ([side isEqualToString:@"left"])
  257. {
  258. [viewController.navigationItem setLeftBarButtonItems:barButtonItems animated:animated];
  259. }
  260. if ([side isEqualToString:@"right"])
  261. {
  262. [viewController.navigationItem setRightBarButtonItems:barButtonItems animated:animated];
  263. }
  264. }
  265. -(void)processTitleView:(UIViewController*)viewController
  266. props:(NSDictionary*)props
  267. style:(NSDictionary*)style
  268. {
  269. RCCTitleViewHelper *titleViewHelper = [[RCCTitleViewHelper alloc] init:viewController
  270. navigationController:self
  271. title:props[@"title"]
  272. subtitle:props[@"subtitle"]
  273. titleImageData:props[@"titleImage"]];
  274. [titleViewHelper setup:style];
  275. }
  276. - (UIStatusBarStyle)preferredStatusBarStyle {
  277. return [self.topViewController preferredStatusBarStyle];
  278. }
  279. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
  280. {
  281. if(_transitioning)
  282. {
  283. NSDictionary *pushDetails =@{ @"viewController": viewController, @"animated": @(animated) };
  284. [_queuedViewControllers addObject:pushDetails];
  285. return;
  286. }
  287. _transitioning = YES;
  288. [super pushViewController:viewController animated:animated];
  289. }
  290. #pragma mark - UINavigationControllerDelegate
  291. -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  292. [viewController setNeedsStatusBarAppearanceUpdate];
  293. }
  294. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
  295. {
  296. dispatch_async(dispatch_get_main_queue(), ^{
  297. _transitioning = NO;
  298. if ([_queuedViewControllers count] > 0) {
  299. NSDictionary *toPushDetails = [_queuedViewControllers firstObject];
  300. [_queuedViewControllers removeObjectAtIndex:0];
  301. [self pushViewController:toPushDetails[@"viewController"] animated:[toPushDetails[@"animated"] boolValue]];
  302. }
  303. });
  304. }
  305. @end