react-native-navigation的迁移库

RCCTabBarController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. UIColor *labelColor = nil;
  58. UIColor *selectedLabelColor = nil;
  59. NSDictionary *tabsStyle = props[@"style"];
  60. if (tabsStyle)
  61. {
  62. NSString *tabBarButtonColor = tabsStyle[@"tabBarButtonColor"];
  63. if (tabBarButtonColor)
  64. {
  65. UIColor *color = tabBarButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarButtonColor] : nil;
  66. self.tabBar.tintColor = color;
  67. buttonColor = color;
  68. selectedButtonColor = color;
  69. }
  70. NSString *tabBarSelectedButtonColor = tabsStyle[@"tabBarSelectedButtonColor"];
  71. if (tabBarSelectedButtonColor)
  72. {
  73. UIColor *color = tabBarSelectedButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarSelectedButtonColor] : nil;
  74. self.tabBar.tintColor = color;
  75. selectedButtonColor = color;
  76. }
  77. NSString *tabBarLabelColor = tabsStyle[@"tabBarLabelColor"];
  78. if(tabBarLabelColor) {
  79. UIColor *color = tabBarLabelColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarLabelColor] : nil;
  80. labelColor = color;
  81. }
  82. NSString *tabBarSelectedLabelColor = tabsStyle[@"tabBarSelectedLabelColor"];
  83. if(tabBarLabelColor) {
  84. UIColor *color = tabBarSelectedLabelColor != (id)[NSNull null] ? [RCTConvert UIColor:
  85. tabBarSelectedLabelColor] : nil;
  86. selectedLabelColor = color;
  87. }
  88. NSString *tabBarBackgroundColor = tabsStyle[@"tabBarBackgroundColor"];
  89. if (tabBarBackgroundColor)
  90. {
  91. UIColor *color = tabBarBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarBackgroundColor] : nil;
  92. self.tabBar.barTintColor = color;
  93. }
  94. NSString *tabBarTranslucent = tabsStyle[@"tabBarTranslucent"];
  95. if (tabBarTranslucent)
  96. {
  97. self.tabBar.translucent = [tabBarTranslucent boolValue] ? YES : NO;
  98. }
  99. NSString *tabBarHideShadow = tabsStyle[@"tabBarHideShadow"];
  100. if (tabBarHideShadow)
  101. {
  102. self.tabBar.clipsToBounds = [tabBarHideShadow boolValue] ? YES : NO;
  103. }
  104. }
  105. NSMutableArray *viewControllers = [NSMutableArray array];
  106. // go over all the tab bar items
  107. for (NSDictionary *tabItemLayout in children)
  108. {
  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. {
  125. iconImage = [RCTConvert UIImage:icon];
  126. if (buttonColor)
  127. {
  128. iconImage = [[self image:iconImage withColor:buttonColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  129. }
  130. }
  131. UIImage *iconImageSelected = nil;
  132. id selectedIcon = tabItemLayout[@"props"][@"selectedIcon"];
  133. if (selectedIcon) {
  134. iconImageSelected = [RCTConvert UIImage:selectedIcon];
  135. } else {
  136. iconImageSelected = [RCTConvert UIImage:icon];
  137. }
  138. viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:iconImage tag:0];
  139. viewController.tabBarItem.accessibilityIdentifier = tabItemLayout[@"props"][@"testID"];
  140. viewController.tabBarItem.selectedImage = iconImageSelected;
  141. id imageInsets = tabItemLayout[@"props"][@"iconInsets"];
  142. if (imageInsets && imageInsets != (id)[NSNull null])
  143. {
  144. id topInset = imageInsets[@"top"];
  145. id leftInset = imageInsets[@"left"];
  146. id bottomInset = imageInsets[@"bottom"];
  147. id rightInset = imageInsets[@"right"];
  148. CGFloat top = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:topInset] : 0;
  149. CGFloat left = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:leftInset] : 0;
  150. CGFloat bottom = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:bottomInset] : 0;
  151. CGFloat right = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:rightInset] : 0;
  152. viewController.tabBarItem.imageInsets = UIEdgeInsetsMake(top, left, bottom, right);
  153. }
  154. NSMutableDictionary *unselectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarText" baseFont:[UIFont systemFontOfSize:10]];
  155. if (!unselectedAttributes[NSForegroundColorAttributeName] && labelColor) {
  156. unselectedAttributes[NSForegroundColorAttributeName] = labelColor;
  157. }
  158. [viewController.tabBarItem setTitleTextAttributes:unselectedAttributes forState:UIControlStateNormal];
  159. NSMutableDictionary *selectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarSelectedText" baseFont:[UIFont systemFontOfSize:10]];
  160. if (!selectedAttributes[NSForegroundColorAttributeName] && selectedLabelColor) {
  161. selectedAttributes[NSForegroundColorAttributeName] = selectedLabelColor;
  162. }
  163. [viewController.tabBarItem setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
  164. // create badge
  165. NSObject *badge = tabItemLayout[@"props"][@"badge"];
  166. if (badge == nil || [badge isEqual:[NSNull null]])
  167. {
  168. viewController.tabBarItem.badgeValue = nil;
  169. }
  170. else
  171. {
  172. viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
  173. }
  174. [viewControllers addObject:viewController];
  175. }
  176. // replace the tabs
  177. self.viewControllers = viewControllers;
  178. [self setRotation:props];
  179. return self;
  180. }
  181. - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge completion:(void (^)(void))completion
  182. {
  183. if ([performAction isEqualToString:@"setBadge"])
  184. {
  185. UIViewController *viewController = nil;
  186. NSNumber *tabIndex = actionParams[@"tabIndex"];
  187. if (tabIndex)
  188. {
  189. int i = (int)[tabIndex integerValue];
  190. if ([self.viewControllers count] > i)
  191. {
  192. viewController = [self.viewControllers objectAtIndex:i];
  193. }
  194. }
  195. NSString *contentId = actionParams[@"contentId"];
  196. NSString *contentType = actionParams[@"contentType"];
  197. if (contentId && contentType)
  198. {
  199. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  200. }
  201. if (viewController)
  202. {
  203. NSObject *badge = actionParams[@"badge"];
  204. if (badge == nil || [badge isEqual:[NSNull null]])
  205. {
  206. viewController.tabBarItem.badgeValue = nil;
  207. }
  208. else
  209. {
  210. viewController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@", badge];
  211. }
  212. }
  213. }
  214. if ([performAction isEqualToString:@"switchTo"])
  215. {
  216. UIViewController *viewController = nil;
  217. NSNumber *tabIndex = actionParams[@"tabIndex"];
  218. if (tabIndex)
  219. {
  220. int i = (int)[tabIndex integerValue];
  221. if ([self.viewControllers count] > i)
  222. {
  223. viewController = [self.viewControllers objectAtIndex:i];
  224. }
  225. }
  226. NSString *contentId = actionParams[@"contentId"];
  227. NSString *contentType = actionParams[@"contentType"];
  228. if (contentId && contentType)
  229. {
  230. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  231. }
  232. if (viewController)
  233. {
  234. [self setSelectedViewController:viewController];
  235. }
  236. }
  237. if ([performAction isEqualToString:@"setTabButton"])
  238. {
  239. UIViewController *viewController = nil;
  240. NSNumber *tabIndex = actionParams[@"tabIndex"];
  241. if (tabIndex)
  242. {
  243. int i = (int)[tabIndex integerValue];
  244. if ([self.viewControllers count] > i)
  245. {
  246. viewController = [self.viewControllers objectAtIndex:i];
  247. }
  248. }
  249. NSString *contentId = actionParams[@"contentId"];
  250. NSString *contentType = actionParams[@"contentType"];
  251. if (contentId && contentType)
  252. {
  253. viewController = [[RCCManager sharedInstance] getControllerWithId:contentId componentType:contentType];
  254. }
  255. if (viewController)
  256. {
  257. UIImage *iconImage = nil;
  258. id icon = actionParams[@"icon"];
  259. if (icon && icon != (id)[NSNull null])
  260. {
  261. iconImage = [RCTConvert UIImage:icon];
  262. iconImage = [[self image:iconImage withColor:self.tabBar.tintColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  263. viewController.tabBarItem.image = iconImage;
  264. }
  265. UIImage *iconImageSelected = nil;
  266. id selectedIcon = actionParams[@"selectedIcon"];
  267. if (selectedIcon && selectedIcon != (id)[NSNull null])
  268. {
  269. iconImageSelected = [RCTConvert UIImage:selectedIcon];
  270. viewController.tabBarItem.selectedImage = iconImageSelected;
  271. }
  272. }
  273. }
  274. if ([performAction isEqualToString:@"setTabBarHidden"])
  275. {
  276. BOOL hidden = [actionParams[@"hidden"] boolValue];
  277. [UIView animateWithDuration: ([actionParams[@"animated"] boolValue] ? 0.45 : 0)
  278. delay: 0
  279. usingSpringWithDamping: 0.75
  280. initialSpringVelocity: 0
  281. options: (hidden ? UIViewAnimationOptionCurveEaseIn : UIViewAnimationOptionCurveEaseOut)
  282. animations:^()
  283. {
  284. self.tabBar.transform = hidden ? CGAffineTransformMakeTranslation(0, self.tabBar.frame.size.height) : CGAffineTransformIdentity;
  285. }
  286. completion:^(BOOL finished)
  287. {
  288. if (completion != nil)
  289. {
  290. completion();
  291. }
  292. }];
  293. return;
  294. }
  295. else if (completion != nil)
  296. {
  297. completion();
  298. }
  299. }
  300. +(void)sendScreenTabChangedEvent:(UIViewController*)viewController body:(NSDictionary*)body{
  301. [RCCTabBarController sendTabEvent:@"bottomTabSelected" controller:viewController body:body];
  302. }
  303. +(void)sendScreenTabPressedEvent:(UIViewController*)viewController body:(NSDictionary*)body{
  304. [RCCTabBarController sendTabEvent:@"bottomTabReselected" controller:viewController body:body];
  305. }
  306. +(void)sendTabEvent:(NSString *)event controller:(UIViewController*)viewController body:(NSDictionary*)body{
  307. if ([viewController.view isKindOfClass:[RCTRootView class]]){
  308. RCTRootView *rootView = (RCTRootView *)viewController.view;
  309. if (rootView.appProperties && rootView.appProperties[@"navigatorEventID"]) {
  310. NSString *navigatorID = rootView.appProperties[@"navigatorID"];
  311. NSString *screenInstanceID = rootView.appProperties[@"screenInstanceID"];
  312. NSMutableDictionary *screenDict = [NSMutableDictionary dictionaryWithDictionary:@
  313. {
  314. @"id": event,
  315. @"navigatorID": navigatorID,
  316. @"screenInstanceID": screenInstanceID
  317. }];
  318. if (body) {
  319. [screenDict addEntriesFromDictionary:body];
  320. }
  321. [[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:rootView.appProperties[@"navigatorEventID"] body:screenDict];
  322. }
  323. }
  324. if ([viewController isKindOfClass:[UINavigationController class]]) {
  325. UINavigationController *navigationController = (UINavigationController*)viewController;
  326. UIViewController *topViewController = [navigationController topViewController];
  327. [RCCTabBarController sendTabEvent:event controller:topViewController body:body];
  328. }
  329. }
  330. @end