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