react-native-navigation的迁移库

RCCTabBarController.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. }
  82. NSMutableArray *viewControllers = [NSMutableArray array];
  83. // go over all the tab bar items
  84. for (NSDictionary *tabItemLayout in children)
  85. {
  86. // make sure the layout is valid
  87. if (![tabItemLayout[@"type"] isEqualToString:@"TabBarControllerIOS.Item"]) continue;
  88. if (!tabItemLayout[@"props"]) continue;
  89. // get the view controller inside
  90. if (!tabItemLayout[@"children"]) continue;
  91. if (![tabItemLayout[@"children"] isKindOfClass:[NSArray class]]) continue;
  92. if ([tabItemLayout[@"children"] count] < 1) continue;
  93. NSDictionary *childLayout = tabItemLayout[@"children"][0];
  94. UIViewController *viewController = [RCCViewController controllerWithLayout:childLayout globalProps:globalProps bridge:bridge];
  95. if (!viewController) continue;
  96. // create the tab icon and title
  97. NSString *title = tabItemLayout[@"props"][@"title"];
  98. UIImage *iconImage = nil;
  99. id icon = tabItemLayout[@"props"][@"icon"];
  100. if (icon)
  101. {
  102. iconImage = [RCTConvert UIImage:icon];
  103. if (buttonColor)
  104. {
  105. iconImage = [[self image:iconImage withColor:buttonColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  106. }
  107. }
  108. UIImage *iconImageSelected = nil;
  109. id selectedIcon = tabItemLayout[@"props"][@"selectedIcon"];
  110. if (selectedIcon) {
  111. iconImageSelected = [RCTConvert UIImage:selectedIcon];
  112. } else {
  113. iconImageSelected = [RCTConvert UIImage:icon];
  114. }
  115. viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:iconImage tag:0];
  116. viewController.tabBarItem.accessibilityIdentifier = tabItemLayout[@"props"][@"testID"];
  117. viewController.tabBarItem.selectedImage = iconImageSelected;
  118. NSMutableDictionary *unselectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarText" baseFont:[UIFont systemFontOfSize:10]];
  119. if (!unselectedAttributes[NSForegroundColorAttributeName] && buttonColor) {
  120. unselectedAttributes[NSForegroundColorAttributeName] = buttonColor;
  121. }
  122. [viewController.tabBarItem setTitleTextAttributes:unselectedAttributes forState:UIControlStateNormal]
  123. ;
  124. NSMutableDictionary *selectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarSelectedText" baseFont:[UIFont systemFontOfSize:10]];
  125. if (!selectedAttributes[NSForegroundColorAttributeName] && selectedButtonColor) {
  126. selectedAttributes[NSForegroundColorAttributeName] = selectedButtonColor;
  127. }
  128. [viewController.tabBarItem setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
  129. // create badge
  130. NSObject *badge = tabItemLayout[@"props"][@"badge"];
  131. if (badge == nil || [badge isEqual:[NSNull null]])
  132. {
  133. viewController.tabBarItem.badgeValue = nil;
  134. }
  135. else
  136. {
  137. viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
  138. }
  139. [viewControllers addObject:viewController];
  140. }
  141. // replace the tabs
  142. self.viewControllers = viewControllers;
  143. [self setRotation:props];
  144. return self;
  145. }
  146. - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge completion:(void (^)(void))completion
  147. {
  148. if ([performAction isEqualToString:@"setBadge"])
  149. {
  150. UIViewController *viewController = nil;
  151. NSNumber *tabIndex = actionParams[@"tabIndex"];
  152. if (tabIndex)
  153. {
  154. int i = (int)[tabIndex integerValue];
  155. if ([self.viewControllers count] > i)
  156. {
  157. viewController = [self.viewControllers objectAtIndex:i];
  158. }
  159. }
  160. NSString *contentId = actionParams[@"contentId"];
  161. NSString *contentType = actionParams[@"contentType"];
  162. if (contentId && contentType)
  163. {
  164. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  165. }
  166. if (viewController)
  167. {
  168. NSObject *badge = actionParams[@"badge"];
  169. if (badge == nil || [badge isEqual:[NSNull null]])
  170. {
  171. viewController.tabBarItem.badgeValue = nil;
  172. }
  173. else
  174. {
  175. viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
  176. }
  177. }
  178. }
  179. if ([performAction isEqualToString:@"switchTo"])
  180. {
  181. UIViewController *viewController = nil;
  182. NSNumber *tabIndex = actionParams[@"tabIndex"];
  183. if (tabIndex)
  184. {
  185. int i = (int)[tabIndex integerValue];
  186. if ([self.viewControllers count] > i)
  187. {
  188. viewController = [self.viewControllers objectAtIndex:i];
  189. }
  190. }
  191. NSString *contentId = actionParams[@"contentId"];
  192. NSString *contentType = actionParams[@"contentType"];
  193. if (contentId && contentType)
  194. {
  195. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  196. }
  197. if (viewController)
  198. {
  199. [self setSelectedViewController:viewController];
  200. }
  201. }
  202. if ([performAction isEqualToString:@"setTabBarHidden"])
  203. {
  204. BOOL hidden = [actionParams[@"hidden"] boolValue];
  205. [UIView animateWithDuration: ([actionParams[@"animated"] boolValue] ? 0.45 : 0)
  206. delay: 0
  207. usingSpringWithDamping: 0.75
  208. initialSpringVelocity: 0
  209. options: (hidden ? UIViewAnimationOptionCurveEaseIn : UIViewAnimationOptionCurveEaseOut)
  210. animations:^()
  211. {
  212. self.tabBar.transform = hidden ? CGAffineTransformMakeTranslation(0, self.tabBar.frame.size.height) : CGAffineTransformIdentity;
  213. }
  214. completion:^(BOOL finished)
  215. {
  216. if (completion != nil)
  217. {
  218. completion();
  219. }
  220. }];
  221. return;
  222. }
  223. else if (completion != nil)
  224. {
  225. completion();
  226. }
  227. }
  228. +(void)sendScreenTabChangedEvent:(UIViewController*)viewController body:(NSDictionary*)body{
  229. [RCCTabBarController sendTabEvent:@"bottomTabSelected" controller:viewController body:body];
  230. }
  231. +(void)sendScreenTabPressedEvent:(UIViewController*)viewController body:(NSDictionary*)body{
  232. [RCCTabBarController sendTabEvent:@"bottomTabReselected" controller:viewController body:body];
  233. }
  234. +(void)sendTabEvent:(NSString *)event controller:(UIViewController*)viewController body:(NSDictionary*)body{
  235. if ([viewController.view isKindOfClass:[RCTRootView class]]){
  236. RCTRootView *rootView = (RCTRootView *)viewController.view;
  237. if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
  238. NSString *navigatorID = rootView.appProperties[@"navigatorID"];
  239. NSString *screenInstanceID = rootView.appProperties[@"screenInstanceID"];
  240. NSMutableDictionary *screenDict = [NSMutableDictionary dictionaryWithDictionary:@
  241. {
  242. @"id": event,
  243. @"navigatorID": navigatorID,
  244. @"screenInstanceID": screenInstanceID
  245. }];
  246. if (body) {
  247. [screenDict addEntriesFromDictionary:body];
  248. }
  249. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:screenDict];
  250. }
  251. }
  252. if ([viewController isKindOfClass:[UINavigationController class]]) {
  253. UINavigationController *navigationController = (UINavigationController*)viewController;
  254. UIViewController *topViewController = [navigationController topViewController];
  255. [RCCTabBarController sendTabEvent:event controller:topViewController body:body];
  256. }
  257. }
  258. @end