react-native-navigation的迁移库

RCCNavigationController.m 12KB

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