react-native-navigation的迁移库

RCCNavigationController.m 16KB

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