react-native-navigation的迁移库

RCCTabBarController.m 15KB

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