react-native-navigation的迁移库

RCCTabBarController.m 18KB

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