react-native-navigation的迁移库

RCCNavigationController.m 16KB

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