react-native-navigation的迁移库

RCCNavigationController.m 16KB

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