react-native-navigation的迁移库

RCCNavigationController.m 10KB

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