react-native-navigation的迁移库

RCCNavigationController.m 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. [self processTitleView:viewController
  181. props:actionParams
  182. style:navigatorStyle];
  183. NSArray *leftButtons = actionParams[@"leftButtons"];
  184. if (leftButtons)
  185. {
  186. [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
  187. }
  188. NSArray *rightButtons = actionParams[@"rightButtons"];
  189. if (rightButtons)
  190. {
  191. [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
  192. }
  193. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  194. NSString *animationType = actionParams[@"animationType"];
  195. if ([animationType isEqualToString:@"fade"])
  196. {
  197. CATransition *transition = [CATransition animation];
  198. transition.duration = 0.25;
  199. transition.type = kCATransitionFade;
  200. [self.view.layer addAnimation:transition forKey:kCATransition];
  201. [self setViewControllers:@[viewController] animated:NO];
  202. }
  203. else
  204. {
  205. [self setViewControllers:@[viewController] animated:animated];
  206. }
  207. return;
  208. }
  209. // setButtons
  210. if ([performAction isEqualToString:@"setButtons"])
  211. {
  212. NSArray *buttons = actionParams[@"buttons"];
  213. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  214. NSString *side = actionParams[@"side"] ? actionParams[@"side"] : @"left";
  215. [self setButtons:buttons viewController:self.topViewController side:side animated:animated];
  216. return;
  217. }
  218. // setTitle
  219. if ([performAction isEqualToString:@"setTitle"] || [performAction isEqualToString:@"setTitleImage"])
  220. {
  221. NSDictionary *navigatorStyle = actionParams[@"style"];
  222. [self processTitleView:self.topViewController
  223. props:actionParams
  224. style:navigatorStyle];
  225. return;
  226. }
  227. // toggleNavBar
  228. if ([performAction isEqualToString:@"setHidden"]) {
  229. NSNumber *animated = actionParams[@"animated"];
  230. BOOL animatedBool = animated ? [animated boolValue] : YES;
  231. NSNumber *setHidden = actionParams[@"hidden"];
  232. BOOL isHiddenBool = setHidden ? [setHidden boolValue] : NO;
  233. RCCViewController *topViewController = ((RCCViewController*)self.topViewController);
  234. topViewController.navigatorStyle[@"navBarHidden"] = setHidden;
  235. [topViewController setNavBarVisibilityChange:animatedBool];
  236. }
  237. // setStyle
  238. if ([performAction isEqualToString:@"setStyle"])
  239. {
  240. NSDictionary *navigatorStyle = actionParams;
  241. // merge the navigatorStyle of our parent
  242. if ([self.topViewController isKindOfClass:[RCCViewController class]])
  243. {
  244. RCCViewController *parent = (RCCViewController*)self.topViewController;
  245. NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:parent.navigatorStyle];
  246. // there are a few styles that we don't want to remember from our parent (they should be local)
  247. [mergedStyle setValuesForKeysWithDictionary:navigatorStyle];
  248. navigatorStyle = mergedStyle;
  249. parent.navigatorStyle = navigatorStyle;
  250. [parent setStyleOnInit];
  251. [parent updateStyle];
  252. }
  253. }
  254. }
  255. -(void)onButtonPress:(UIBarButtonItem*)barButtonItem
  256. {
  257. NSString *callbackId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY);
  258. if (!callbackId) return;
  259. NSString *buttonId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID);
  260. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:callbackId body:@
  261. {
  262. @"type": @"NavBarButtonPress",
  263. @"id": buttonId ? buttonId : [NSNull null]
  264. }];
  265. }
  266. -(void)setButtons:(NSArray*)buttons viewController:(UIViewController*)viewController side:(NSString*)side animated:(BOOL)animated
  267. {
  268. NSMutableArray *barButtonItems = [NSMutableArray new];
  269. for (NSDictionary *button in buttons)
  270. {
  271. NSString *title = button[@"title"];
  272. UIImage *iconImage = nil;
  273. id icon = button[@"icon"];
  274. if (icon) iconImage = [RCTConvert UIImage:icon];
  275. UIBarButtonItem *barButtonItem;
  276. if (iconImage)
  277. {
  278. barButtonItem = [[UIBarButtonItem alloc] initWithImage:iconImage style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
  279. }
  280. else if (title)
  281. {
  282. barButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
  283. NSMutableDictionary *buttonTextAttributes = [RCTHelpers textAttributesFromDictionary:button withPrefix:@"button"];
  284. if (buttonTextAttributes.allKeys.count > 0) {
  285. [barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
  286. }
  287. }
  288. else continue;
  289. objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY, button[@"onPress"], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  290. [barButtonItems addObject:barButtonItem];
  291. NSString *buttonId = button[@"id"];
  292. if (buttonId)
  293. {
  294. objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID, buttonId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  295. }
  296. NSNumber *disabled = button[@"disabled"];
  297. BOOL disabledBool = disabled ? [disabled boolValue] : NO;
  298. if (disabledBool) {
  299. [barButtonItem setEnabled:NO];
  300. }
  301. NSNumber *disableIconTintString = button[@"disableIconTint"];
  302. BOOL disableIconTint = disableIconTintString ? [disableIconTintString boolValue] : NO;
  303. if (disableIconTint) {
  304. [barButtonItem setImage:[barButtonItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  305. }
  306. NSString *testID = button[@"testID"];
  307. if (testID)
  308. {
  309. barButtonItem.accessibilityIdentifier = testID;
  310. }
  311. }
  312. if ([side isEqualToString:@"left"])
  313. {
  314. [viewController.navigationItem setLeftBarButtonItems:barButtonItems animated:animated];
  315. }
  316. if ([side isEqualToString:@"right"])
  317. {
  318. [viewController.navigationItem setRightBarButtonItems:barButtonItems animated:animated];
  319. }
  320. }
  321. -(void)processTitleView:(UIViewController*)viewController
  322. props:(NSDictionary*)props
  323. style:(NSDictionary*)style
  324. {
  325. BOOL isSetSubtitleBool = props[@"isSetSubtitle"] ? [props[@"isSetSubtitle"] boolValue] : NO;
  326. RCCTitleViewHelper *titleViewHelper = [[RCCTitleViewHelper alloc] init:viewController
  327. navigationController:self
  328. title:props[@"title"]
  329. subtitle:props[@"subtitle"]
  330. titleImageData:props[@"titleImage"]
  331. isSetSubtitle:isSetSubtitleBool];
  332. [titleViewHelper setup:style];
  333. }
  334. - (UIStatusBarStyle)preferredStatusBarStyle {
  335. return [self.topViewController preferredStatusBarStyle];
  336. }
  337. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
  338. {
  339. if(_transitioning)
  340. {
  341. NSDictionary *pushDetails =@{ @"viewController": viewController, @"animated": @(animated) };
  342. [_queuedViewControllers addObject:pushDetails];
  343. return;
  344. }
  345. _transitioning = YES;
  346. [super pushViewController:viewController animated:animated];
  347. }
  348. #pragma mark - UINavigationControllerDelegate
  349. -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  350. [viewController setNeedsStatusBarAppearanceUpdate];
  351. }
  352. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
  353. {
  354. dispatch_async(dispatch_get_main_queue(), ^{
  355. _transitioning = NO;
  356. if ([_queuedViewControllers count] > 0) {
  357. NSDictionary *toPushDetails = [_queuedViewControllers firstObject];
  358. [_queuedViewControllers removeObjectAtIndex:0];
  359. [self pushViewController:toPushDetails[@"viewController"] animated:[toPushDetails[@"animated"] boolValue]];
  360. }
  361. });
  362. }
  363. @end