react-native-navigation的迁移库

RCCNavigationController.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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:@"statusBarTextColorSchemeSingleScreen"];
  64. [mergedStyle addEntriesFromDictionary:navigatorStyle];
  65. navigatorStyle = mergedStyle;
  66. }
  67. RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:nil bridge:bridge];
  68. [self processTitleView:viewController
  69. props:actionParams
  70. style:navigatorStyle];
  71. NSString *backButtonTitle = actionParams[@"backButtonTitle"];
  72. if (backButtonTitle)
  73. {
  74. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:backButtonTitle
  75. style:UIBarButtonItemStylePlain
  76. target:nil
  77. action:nil];
  78. self.topViewController.navigationItem.backBarButtonItem = backItem;
  79. }
  80. else
  81. {
  82. self.topViewController.navigationItem.backBarButtonItem = nil;
  83. }
  84. NSNumber *backButtonHidden = actionParams[@"backButtonHidden"];
  85. BOOL backButtonHiddenBool = backButtonHidden ? [backButtonHidden boolValue] : NO;
  86. if (backButtonHiddenBool)
  87. {
  88. viewController.navigationItem.hidesBackButton = YES;
  89. }
  90. NSArray *leftButtons = actionParams[@"leftButtons"];
  91. if (leftButtons)
  92. {
  93. [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
  94. }
  95. NSArray *rightButtons = actionParams[@"rightButtons"];
  96. if (rightButtons)
  97. {
  98. [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
  99. }
  100. [self pushViewController:viewController animated:animated];
  101. return;
  102. }
  103. // pop
  104. if ([performAction isEqualToString:@"pop"])
  105. {
  106. [self popViewControllerAnimated:animated];
  107. return;
  108. }
  109. // popToRoot
  110. if ([performAction isEqualToString:@"popToRoot"])
  111. {
  112. [self popToRootViewControllerAnimated:animated];
  113. return;
  114. }
  115. // resetTo
  116. if ([performAction isEqualToString:@"resetTo"])
  117. {
  118. NSString *component = actionParams[@"component"];
  119. if (!component) return;
  120. NSDictionary *passProps = actionParams[@"passProps"];
  121. NSDictionary *navigatorStyle = actionParams[@"style"];
  122. RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:nil bridge:bridge];
  123. [self processTitleView:viewController
  124. props:actionParams
  125. style:navigatorStyle];
  126. NSArray *leftButtons = actionParams[@"leftButtons"];
  127. if (leftButtons)
  128. {
  129. [self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
  130. }
  131. NSArray *rightButtons = actionParams[@"rightButtons"];
  132. if (rightButtons)
  133. {
  134. [self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
  135. }
  136. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  137. [self setViewControllers:@[viewController] animated:animated];
  138. return;
  139. }
  140. // setButtons
  141. if ([performAction isEqualToString:@"setButtons"])
  142. {
  143. NSArray *buttons = actionParams[@"buttons"];
  144. BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
  145. NSString *side = actionParams[@"side"] ? actionParams[@"side"] : @"left";
  146. [self setButtons:buttons viewController:self.topViewController side:side animated:animated];
  147. return;
  148. }
  149. // setTitle
  150. if ([performAction isEqualToString:@"setTitle"] || [performAction isEqualToString:@"setTitleImage"])
  151. {
  152. NSDictionary *navigatorStyle = actionParams[@"style"];
  153. [self processTitleView:self.topViewController
  154. props:actionParams
  155. style:navigatorStyle];
  156. return;
  157. }
  158. // toggleNavBar
  159. if ([performAction isEqualToString:@"setHidden"]) {
  160. NSNumber *animated = actionParams[@"animated"];
  161. BOOL animatedBool = animated ? [animated boolValue] : YES;
  162. NSNumber *setHidden = actionParams[@"hidden"];
  163. BOOL isHiddenBool = setHidden ? [setHidden boolValue] : NO;
  164. RCCViewController *topViewController = ((RCCViewController*)self.topViewController);
  165. topViewController.navigatorStyle[@"navBarHidden"] = setHidden;
  166. [topViewController setNavBarVisibilityChange:animatedBool];
  167. }
  168. }
  169. -(void)onButtonPress:(UIBarButtonItem*)barButtonItem
  170. {
  171. NSString *callbackId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY);
  172. if (!callbackId) return;
  173. NSString *buttonId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID);
  174. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:callbackId body:@
  175. {
  176. @"type": @"NavBarButtonPress",
  177. @"id": buttonId ? buttonId : [NSNull null]
  178. }];
  179. }
  180. -(void)setButtons:(NSArray*)buttons viewController:(UIViewController*)viewController side:(NSString*)side animated:(BOOL)animated
  181. {
  182. NSMutableArray *barButtonItems = [NSMutableArray new];
  183. for (NSDictionary *button in buttons)
  184. {
  185. NSString *title = button[@"title"];
  186. UIImage *iconImage = nil;
  187. id icon = button[@"icon"];
  188. if (icon) iconImage = [RCTConvert UIImage:icon];
  189. UIBarButtonItem *barButtonItem;
  190. if (iconImage)
  191. {
  192. barButtonItem = [[UIBarButtonItem alloc] initWithImage:iconImage style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
  193. }
  194. else if (title)
  195. {
  196. barButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
  197. }
  198. else continue;
  199. objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY, button[@"onPress"], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  200. [barButtonItems addObject:barButtonItem];
  201. NSString *buttonId = button[@"id"];
  202. if (buttonId)
  203. {
  204. objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID, buttonId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  205. }
  206. NSNumber *disabled = button[@"disabled"];
  207. BOOL disabledBool = disabled ? [disabled boolValue] : NO;
  208. if (disabledBool) {
  209. [barButtonItem setEnabled:NO];
  210. }
  211. NSNumber *disableIconTintString = button[@"disableIconTint"];
  212. BOOL disableIconTint = disableIconTintString ? [disableIconTintString boolValue] : NO;
  213. if (disableIconTint) {
  214. [barButtonItem setImage:[barButtonItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  215. }
  216. NSString *testID = button[@"testID"];
  217. if (testID)
  218. {
  219. barButtonItem.accessibilityIdentifier = testID;
  220. }
  221. }
  222. if ([side isEqualToString:@"left"])
  223. {
  224. [viewController.navigationItem setLeftBarButtonItems:barButtonItems animated:animated];
  225. }
  226. if ([side isEqualToString:@"right"])
  227. {
  228. [viewController.navigationItem setRightBarButtonItems:barButtonItems animated:animated];
  229. }
  230. }
  231. -(void)processTitleView:(UIViewController*)viewController
  232. props:(NSDictionary*)props
  233. style:(NSDictionary*)style
  234. {
  235. RCCTitleViewHelper *titleViewHelper = [[RCCTitleViewHelper alloc] init:viewController
  236. navigationController:self
  237. title:props[@"title"]
  238. subtitle:props[@"subtitle"]
  239. titleImageData:props[@"titleImage"]];
  240. [titleViewHelper setup:style];
  241. }
  242. - (UIStatusBarStyle)preferredStatusBarStyle {
  243. return [self.topViewController preferredStatusBarStyle];
  244. }
  245. #pragma mark - UINavigationControllerDelegate
  246. -(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  247. [viewController setNeedsStatusBarAppearanceUpdate];
  248. }
  249. @end