react-native-navigation的迁移库

RCCTabBarController.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. viewController.tabBarItem.badgeColor = color;
  228. viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
  229. }
  230. }
  231. }
  232. if ([performAction isEqualToString:@"switchTo"])
  233. {
  234. UIViewController *viewController = nil;
  235. NSNumber *tabIndex = actionParams[@"tabIndex"];
  236. if (tabIndex)
  237. {
  238. int i = (int)[tabIndex integerValue];
  239. if ([self.viewControllers count] > i)
  240. {
  241. viewController = [self.viewControllers objectAtIndex:i];
  242. }
  243. }
  244. NSString *contentId = actionParams[@"contentId"];
  245. NSString *contentType = actionParams[@"contentType"];
  246. if (contentId && contentType)
  247. {
  248. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  249. }
  250. if (viewController)
  251. {
  252. [self setSelectedViewController:viewController];
  253. }
  254. }
  255. if ([performAction isEqualToString:@"setTabButton"])
  256. {
  257. UIViewController *viewController = nil;
  258. NSNumber *tabIndex = actionParams[@"tabIndex"];
  259. if (tabIndex)
  260. {
  261. int i = (int)[tabIndex integerValue];
  262. if ([self.viewControllers count] > i)
  263. {
  264. viewController = [self.viewControllers objectAtIndex:i];
  265. }
  266. }
  267. NSString *contentId = actionParams[@"contentId"];
  268. NSString *contentType = actionParams[@"contentType"];
  269. if (contentId && contentType)
  270. {
  271. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  272. }
  273. if (viewController)
  274. {
  275. UIImage *iconImage = nil;
  276. id icon = actionParams[@"icon"];
  277. if (icon && icon != (id)[NSNull null])
  278. {
  279. iconImage = [RCTConvert UIImage:icon];
  280. iconImage = [[self image:iconImage withColor:self.tabBar.tintColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  281. viewController.tabBarItem.image = iconImage;
  282. }
  283. UIImage *iconImageSelected = nil;
  284. id selectedIcon = actionParams[@"selectedIcon"];
  285. if (selectedIcon && selectedIcon != (id)[NSNull null])
  286. {
  287. iconImageSelected = [RCTConvert UIImage:selectedIcon];
  288. viewController.tabBarItem.selectedImage = iconImageSelected;
  289. }
  290. }
  291. }
  292. if ([performAction isEqualToString:@"setTabBarHidden"])
  293. {
  294. BOOL hidden = [actionParams[@"hidden"] boolValue];
  295. [UIView animateWithDuration: ([actionParams[@"animated"] boolValue] ? 0.45 : 0)
  296. delay: 0
  297. usingSpringWithDamping: 0.75
  298. initialSpringVelocity: 0
  299. options: (hidden ? UIViewAnimationOptionCurveEaseIn : UIViewAnimationOptionCurveEaseOut)
  300. animations:^()
  301. {
  302. self.tabBar.transform = hidden ? CGAffineTransformMakeTranslation(0, self.tabBar.frame.size.height) : CGAffineTransformIdentity;
  303. }
  304. completion:^(BOOL finished)
  305. {
  306. if (completion != nil)
  307. {
  308. completion();
  309. }
  310. }];
  311. return;
  312. }
  313. else if (completion != nil)
  314. {
  315. completion();
  316. }
  317. }
  318. +(void)sendScreenTabChangedEvent:(UIViewController*)viewController body:(NSDictionary*)body{
  319. [RCCTabBarController sendTabEvent:@"bottomTabSelected" controller:viewController body:body];
  320. }
  321. +(void)sendScreenTabPressedEvent:(UIViewController*)viewController body:(NSDictionary*)body{
  322. [RCCTabBarController sendTabEvent:@"bottomTabReselected" controller:viewController body:body];
  323. }
  324. +(void)sendTabEvent:(NSString *)event controller:(UIViewController*)viewController body:(NSDictionary*)body{
  325. if ([viewController.view isKindOfClass:[RCTRootView class]]){
  326. RCTRootView *rootView = (RCTRootView *)viewController.view;
  327. if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
  328. NSString *navigatorID = rootView.appProperties[@"navigatorID"];
  329. NSString *screenInstanceID = rootView.appProperties[@"screenInstanceID"];
  330. NSMutableDictionary *screenDict = [NSMutableDictionary dictionaryWithDictionary:@
  331. {
  332. @"id": event,
  333. @"navigatorID": navigatorID,
  334. @"screenInstanceID": screenInstanceID
  335. }];
  336. if (body) {
  337. [screenDict addEntriesFromDictionary:body];
  338. }
  339. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:screenDict];
  340. }
  341. }
  342. if ([viewController isKindOfClass:[UINavigationController class]]) {
  343. UINavigationController *navigationController = (UINavigationController*)viewController;
  344. UIViewController *topViewController = [navigationController topViewController];
  345. [RCCTabBarController sendTabEvent:event controller:topViewController body:body];
  346. }
  347. }
  348. @end