react-native-navigation的迁移库

RCCNavigationController.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. NSMutableDictionary *passProps = [actionParams[@"passProps"] mutableCopy];
  58. passProps[GLOBAL_SCREEN_ACTION_COMMAND_TYPE] = COMMAND_TYPE_PUSH;
  59. passProps[GLOBAL_SCREEN_ACTION_TIMESTAMP] = actionParams[GLOBAL_SCREEN_ACTION_TIMESTAMP];
  60. NSDictionary *navigatorStyle = actionParams[@"style"];
  61. // merge the navigatorStyle of our parent
  62. if ([self.topViewController isKindOfClass:[RCCViewController class]])
  63. {
  64. RCCViewController *parent = (RCCViewController*)self.topViewController;
  65. NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:parent.navigatorStyle];
  66. // there are a few styles that we don't want to remember from our parent (they should be local)
  67. [mergedStyle removeObjectForKey:@"navBarHidden"];
  68. [mergedStyle removeObjectForKey:@"statusBarHidden"];
  69. [mergedStyle removeObjectForKey:@"navBarHideOnScroll"];
  70. [mergedStyle removeObjectForKey:@"drawUnderNavBar"];
  71. [mergedStyle removeObjectForKey:@"drawUnderTabBar"];
  72. [mergedStyle removeObjectForKey:@"statusBarBlur"];
  73. [mergedStyle removeObjectForKey:@"navBarBlur"];
  74. [mergedStyle removeObjectForKey:@"navBarTranslucent"];
  75. [mergedStyle removeObjectForKey:@"statusBarHideWithNavBar"];
  76. [mergedStyle removeObjectForKey:@"autoAdjustScrollViewInsets"];
  77. [mergedStyle removeObjectForKey:@"statusBarTextColorSchemeSingleScreen"];
  78. [mergedStyle removeObjectForKey:@"disabledBackGesture"];
  79. [mergedStyle removeObjectForKey:@"navBarCustomView"];
  80. [mergedStyle removeObjectForKey:@"navBarComponentAlignment"];
  81. [mergedStyle addEntriesFromDictionary:navigatorStyle];
  82. navigatorStyle = mergedStyle;
  83. }
  84. RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:nil bridge:bridge];
  85. [self processTitleView:viewController
  86. props:actionParams
  87. style:navigatorStyle];
  88. NSString *backButtonTitle = actionParams[@"backButtonTitle"];
  89. if (backButtonTitle)
  90. {
  91. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:backButtonTitle
  92. style:UIBarButtonItemStylePlain
  93. target:nil
  94. action:nil];
  95. self.topViewController.navigationItem.backBarButtonItem = backItem;
  96. }
  97. else
  98. {
  99. self.topViewController.navigationItem.backBarButtonItem = nil;
  100. }
  101. NSNumber *backButtonHidden = actionParams[@"backButtonHidden"];
  102. BOOL backButtonHiddenBool = backButtonHidden ? [backButtonHidden boolValue] : NO;
  103. if (backButtonHiddenBool)
  104. {
  105. viewController.navigationItem.hidesBackButton = YES;
  106. }
  107. NSArray *leftButtons = actionParams[@"leftButtons"];
  108. if (leftButtons)
  109. {
  110. [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
  111. }
  112. NSArray *rightButtons = actionParams[@"rightButtons"];
  113. if (rightButtons)
  114. {
  115. [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
  116. }
  117. NSString *animationType = actionParams[@"animationType"];
  118. if ([animationType isEqualToString:@"fade"])
  119. {
  120. CATransition *transition = [CATransition animation];
  121. transition.duration = 0.25;
  122. transition.type = kCATransitionFade;
  123. [self.view.layer addAnimation:transition forKey:kCATransition];
  124. [self pushViewController:viewController animated:NO];
  125. }
  126. else
  127. {
  128. [self pushViewController:viewController animated:animated];
  129. }
  130. return;
  131. }
  132. // pop
  133. if ([performAction isEqualToString:@"pop"])
  134. {
  135. NSString *animationType = actionParams[@"animationType"];
  136. if ([animationType isEqualToString:@"fade"])
  137. {
  138. CATransition *transition = [CATransition animation];
  139. transition.duration = 0.25;
  140. transition.type = kCATransitionFade;
  141. [self.view.layer addAnimation:transition forKey:kCATransition];
  142. [self popViewControllerAnimated:NO];
  143. }
  144. else
  145. {
  146. [self popViewControllerAnimated:animated];
  147. }
  148. return;
  149. }
  150. // popToRoot
  151. if ([performAction isEqualToString:@"popToRoot"])
  152. {
  153. NSString *animationType = actionParams[@"animationType"];
  154. if ([animationType isEqualToString:@"fade"])
  155. {
  156. CATransition *transition = [CATransition animation];
  157. transition.duration = 0.25;
  158. transition.type = kCATransitionFade;
  159. [self.view.layer addAnimation:transition forKey:kCATransition];
  160. [self popToRootViewControllerAnimated:NO];
  161. }
  162. else
  163. {
  164. [self popToRootViewControllerAnimated:animated];
  165. }
  166. return;
  167. }
  168. // resetTo
  169. if ([performAction isEqualToString:@"resetTo"])
  170. {
  171. NSString *component = actionParams[@"component"];
  172. if (!component) return;
  173. NSMutableDictionary *passProps = [actionParams[@"passProps"] mutableCopy];
  174. passProps[@"commantType"] = @"resetTo";
  175. NSDictionary *navigatorStyle = actionParams[@"style"];
  176. RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:nil bridge:bridge];
  177. [self processTitleView:viewController
  178. props:actionParams
  179. style:navigatorStyle];
  180. NSArray *leftButtons = actionParams[@"leftButtons"];
  181. if (leftButtons)
  182. {
  183. [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
  184. }
  185. NSArray *rightButtons = actionParams[@"rightButtons"];
  186. if (rightButtons)
  187. {
  188. [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
  189. }
  190. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  191. NSString *animationType = actionParams[@"animationType"];
  192. if ([animationType isEqualToString:@"fade"])
  193. {
  194. CATransition *transition = [CATransition animation];
  195. transition.duration = 0.25;
  196. transition.type = kCATransitionFade;
  197. [self.view.layer addAnimation:transition forKey:kCATransition];
  198. [self setViewControllers:@[viewController] animated:NO];
  199. }
  200. else
  201. {
  202. [self setViewControllers:@[viewController] animated:animated];
  203. }
  204. return;
  205. }
  206. // setButtons
  207. if ([performAction isEqualToString:@"setButtons"])
  208. {
  209. NSArray *buttons = actionParams[@"buttons"];
  210. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  211. NSString *side = actionParams[@"side"] ? actionParams[@"side"] : @"left";
  212. [self setButtons:buttons viewController:self.topViewController side:side animated:animated];
  213. return;
  214. }
  215. // setTitle
  216. if ([performAction isEqualToString:@"setTitle"] || [performAction isEqualToString:@"setTitleImage"])
  217. {
  218. NSDictionary *navigatorStyle = actionParams[@"style"];
  219. [self processTitleView:self.topViewController
  220. props:actionParams
  221. style:navigatorStyle];
  222. return;
  223. }
  224. // toggleNavBar
  225. if ([performAction isEqualToString:@"setHidden"]) {
  226. NSNumber *animated = actionParams[@"animated"];
  227. BOOL animatedBool = animated ? [animated boolValue] : YES;
  228. NSNumber *setHidden = actionParams[@"hidden"];
  229. BOOL isHiddenBool = setHidden ? [setHidden boolValue] : NO;
  230. RCCViewController *topViewController = ((RCCViewController*)self.topViewController);
  231. topViewController.navigatorStyle[@"navBarHidden"] = setHidden;
  232. [topViewController setNavBarVisibilityChange:animatedBool];
  233. }
  234. // setStyle
  235. if ([performAction isEqualToString:@"setStyle"])
  236. {
  237. NSDictionary *navigatorStyle = actionParams;
  238. // merge the navigatorStyle of our parent
  239. if ([self.topViewController isKindOfClass:[RCCViewController class]])
  240. {
  241. RCCViewController *parent = (RCCViewController*)self.topViewController;
  242. NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:parent.navigatorStyle];
  243. // there are a few styles that we don't want to remember from our parent (they should be local)
  244. [mergedStyle setValuesForKeysWithDictionary:navigatorStyle];
  245. navigatorStyle = mergedStyle;
  246. parent.navigatorStyle = navigatorStyle;
  247. [parent setStyleOnInit];
  248. [parent updateStyle];
  249. }
  250. }
  251. }
  252. -(void)onButtonPress:(UIBarButtonItem*)barButtonItem
  253. {
  254. NSString *callbackId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY);
  255. if (!callbackId) return;
  256. NSString *buttonId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID);
  257. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:callbackId body:@
  258. {
  259. @"type": @"NavBarButtonPress",
  260. @"id": buttonId ? buttonId : [NSNull null]
  261. }];
  262. }
  263. -(void)setButtons:(NSArray*)buttons viewController:(UIViewController*)viewController side:(NSString*)side animated:(BOOL)animated
  264. {
  265. NSMutableArray *barButtonItems = [NSMutableArray new];
  266. for (NSDictionary *button in buttons)
  267. {
  268. NSString *title = button[@"title"];
  269. UIImage *iconImage = nil;
  270. id icon = button[@"icon"];
  271. if (icon) iconImage = [RCTConvert UIImage:icon];
  272. UIBarButtonItem *barButtonItem;
  273. if (iconImage)
  274. {
  275. barButtonItem = [[UIBarButtonItem alloc] initWithImage:iconImage style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
  276. }
  277. else if (title)
  278. {
  279. barButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
  280. NSMutableDictionary *buttonTextAttributes = [RCTHelpers textAttributesFromDictionary:button withPrefix:@"button"];
  281. if (buttonTextAttributes.allKeys.count > 0) {
  282. [barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
  283. }
  284. }
  285. else continue;
  286. objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY, button[@"onPress"], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  287. [barButtonItems addObject:barButtonItem];
  288. NSString *buttonId = button[@"id"];
  289. if (buttonId)
  290. {
  291. objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID, buttonId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  292. }
  293. NSNumber *disabled = button[@"disabled"];
  294. BOOL disabledBool = disabled ? [disabled boolValue] : NO;
  295. if (disabledBool) {
  296. [barButtonItem setEnabled:NO];
  297. }
  298. NSNumber *disableIconTintString = button[@"disableIconTint"];
  299. BOOL disableIconTint = disableIconTintString ? [disableIconTintString boolValue] : NO;
  300. if (disableIconTint) {
  301. [barButtonItem setImage:[barButtonItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  302. }
  303. NSString *testID = button[@"testID"];
  304. if (testID)
  305. {
  306. barButtonItem.accessibilityIdentifier = testID;
  307. }
  308. }
  309. if ([side isEqualToString:@"left"])
  310. {
  311. [viewController.navigationItem setLeftBarButtonItems:barButtonItems animated:animated];
  312. }
  313. if ([side isEqualToString:@"right"])
  314. {
  315. [viewController.navigationItem setRightBarButtonItems:barButtonItems animated:animated];
  316. }
  317. }
  318. -(void)processTitleView:(UIViewController*)viewController
  319. props:(NSDictionary*)props
  320. style:(NSDictionary*)style
  321. {
  322. BOOL isSetSubtitleBool = props[@"isSetSubtitle"] ? [props[@"isSetSubtitle"] boolValue] : NO;
  323. RCCTitleViewHelper *titleViewHelper = [[RCCTitleViewHelper alloc] init:viewController
  324. navigationController:self
  325. title:props[@"title"]
  326. subtitle:props[@"subtitle"]
  327. titleImageData:props[@"titleImage"]
  328. isSetSubtitle:isSetSubtitleBool];
  329. [titleViewHelper setup:style];
  330. }
  331. - (UIStatusBarStyle)preferredStatusBarStyle {
  332. return [self.topViewController preferredStatusBarStyle];
  333. }
  334. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
  335. {
  336. if(_transitioning)
  337. {
  338. NSDictionary *pushDetails =@{ @"viewController": viewController, @"animated": @(animated) };
  339. [_queuedViewControllers addObject:pushDetails];
  340. return;
  341. }
  342. _transitioning = YES;
  343. [super pushViewController:viewController animated:animated];
  344. }
  345. #pragma mark - UINavigationControllerDelegate
  346. -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  347. [viewController setNeedsStatusBarAppearanceUpdate];
  348. }
  349. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
  350. {
  351. dispatch_async(dispatch_get_main_queue(), ^{
  352. _transitioning = NO;
  353. if ([_queuedViewControllers count] > 0) {
  354. NSDictionary *toPushDetails = [_queuedViewControllers firstObject];
  355. [_queuedViewControllers removeObjectAtIndex:0];
  356. [self pushViewController:toPushDetails[@"viewController"] animated:[toPushDetails[@"animated"] boolValue]];
  357. }
  358. });
  359. }
  360. @end