react-native-navigation的迁移库

RCCTabBarController.m 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #import "RCCTabBarController.h"
  2. #import "RCCViewController.h"
  3. #import "RCTConvert.h"
  4. #import "RCCManager.h"
  5. #import "RCTUIManager.h"
  6. @interface RCTUIManager ()
  7. - (void)configureNextLayoutAnimation:(NSDictionary *)config
  8. withCallback:(RCTResponseSenderBlock)callback
  9. errorCallback:(__unused RCTResponseSenderBlock)errorCallback;
  10. @end
  11. @implementation RCCTabBarController
  12. - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
  13. id queue = [[RCCManager sharedInstance].getBridge uiManager].methodQueue;
  14. dispatch_async(queue, ^{
  15. [[[RCCManager sharedInstance].getBridge uiManager] configureNextLayoutAnimation:nil withCallback:^(NSArray* arr){} errorCallback:^(NSArray* arr){}];
  16. });
  17. [RCCTabBarController sendScreenTabChangedEvent:viewController];
  18. return YES;
  19. }
  20. - (UIImage *)image:(UIImage*)image withColor:(UIColor *)color1
  21. {
  22. UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
  23. CGContextRef context = UIGraphicsGetCurrentContext();
  24. CGContextTranslateCTM(context, 0, image.size.height);
  25. CGContextScaleCTM(context, 1.0, -1.0);
  26. CGContextSetBlendMode(context, kCGBlendModeNormal);
  27. CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
  28. CGContextClipToMask(context, rect, image.CGImage);
  29. [color1 setFill];
  30. CGContextFillRect(context, rect);
  31. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  32. UIGraphicsEndImageContext();
  33. return newImage;
  34. }
  35. - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary*)globalProps bridge:(RCTBridge *)bridge
  36. {
  37. self = [super init];
  38. if (!self) return nil;
  39. self.delegate = self;
  40. self.tabBar.translucent = YES; // default
  41. UIColor *buttonColor = nil;
  42. UIColor *selectedButtonColor = nil;
  43. NSDictionary *tabsStyle = props[@"style"];
  44. if (tabsStyle)
  45. {
  46. NSString *tabBarButtonColor = tabsStyle[@"tabBarButtonColor"];
  47. if (tabBarButtonColor)
  48. {
  49. UIColor *color = tabBarButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarButtonColor] : nil;
  50. self.tabBar.tintColor = color;
  51. buttonColor = color;
  52. selectedButtonColor = color;
  53. }
  54. NSString *tabBarSelectedButtonColor = tabsStyle[@"tabBarSelectedButtonColor"];
  55. if (tabBarSelectedButtonColor)
  56. {
  57. UIColor *color = tabBarSelectedButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarSelectedButtonColor] : nil;
  58. self.tabBar.tintColor = color;
  59. selectedButtonColor = color;
  60. }
  61. NSString *tabBarBackgroundColor = tabsStyle[@"tabBarBackgroundColor"];
  62. if (tabBarBackgroundColor)
  63. {
  64. UIColor *color = tabBarBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarBackgroundColor] : nil;
  65. self.tabBar.barTintColor = color;
  66. }
  67. }
  68. NSMutableArray *viewControllers = [NSMutableArray array];
  69. // go over all the tab bar items
  70. for (NSDictionary *tabItemLayout in children)
  71. {
  72. // make sure the layout is valid
  73. if (![tabItemLayout[@"type"] isEqualToString:@"TabBarControllerIOS.Item"]) continue;
  74. if (!tabItemLayout[@"props"]) continue;
  75. // get the view controller inside
  76. if (!tabItemLayout[@"children"]) continue;
  77. if (![tabItemLayout[@"children"] isKindOfClass:[NSArray class]]) continue;
  78. if ([tabItemLayout[@"children"] count] < 1) continue;
  79. NSDictionary *childLayout = tabItemLayout[@"children"][0];
  80. UIViewController *viewController = [RCCViewController controllerWithLayout:childLayout globalProps:globalProps bridge:bridge];
  81. if (!viewController) continue;
  82. // create the tab icon and title
  83. NSString *title = tabItemLayout[@"props"][@"title"];
  84. UIImage *iconImage = nil;
  85. id icon = tabItemLayout[@"props"][@"icon"];
  86. if (icon)
  87. {
  88. iconImage = [RCTConvert UIImage:icon];
  89. if (buttonColor)
  90. {
  91. iconImage = [[self image:iconImage withColor:buttonColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  92. }
  93. }
  94. UIImage *iconImageSelected = nil;
  95. id selectedIcon = tabItemLayout[@"props"][@"selectedIcon"];
  96. if (selectedIcon) iconImageSelected = [RCTConvert UIImage:selectedIcon];
  97. viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:iconImage tag:0];
  98. viewController.tabBarItem.accessibilityIdentifier = tabItemLayout[@"props"][@"testID"];
  99. viewController.tabBarItem.selectedImage = iconImageSelected;
  100. if (buttonColor)
  101. {
  102. [viewController.tabBarItem setTitleTextAttributes:
  103. @{NSForegroundColorAttributeName : buttonColor} forState:UIControlStateNormal];
  104. }
  105. if (selectedButtonColor)
  106. {
  107. [viewController.tabBarItem setTitleTextAttributes:
  108. @{NSForegroundColorAttributeName : selectedButtonColor} forState:UIControlStateSelected];
  109. }
  110. // create badge
  111. NSObject *badge = tabItemLayout[@"props"][@"badge"];
  112. if (badge == nil || [badge isEqual:[NSNull null]])
  113. {
  114. viewController.tabBarItem.badgeValue = nil;
  115. }
  116. else
  117. {
  118. viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
  119. }
  120. [viewControllers addObject:viewController];
  121. }
  122. // replace the tabs
  123. self.viewControllers = viewControllers;
  124. return self;
  125. }
  126. - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge completion:(void (^)(void))completion
  127. {
  128. if ([performAction isEqualToString:@"setBadge"])
  129. {
  130. UIViewController *viewController = nil;
  131. NSNumber *tabIndex = actionParams[@"tabIndex"];
  132. if (tabIndex)
  133. {
  134. int i = (int)[tabIndex integerValue];
  135. if ([self.viewControllers count] > i)
  136. {
  137. viewController = [self.viewControllers objectAtIndex:i];
  138. }
  139. }
  140. NSString *contentId = actionParams[@"contentId"];
  141. NSString *contentType = actionParams[@"contentType"];
  142. if (contentId && contentType)
  143. {
  144. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  145. }
  146. if (viewController)
  147. {
  148. NSObject *badge = actionParams[@"badge"];
  149. if (badge == nil || [badge isEqual:[NSNull null]])
  150. {
  151. viewController.tabBarItem.badgeValue = nil;
  152. }
  153. else
  154. {
  155. viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
  156. }
  157. }
  158. }
  159. if ([performAction isEqualToString:@"switchTo"])
  160. {
  161. UIViewController *viewController = nil;
  162. NSNumber *tabIndex = actionParams[@"tabIndex"];
  163. if (tabIndex)
  164. {
  165. int i = (int)[tabIndex integerValue];
  166. if ([self.viewControllers count] > i)
  167. {
  168. viewController = [self.viewControllers objectAtIndex:i];
  169. }
  170. }
  171. NSString *contentId = actionParams[@"contentId"];
  172. NSString *contentType = actionParams[@"contentType"];
  173. if (contentId && contentType)
  174. {
  175. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  176. }
  177. if (viewController)
  178. {
  179. [self setSelectedViewController:viewController];
  180. }
  181. }
  182. if ([performAction isEqualToString:@"setTabBarHidden"])
  183. {
  184. BOOL hidden = [actionParams[@"hidden"] boolValue];
  185. [UIView animateWithDuration: ([actionParams[@"animated"] boolValue] ? 0.45 : 0)
  186. delay: 0
  187. usingSpringWithDamping: 0.75
  188. initialSpringVelocity: 0
  189. options: (hidden ? UIViewAnimationOptionCurveEaseIn : UIViewAnimationOptionCurveEaseOut)
  190. animations:^()
  191. {
  192. self.tabBar.transform = hidden ? CGAffineTransformMakeTranslation(0, self.tabBar.frame.size.height) : CGAffineTransformIdentity;
  193. }
  194. completion:^(BOOL finished)
  195. {
  196. if (completion != nil)
  197. {
  198. completion();
  199. }
  200. }];
  201. return;
  202. }
  203. else if (completion != nil)
  204. {
  205. completion();
  206. }
  207. }
  208. +(void)sendScreenTabChangedEvent:(UIViewController*)viewController {
  209. if ([viewController.view isKindOfClass:[RCTRootView class]]){
  210. RCTRootView *rootView = (RCTRootView *)viewController.view;
  211. if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
  212. NSString *navigatorID = rootView.appProperties[@"navigatorID"];
  213. NSString *screenInstanceID = rootView.appProperties[@"screenInstanceID"];
  214. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:@
  215. {
  216. @"id": @"bottomTabSelected",
  217. @"navigatorID": navigatorID,
  218. @"screenInstanceID": screenInstanceID
  219. }];
  220. }
  221. }
  222. if ([viewController isKindOfClass:[UINavigationController class]]) {
  223. UINavigationController *navigationController = (UINavigationController*)viewController;
  224. UIViewController *topViewController = [navigationController topViewController];
  225. [RCCTabBarController sendScreenTabChangedEvent:topViewController];
  226. }
  227. }
  228. @end