react-native-navigation的迁移库

RCCTabBarController.m 13KB

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