react-native-navigation的迁移库

RCCTabBarController.m 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. if ([viewController isKindOfClass:[UINavigationController class]]) {
  30. UINavigationController *navigationController = (UINavigationController*)viewController;
  31. UIViewController *topViewController = navigationController.topViewController;
  32. if ([topViewController isKindOfClass:[RCCViewController class]]) {
  33. RCCViewController *topRCCViewController = (RCCViewController*)topViewController;
  34. topRCCViewController.commandType = COMMAND_TYPE_BOTTOME_TAB_SELECTED;
  35. topRCCViewController.timestamp = [RCTHelpers getTimestampString];
  36. }
  37. }
  38. } else {
  39. [RCCTabBarController sendScreenTabPressedEvent:viewController body:nil];
  40. }
  41. return YES;
  42. }
  43. - (UIImage *)image:(UIImage*)image withColor:(UIColor *)color1 {
  44. UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
  45. CGContextRef context = UIGraphicsGetCurrentContext();
  46. CGContextTranslateCTM(context, 0, image.size.height);
  47. CGContextScaleCTM(context, 1.0, -1.0);
  48. CGContextSetBlendMode(context, kCGBlendModeNormal);
  49. CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
  50. CGContextClipToMask(context, rect, image.CGImage);
  51. [color1 setFill];
  52. CGContextFillRect(context, rect);
  53. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  54. UIGraphicsEndImageContext();
  55. return newImage;
  56. }
  57. - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary*)globalProps bridge:(RCTBridge *)bridge {
  58. self = [super init];
  59. if (!self) return nil;
  60. self.delegate = self;
  61. self.tabBar.translucent = YES; // default
  62. UIColor *buttonColor = nil;
  63. UIColor *selectedButtonColor = nil;
  64. UIColor *labelColor = nil;
  65. UIColor *selectedLabelColor = nil;
  66. NSDictionary *tabsStyle = props[@"style"];
  67. if (tabsStyle) {
  68. NSString *tabBarButtonColor = tabsStyle[@"tabBarButtonColor"];
  69. if (tabBarButtonColor) {
  70. UIColor *color = tabBarButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarButtonColor] : nil;
  71. self.tabBar.tintColor = color;
  72. buttonColor = color;
  73. selectedButtonColor = color;
  74. }
  75. NSString *tabBarSelectedButtonColor = tabsStyle[@"tabBarSelectedButtonColor"];
  76. if (tabBarSelectedButtonColor) {
  77. UIColor *color = tabBarSelectedButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarSelectedButtonColor] : nil;
  78. self.tabBar.tintColor = color;
  79. selectedButtonColor = color;
  80. }
  81. NSString *tabBarLabelColor = tabsStyle[@"tabBarLabelColor"];
  82. if (tabBarLabelColor) {
  83. UIColor *color = tabBarLabelColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarLabelColor] : nil;
  84. labelColor = color;
  85. }
  86. NSString *tabBarSelectedLabelColor = tabsStyle[@"tabBarSelectedLabelColor"];
  87. if (tabBarLabelColor) {
  88. UIColor *color = tabBarSelectedLabelColor != (id)[NSNull null] ? [RCTConvert UIColor:
  89. tabBarSelectedLabelColor] : nil;
  90. selectedLabelColor = color;
  91. }
  92. NSString *tabBarBackgroundColor = tabsStyle[@"tabBarBackgroundColor"];
  93. if (tabBarBackgroundColor) {
  94. UIColor *color = tabBarBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarBackgroundColor] : nil;
  95. self.tabBar.barTintColor = color;
  96. }
  97. NSString *tabBarTranslucent = tabsStyle[@"tabBarTranslucent"];
  98. if (tabBarTranslucent) {
  99. self.tabBar.translucent = [tabBarTranslucent boolValue] ? YES : NO;
  100. }
  101. NSString *tabBarHideShadow = tabsStyle[@"tabBarHideShadow"];
  102. if (tabBarHideShadow) {
  103. self.tabBar.clipsToBounds = [tabBarHideShadow boolValue] ? YES : NO;
  104. }
  105. }
  106. NSMutableArray *viewControllers = [NSMutableArray array];
  107. // go over all the tab bar items
  108. for (NSDictionary *tabItemLayout in children) {
  109. // make sure the layout is valid
  110. if (![tabItemLayout[@"type"] isEqualToString:@"TabBarControllerIOS.Item"]) continue;
  111. if (!tabItemLayout[@"props"]) continue;
  112. // get the view controller inside
  113. if (!tabItemLayout[@"children"]) continue;
  114. if (![tabItemLayout[@"children"] isKindOfClass:[NSArray class]]) continue;
  115. if ([tabItemLayout[@"children"] count] < 1) continue;
  116. NSDictionary *childLayout = tabItemLayout[@"children"][0];
  117. UIViewController *viewController = [RCCViewController controllerWithLayout:childLayout globalProps:globalProps bridge:bridge];
  118. if (!viewController) continue;
  119. // create the tab icon and title
  120. NSString *title = tabItemLayout[@"props"][@"title"];
  121. UIImage *iconImage = nil;
  122. id icon = tabItemLayout[@"props"][@"icon"];
  123. if (icon) {
  124. iconImage = [RCTConvert UIImage:icon];
  125. if (buttonColor) {
  126. iconImage = [[self image:iconImage withColor:buttonColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  127. }
  128. }
  129. UIImage *iconImageSelected = nil;
  130. id selectedIcon = tabItemLayout[@"props"][@"selectedIcon"];
  131. if (selectedIcon) {
  132. iconImageSelected = [RCTConvert UIImage:selectedIcon];
  133. } else {
  134. iconImageSelected = [RCTConvert UIImage:icon];
  135. }
  136. viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:iconImage tag:0];
  137. viewController.tabBarItem.accessibilityIdentifier = tabItemLayout[@"props"][@"testID"];
  138. viewController.tabBarItem.selectedImage = iconImageSelected;
  139. id imageInsets = tabItemLayout[@"props"][@"iconInsets"];
  140. if (imageInsets && imageInsets != (id)[NSNull null]) {
  141. id topInset = imageInsets[@"top"];
  142. id leftInset = imageInsets[@"left"];
  143. id bottomInset = imageInsets[@"bottom"];
  144. id rightInset = imageInsets[@"right"];
  145. CGFloat top = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:topInset] : 0;
  146. CGFloat left = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:leftInset] : 0;
  147. CGFloat bottom = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:bottomInset] : 0;
  148. CGFloat right = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:rightInset] : 0;
  149. viewController.tabBarItem.imageInsets = UIEdgeInsetsMake(top, left, bottom, right);
  150. }
  151. NSMutableDictionary *unselectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarText" baseFont:[UIFont systemFontOfSize:10]];
  152. if (!unselectedAttributes[NSForegroundColorAttributeName] && labelColor) {
  153. unselectedAttributes[NSForegroundColorAttributeName] = labelColor;
  154. }
  155. [viewController.tabBarItem setTitleTextAttributes:unselectedAttributes forState:UIControlStateNormal];
  156. NSMutableDictionary *selectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarSelectedText" baseFont:[UIFont systemFontOfSize:10]];
  157. if (!selectedAttributes[NSForegroundColorAttributeName] && selectedLabelColor) {
  158. selectedAttributes[NSForegroundColorAttributeName] = selectedLabelColor;
  159. }
  160. [viewController.tabBarItem setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
  161. // create badge
  162. NSObject *badge = tabItemLayout[@"props"][@"badge"];
  163. if (badge == nil || [badge isEqual:[NSNull null]]) {
  164. viewController.tabBarItem.badgeValue = nil;
  165. } else {
  166. viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
  167. }
  168. [viewControllers addObject:viewController];
  169. }
  170. // replace the tabs
  171. self.viewControllers = viewControllers;
  172. NSNumber *initialTab = tabsStyle[@"initialTabIndex"];
  173. if (initialTab) {
  174. NSInteger initialTabIndex = initialTab.integerValue;
  175. [self setSelectedIndex:initialTabIndex];
  176. }
  177. [self setRotation:props];
  178. return self;
  179. }
  180. - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge completion:(void (^)(void))completion {
  181. if ([performAction isEqualToString:@"setBadge"]) {
  182. UIViewController *viewController = nil;
  183. NSNumber *tabIndex = actionParams[@"tabIndex"];
  184. if (tabIndex) {
  185. int i = (int)[tabIndex integerValue];
  186. if ([self.viewControllers count] > i) {
  187. viewController = [self.viewControllers objectAtIndex:i];
  188. }
  189. }
  190. NSString *contentId = actionParams[@"contentId"];
  191. NSString *contentType = actionParams[@"contentType"];
  192. if (contentId && contentType) {
  193. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  194. }
  195. if (viewController) {
  196. NSObject *badge = actionParams[@"badge"];
  197. if (badge == nil || [badge isEqual:[NSNull null]]) {
  198. viewController.tabBarItem.badgeValue = nil;
  199. }
  200. else
  201. {
  202. NSString *badgeColor = actionParams[@"badgeColor"];
  203. UIColor *color = badgeColor != (id)[NSNull null] ? [RCTConvert UIColor:badgeColor] : nil;
  204. if ([viewController.tabBarItem respondsToSelector:@selector(badgeColor)]) {
  205. viewController.tabBarItem.badgeColor = color;
  206. }
  207. viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
  208. }
  209. }
  210. }
  211. if ([performAction isEqualToString:@"switchTo"]) {
  212. UIViewController *viewController = nil;
  213. NSNumber *tabIndex = actionParams[@"tabIndex"];
  214. if (tabIndex) {
  215. int i = (int)[tabIndex integerValue];
  216. if ([self.viewControllers count] > i) {
  217. viewController = [self.viewControllers objectAtIndex:i];
  218. }
  219. }
  220. NSString *contentId = actionParams[@"contentId"];
  221. NSString *contentType = actionParams[@"contentType"];
  222. if (contentId && contentType) {
  223. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  224. }
  225. if (viewController) {
  226. [self setSelectedViewController:viewController];
  227. }
  228. }
  229. if ([performAction isEqualToString:@"setTabButton"]) {
  230. UIViewController *viewController = nil;
  231. NSNumber *tabIndex = actionParams[@"tabIndex"];
  232. if (tabIndex) {
  233. int i = (int)[tabIndex integerValue];
  234. if ([self.viewControllers count] > i) {
  235. viewController = [self.viewControllers objectAtIndex:i];
  236. }
  237. }
  238. NSString *contentId = actionParams[@"contentId"];
  239. NSString *contentType = actionParams[@"contentType"];
  240. if (contentId && contentType) {
  241. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  242. }
  243. if (viewController) {
  244. UIImage *iconImage = nil;
  245. id icon = actionParams[@"icon"];
  246. if (icon && icon != (id)[NSNull null]) {
  247. iconImage = [RCTConvert UIImage:icon];
  248. iconImage = [[self image:iconImage withColor:self.tabBar.tintColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  249. viewController.tabBarItem.image = iconImage;
  250. }
  251. UIImage *iconImageSelected = nil;
  252. id selectedIcon = actionParams[@"selectedIcon"];
  253. if (selectedIcon && selectedIcon != (id)[NSNull null]) {
  254. iconImageSelected = [RCTConvert UIImage:selectedIcon];
  255. viewController.tabBarItem.selectedImage = iconImageSelected;
  256. }
  257. id label = actionParams[@"label"];
  258. if (label && label != (id)[NSNull null]) {
  259. viewController.tabBarItem.title = label;
  260. }
  261. }
  262. }
  263. if ([performAction isEqualToString:@"setTabBarHidden"]) {
  264. BOOL hidden = [actionParams[@"hidden"] boolValue];
  265. self.tabBarHidden = hidden;
  266. CGRect nextFrame = self.tabBar.frame;
  267. nextFrame.origin.y = UIScreen.mainScreen.bounds.size.height - (hidden ? 0 : self.tabBar.frame.size.height);
  268. [UIView animateWithDuration: ([actionParams[@"animated"] boolValue] ? 0.45 : 0)
  269. delay: 0
  270. usingSpringWithDamping: 0.75
  271. initialSpringVelocity: 0
  272. options: (hidden ? UIViewAnimationOptionCurveEaseIn : UIViewAnimationOptionCurveEaseOut)
  273. animations:^()
  274. {
  275. [self.tabBar setFrame:nextFrame];
  276. }
  277. completion:^(BOOL finished)
  278. {
  279. if (completion != nil) {
  280. completion();
  281. }
  282. }];
  283. return;
  284. } else if (completion != nil) {
  285. completion();
  286. }
  287. }
  288. +(void)sendScreenTabChangedEvent:(UIViewController*)viewController body:(NSDictionary*)body{
  289. [RCCTabBarController sendTabEvent:@"bottomTabSelected" controller:viewController body:body];
  290. }
  291. +(void)sendScreenTabPressedEvent:(UIViewController*)viewController body:(NSDictionary*)body{
  292. [RCCTabBarController sendTabEvent:@"bottomTabReselected" controller:viewController body:body];
  293. }
  294. +(void)sendTabEvent:(NSString *)event controller:(UIViewController*)viewController body:(NSDictionary*)body{
  295. if ([viewController.view isKindOfClass:[RCTRootView class]]){
  296. RCTRootView *rootView = (RCTRootView *)viewController.view;
  297. if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
  298. NSString *navigatorID = rootView.appProperties[@"navigatorID"];
  299. NSString *screenInstanceID = rootView.appProperties[@"screenInstanceID"];
  300. NSMutableDictionary *screenDict = [NSMutableDictionary dictionaryWithDictionary:@
  301. {
  302. @"id": event,
  303. @"navigatorID": navigatorID,
  304. @"screenInstanceID": screenInstanceID
  305. }];
  306. if (body) {
  307. [screenDict addEntriesFromDictionary:body];
  308. }
  309. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:screenDict];
  310. }
  311. }
  312. if ([viewController isKindOfClass:[UINavigationController class]]) {
  313. UINavigationController *navigationController = (UINavigationController*)viewController;
  314. UIViewController *topViewController = [navigationController topViewController];
  315. [RCCTabBarController sendTabEvent:event controller:topViewController body:body];
  316. }
  317. }
  318. @end