react-native-navigation的迁移库

RCCNavigationController.m 19KB

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