react-native-navigation的迁移库

RCCNavigationController.m 17KB

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