react-native-navigation的迁移库

RCCNavigationController.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. @implementation RCCNavigationController
  10. NSString const *CALLBACK_ASSOCIATED_KEY = @"RCCNavigationController.CALLBACK_ASSOCIATED_KEY";
  11. NSString const *CALLBACK_ASSOCIATED_ID = @"RCCNavigationController.CALLBACK_ASSOCIATED_ID";
  12. -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
  13. return [self supportedControllerOrientations];
  14. }
  15. - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary*)globalProps bridge:(RCTBridge *)bridge
  16. {
  17. NSString *component = props[@"component"];
  18. if (!component) return nil;
  19. NSDictionary *passProps = props[@"passProps"];
  20. NSDictionary *navigatorStyle = props[@"style"];
  21. RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:globalProps bridge:bridge];
  22. if (!viewController) return nil;
  23. NSArray *leftButtons = props[@"leftButtons"];
  24. if (leftButtons)
  25. {
  26. [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
  27. }
  28. NSArray *rightButtons = props[@"rightButtons"];
  29. if (rightButtons)
  30. {
  31. [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
  32. }
  33. self = [super initWithRootViewController:viewController];
  34. if (!self) return nil;
  35. self.delegate = self;
  36. self.navigationBar.translucent = NO; // default
  37. [self processTitleView:viewController
  38. props:props
  39. style:navigatorStyle];
  40. [self setRotation:props];
  41. return self;
  42. }
  43. - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge
  44. {
  45. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  46. // push
  47. if ([performAction isEqualToString:@"push"])
  48. {
  49. NSString *component = actionParams[@"component"];
  50. if (!component) return;
  51. NSDictionary *passProps = actionParams[@"passProps"];
  52. NSDictionary *navigatorStyle = actionParams[@"style"];
  53. // merge the navigatorStyle of our parent
  54. if ([self.topViewController isKindOfClass:[RCCViewController class]])
  55. {
  56. RCCViewController *parent = (RCCViewController*)self.topViewController;
  57. NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:parent.navigatorStyle];
  58. // there are a few styles that we don't want to remember from our parent (they should be local)
  59. [mergedStyle removeObjectForKey:@"navBarHidden"];
  60. [mergedStyle removeObjectForKey:@"statusBarHidden"];
  61. [mergedStyle removeObjectForKey:@"navBarHideOnScroll"];
  62. [mergedStyle removeObjectForKey:@"drawUnderNavBar"];
  63. [mergedStyle removeObjectForKey:@"drawUnderTabBar"];
  64. [mergedStyle removeObjectForKey:@"statusBarBlur"];
  65. [mergedStyle removeObjectForKey:@"navBarBlur"];
  66. [mergedStyle removeObjectForKey:@"navBarTranslucent"];
  67. [mergedStyle removeObjectForKey:@"statusBarHideWithNavBar"];
  68. [mergedStyle removeObjectForKey:@"autoAdjustScrollViewInsets"];
  69. [mergedStyle removeObjectForKey:@"statusBarTextColorSchemeSingleScreen"];
  70. [mergedStyle addEntriesFromDictionary:navigatorStyle];
  71. navigatorStyle = mergedStyle;
  72. }
  73. RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:nil bridge:bridge];
  74. [self processTitleView:viewController
  75. props:actionParams
  76. style:navigatorStyle];
  77. NSString *backButtonTitle = actionParams[@"backButtonTitle"];
  78. if (backButtonTitle)
  79. {
  80. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:backButtonTitle
  81. style:UIBarButtonItemStylePlain
  82. target:nil
  83. action:nil];
  84. self.topViewController.navigationItem.backBarButtonItem = backItem;
  85. }
  86. else
  87. {
  88. self.topViewController.navigationItem.backBarButtonItem = nil;
  89. }
  90. NSNumber *backButtonHidden = actionParams[@"backButtonHidden"];
  91. BOOL backButtonHiddenBool = backButtonHidden ? [backButtonHidden boolValue] : NO;
  92. if (backButtonHiddenBool)
  93. {
  94. viewController.navigationItem.hidesBackButton = YES;
  95. }
  96. NSArray *leftButtons = actionParams[@"leftButtons"];
  97. if (leftButtons)
  98. {
  99. [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
  100. }
  101. NSArray *rightButtons = actionParams[@"rightButtons"];
  102. if (rightButtons)
  103. {
  104. [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
  105. }
  106. [self pushViewController:viewController animated:animated];
  107. return;
  108. }
  109. // pop
  110. if ([performAction isEqualToString:@"pop"])
  111. {
  112. [self popViewControllerAnimated:animated];
  113. return;
  114. }
  115. // popToRoot
  116. if ([performAction isEqualToString:@"popToRoot"])
  117. {
  118. [self popToRootViewControllerAnimated:animated];
  119. return;
  120. }
  121. // resetTo
  122. if ([performAction isEqualToString:@"resetTo"])
  123. {
  124. NSString *component = actionParams[@"component"];
  125. if (!component) return;
  126. NSDictionary *passProps = actionParams[@"passProps"];
  127. NSDictionary *navigatorStyle = actionParams[@"style"];
  128. RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:nil bridge:bridge];
  129. [self processTitleView:viewController
  130. props:actionParams
  131. style:navigatorStyle];
  132. NSArray *leftButtons = actionParams[@"leftButtons"];
  133. if (leftButtons)
  134. {
  135. [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
  136. }
  137. NSArray *rightButtons = actionParams[@"rightButtons"];
  138. if (rightButtons)
  139. {
  140. [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
  141. }
  142. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  143. [self setViewControllers:@[viewController] animated:animated];
  144. return;
  145. }
  146. // setButtons
  147. if ([performAction isEqualToString:@"setButtons"])
  148. {
  149. NSArray *buttons = actionParams[@"buttons"];
  150. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  151. NSString *side = actionParams[@"side"] ? actionParams[@"side"] : @"left";
  152. [self setButtons:buttons viewController:self.topViewController side:side animated:animated];
  153. return;
  154. }
  155. // setTitle
  156. if ([performAction isEqualToString:@"setTitle"] || [performAction isEqualToString:@"setTitleImage"])
  157. {
  158. NSDictionary *navigatorStyle = actionParams[@"style"];
  159. [self processTitleView:self.topViewController
  160. props:actionParams
  161. style:navigatorStyle];
  162. return;
  163. }
  164. // toggleNavBar
  165. if ([performAction isEqualToString:@"setHidden"]) {
  166. NSNumber *animated = actionParams[@"animated"];
  167. BOOL animatedBool = animated ? [animated boolValue] : YES;
  168. NSNumber *setHidden = actionParams[@"hidden"];
  169. BOOL isHiddenBool = setHidden ? [setHidden boolValue] : NO;
  170. RCCViewController *topViewController = ((RCCViewController*)self.topViewController);
  171. topViewController.navigatorStyle[@"navBarHidden"] = setHidden;
  172. [topViewController setNavBarVisibilityChange:animatedBool];
  173. }
  174. // setStyle
  175. if ([performAction isEqualToString:@"setStyle"])
  176. {
  177. NSDictionary *navigatorStyle = actionParams;
  178. // merge the navigatorStyle of our parent
  179. if ([self.topViewController isKindOfClass:[RCCViewController class]])
  180. {
  181. RCCViewController *parent = (RCCViewController*)self.topViewController;
  182. NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:parent.navigatorStyle];
  183. // there are a few styles that we don't want to remember from our parent (they should be local)
  184. [mergedStyle setValuesForKeysWithDictionary:navigatorStyle];
  185. navigatorStyle = mergedStyle;
  186. parent.navigatorStyle = navigatorStyle;
  187. [parent setStyleOnInit];
  188. [parent updateStyle];
  189. }
  190. }
  191. }
  192. -(void)onButtonPress:(UIBarButtonItem*)barButtonItem
  193. {
  194. NSString *callbackId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY);
  195. if (!callbackId) return;
  196. NSString *buttonId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID);
  197. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:callbackId body:@
  198. {
  199. @"type": @"NavBarButtonPress",
  200. @"id": buttonId ? buttonId : [NSNull null]
  201. }];
  202. }
  203. -(void)setButtons:(NSArray*)buttons viewController:(UIViewController*)viewController side:(NSString*)side animated:(BOOL)animated
  204. {
  205. NSMutableArray *barButtonItems = [NSMutableArray new];
  206. for (NSDictionary *button in buttons)
  207. {
  208. NSString *title = button[@"title"];
  209. UIImage *iconImage = nil;
  210. id icon = button[@"icon"];
  211. if (icon) iconImage = [RCTConvert UIImage:icon];
  212. UIBarButtonItem *barButtonItem;
  213. if (iconImage)
  214. {
  215. barButtonItem = [[UIBarButtonItem alloc] initWithImage:iconImage style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
  216. }
  217. else if (title)
  218. {
  219. barButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
  220. }
  221. else continue;
  222. objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY, button[@"onPress"], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  223. [barButtonItems addObject:barButtonItem];
  224. NSString *buttonId = button[@"id"];
  225. if (buttonId)
  226. {
  227. objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID, buttonId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  228. }
  229. NSNumber *disabled = button[@"disabled"];
  230. BOOL disabledBool = disabled ? [disabled boolValue] : NO;
  231. if (disabledBool) {
  232. [barButtonItem setEnabled:NO];
  233. }
  234. NSNumber *disableIconTintString = button[@"disableIconTint"];
  235. BOOL disableIconTint = disableIconTintString ? [disableIconTintString boolValue] : NO;
  236. if (disableIconTint) {
  237. [barButtonItem setImage:[barButtonItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  238. }
  239. NSString *testID = button[@"testID"];
  240. if (testID)
  241. {
  242. barButtonItem.accessibilityIdentifier = testID;
  243. }
  244. }
  245. if ([side isEqualToString:@"left"])
  246. {
  247. [viewController.navigationItem setLeftBarButtonItems:barButtonItems animated:animated];
  248. }
  249. if ([side isEqualToString:@"right"])
  250. {
  251. [viewController.navigationItem setRightBarButtonItems:barButtonItems animated:animated];
  252. }
  253. }
  254. -(void)processTitleView:(UIViewController*)viewController
  255. props:(NSDictionary*)props
  256. style:(NSDictionary*)style
  257. {
  258. RCCTitleViewHelper *titleViewHelper = [[RCCTitleViewHelper alloc] init:viewController
  259. navigationController:self
  260. title:props[@"title"]
  261. subtitle:props[@"subtitle"]
  262. titleImageData:props[@"titleImage"]];
  263. [titleViewHelper setup:style];
  264. }
  265. - (UIStatusBarStyle)preferredStatusBarStyle {
  266. return [self.topViewController preferredStatusBarStyle];
  267. }
  268. #pragma mark - UINavigationControllerDelegate
  269. -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  270. [viewController setNeedsStatusBarAppearanceUpdate];
  271. }
  272. @end