react-native-navigation的迁移库

RCCTabBarController.m 11KB

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