react-native-navigation的迁移库

RCCNavigationController.m 15KB

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