react-native-navigation的迁移库

RCCNavigationController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. [self pushViewController:viewController animated:animated];
  116. return;
  117. }
  118. // pop
  119. if ([performAction isEqualToString:@"pop"])
  120. {
  121. [self popViewControllerAnimated:animated];
  122. return;
  123. }
  124. // popToRoot
  125. if ([performAction isEqualToString:@"popToRoot"])
  126. {
  127. [self popToRootViewControllerAnimated:animated];
  128. return;
  129. }
  130. // resetTo
  131. if ([performAction isEqualToString:@"resetTo"])
  132. {
  133. NSString *component = actionParams[@"component"];
  134. if (!component) return;
  135. NSDictionary *passProps = actionParams[@"passProps"];
  136. NSDictionary *navigatorStyle = actionParams[@"style"];
  137. RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:nil bridge:bridge];
  138. [self processTitleView:viewController
  139. props:actionParams
  140. style:navigatorStyle];
  141. NSArray *leftButtons = actionParams[@"leftButtons"];
  142. if (leftButtons)
  143. {
  144. [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
  145. }
  146. NSArray *rightButtons = actionParams[@"rightButtons"];
  147. if (rightButtons)
  148. {
  149. [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
  150. }
  151. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  152. [self setViewControllers:@[viewController] animated:animated];
  153. return;
  154. }
  155. // setButtons
  156. if ([performAction isEqualToString:@"setButtons"])
  157. {
  158. NSArray *buttons = actionParams[@"buttons"];
  159. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  160. NSString *side = actionParams[@"side"] ? actionParams[@"side"] : @"left";
  161. [self setButtons:buttons viewController:self.topViewController side:side animated:animated];
  162. return;
  163. }
  164. // setTitle
  165. if ([performAction isEqualToString:@"setTitle"] || [performAction isEqualToString:@"setTitleImage"])
  166. {
  167. NSDictionary *navigatorStyle = actionParams[@"style"];
  168. [self processTitleView:self.topViewController
  169. props:actionParams
  170. style:navigatorStyle];
  171. return;
  172. }
  173. // toggleNavBar
  174. if ([performAction isEqualToString:@"setHidden"]) {
  175. NSNumber *animated = actionParams[@"animated"];
  176. BOOL animatedBool = animated ? [animated boolValue] : YES;
  177. NSNumber *setHidden = actionParams[@"hidden"];
  178. BOOL isHiddenBool = setHidden ? [setHidden boolValue] : NO;
  179. RCCViewController *topViewController = ((RCCViewController*)self.topViewController);
  180. topViewController.navigatorStyle[@"navBarHidden"] = setHidden;
  181. [topViewController setNavBarVisibilityChange:animatedBool];
  182. }
  183. // setStyle
  184. if ([performAction isEqualToString:@"setStyle"])
  185. {
  186. NSDictionary *navigatorStyle = actionParams;
  187. // merge the navigatorStyle of our parent
  188. if ([self.topViewController isKindOfClass:[RCCViewController class]])
  189. {
  190. RCCViewController *parent = (RCCViewController*)self.topViewController;
  191. NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:parent.navigatorStyle];
  192. // there are a few styles that we don't want to remember from our parent (they should be local)
  193. [mergedStyle setValuesForKeysWithDictionary:navigatorStyle];
  194. navigatorStyle = mergedStyle;
  195. parent.navigatorStyle = navigatorStyle;
  196. [parent setStyleOnInit];
  197. [parent updateStyle];
  198. }
  199. }
  200. }
  201. -(void)onButtonPress:(UIBarButtonItem*)barButtonItem
  202. {
  203. NSString *callbackId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY);
  204. if (!callbackId) return;
  205. NSString *buttonId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID);
  206. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:callbackId body:@
  207. {
  208. @"type": @"NavBarButtonPress",
  209. @"id": buttonId ? buttonId : [NSNull null]
  210. }];
  211. }
  212. -(void)setButtons:(NSArray*)buttons viewController:(UIViewController*)viewController side:(NSString*)side animated:(BOOL)animated
  213. {
  214. NSMutableArray *barButtonItems = [NSMutableArray new];
  215. for (NSDictionary *button in buttons)
  216. {
  217. NSString *title = button[@"title"];
  218. UIImage *iconImage = nil;
  219. id icon = button[@"icon"];
  220. if (icon) iconImage = [RCTConvert UIImage:icon];
  221. UIBarButtonItem *barButtonItem;
  222. if (iconImage)
  223. {
  224. barButtonItem = [[UIBarButtonItem alloc] initWithImage:iconImage style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
  225. }
  226. else if (title)
  227. {
  228. barButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
  229. NSMutableDictionary *buttonTextAttributes = [RCTHelpers textAttributesFromDictionary:button withPrefix:@"button"];
  230. if (buttonTextAttributes.allKeys.count > 0) {
  231. [barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
  232. }
  233. }
  234. else continue;
  235. objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY, button[@"onPress"], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  236. [barButtonItems addObject:barButtonItem];
  237. NSString *buttonId = button[@"id"];
  238. if (buttonId)
  239. {
  240. objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID, buttonId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  241. }
  242. NSNumber *disabled = button[@"disabled"];
  243. BOOL disabledBool = disabled ? [disabled boolValue] : NO;
  244. if (disabledBool) {
  245. [barButtonItem setEnabled:NO];
  246. }
  247. NSNumber *disableIconTintString = button[@"disableIconTint"];
  248. BOOL disableIconTint = disableIconTintString ? [disableIconTintString boolValue] : NO;
  249. if (disableIconTint) {
  250. [barButtonItem setImage:[barButtonItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  251. }
  252. NSString *testID = button[@"testID"];
  253. if (testID)
  254. {
  255. barButtonItem.accessibilityIdentifier = testID;
  256. }
  257. }
  258. if ([side isEqualToString:@"left"])
  259. {
  260. [viewController.navigationItem setLeftBarButtonItems:barButtonItems animated:animated];
  261. }
  262. if ([side isEqualToString:@"right"])
  263. {
  264. [viewController.navigationItem setRightBarButtonItems:barButtonItems animated:animated];
  265. }
  266. }
  267. -(void)processTitleView:(UIViewController*)viewController
  268. props:(NSDictionary*)props
  269. style:(NSDictionary*)style
  270. {
  271. BOOL isSetSubtitleBool = props[@"isSetSubtitle"] ? [props[@"isSetSubtitle"] boolValue] : NO;
  272. RCCTitleViewHelper *titleViewHelper = [[RCCTitleViewHelper alloc] init:viewController
  273. navigationController:self
  274. title:props[@"title"]
  275. subtitle:props[@"subtitle"]
  276. titleImageData:props[@"titleImage"]
  277. isSetSubtitle:isSetSubtitleBool];
  278. [titleViewHelper setup:style];
  279. }
  280. - (UIStatusBarStyle)preferredStatusBarStyle {
  281. return [self.topViewController preferredStatusBarStyle];
  282. }
  283. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
  284. {
  285. if(_transitioning)
  286. {
  287. NSDictionary *pushDetails =@{ @"viewController": viewController, @"animated": @(animated) };
  288. [_queuedViewControllers addObject:pushDetails];
  289. return;
  290. }
  291. _transitioning = YES;
  292. [super pushViewController:viewController animated:animated];
  293. }
  294. #pragma mark - UINavigationControllerDelegate
  295. -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  296. [viewController setNeedsStatusBarAppearanceUpdate];
  297. }
  298. - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
  299. {
  300. dispatch_async(dispatch_get_main_queue(), ^{
  301. _transitioning = NO;
  302. if ([_queuedViewControllers count] > 0) {
  303. NSDictionary *toPushDetails = [_queuedViewControllers firstObject];
  304. [_queuedViewControllers removeObjectAtIndex:0];
  305. [self pushViewController:toPushDetails[@"viewController"] animated:[toPushDetails[@"animated"] boolValue]];
  306. }
  307. });
  308. }
  309. @end