forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCCNavigationController.m
executable file
·537 lines (435 loc) · 24.9 KB
/
RCCNavigationController.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#import "RCCNavigationController.h"
#import "RCCViewController.h"
#import "RCCManager.h"
#import <React/RCTEventDispatcher.h>
#import <React/RCTUIManager.h>
#if __has_include(<React/RCTUIManagerUtils.h>)
#import <React/RCTUIManagerUtils.h>
#endif
#import <React/RCTConvert.h>
#import <React/RCTRootView.h>
#import <objc/runtime.h>
#import "RCCTitleViewHelper.h"
#import "RCCCustomBarButtonItem.h"
#import "UIViewController+Rotation.h"
#import "RCTHelpers.h"
#import "RCTConvert+UIBarButtonSystemItem.h"
@implementation RCCNavigationController {
BOOL _transitioning;
NSMutableArray *_queuedViewControllers;
}
NSString const *CALLBACK_ASSOCIATED_KEY = @"RCCNavigationController.CALLBACK_ASSOCIATED_KEY";
NSString const *CALLBACK_ASSOCIATED_ID = @"RCCNavigationController.CALLBACK_ASSOCIATED_ID";
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [self supportedControllerOrientations];
}
- (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary*)globalProps bridge:(RCTBridge *)bridge {
_queuedViewControllers = [NSMutableArray new];
NSString *component = props[@"component"];
if (!component) return nil;
NSDictionary *passProps = props[@"passProps"];
NSDictionary *navigatorStyle = props[@"style"];
RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:globalProps bridge:bridge];
if (!viewController) return nil;
viewController.controllerId = passProps[@"screenInstanceID"];
NSArray *leftButtons = props[@"leftButtons"];
if (leftButtons) {
[self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
}
NSArray *rightButtons = props[@"rightButtons"];
if (rightButtons) {
[self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
}
self = [super initWithRootViewController:viewController];
if (!self) return nil;
self.delegate = self;
self.navigationBar.translucent = NO; // default
[self processTitleView:viewController
props:props
style:navigatorStyle];
[self setRotation:props];
NSArray* components = props[@"components"];
if (components.count) {
for (NSDictionary* component in components) {
NSMutableDictionary *mutableParams = [[NSMutableDictionary alloc] initWithDictionary:@{@"animated": @(0), @"component": component[@"screen"]}];
[mutableParams addEntriesFromDictionary:component];
[self performAction:@"push" actionParams:mutableParams bridge:bridge];
}
}
return self;
}
- (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge {
BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
// push
if ([performAction isEqualToString:@"push"]) {
NSString *component = actionParams[@"component"];
if (!component) return;
NSMutableDictionary *passProps = [actionParams[@"passProps"] mutableCopy];
passProps[GLOBAL_SCREEN_ACTION_COMMAND_TYPE] = COMMAND_TYPE_PUSH;
passProps[GLOBAL_SCREEN_ACTION_TIMESTAMP] = actionParams[GLOBAL_SCREEN_ACTION_TIMESTAMP];
NSDictionary *navigatorStyle = actionParams[@"style"] ? actionParams[@"style"] : actionParams[@"navigatorStyle"];
NSNumber *keepStyleAcrossPush = [[RCCManager sharedInstance] getAppStyle][@"keepStyleAcrossPush"];
BOOL keepStyleAcrossPushBool = keepStyleAcrossPush ? [keepStyleAcrossPush boolValue] : YES;
if (keepStyleAcrossPushBool) {
if ([self.topViewController isKindOfClass:[RCCViewController class]]) {
RCCViewController *parent = (RCCViewController*)self.topViewController;
NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:parent.navigatorStyle];
// there are a few styles that we don't want to remember from our parent (they should be local)
[mergedStyle removeObjectForKey:@"navBarHidden"];
[mergedStyle removeObjectForKey:@"statusBarHidden"];
[mergedStyle removeObjectForKey:@"navBarHideOnScroll"];
[mergedStyle removeObjectForKey:@"drawUnderNavBar"];
[mergedStyle removeObjectForKey:@"drawUnderTabBar"];
[mergedStyle removeObjectForKey:@"statusBarBlur"];
[mergedStyle removeObjectForKey:@"navBarBlur"];
[mergedStyle removeObjectForKey:@"navBarTranslucent"];
[mergedStyle removeObjectForKey:@"statusBarHideWithNavBar"];
[mergedStyle removeObjectForKey:@"autoAdjustScrollViewInsets"];
[mergedStyle removeObjectForKey:@"statusBarTextColorSchemeSingleScreen"];
[mergedStyle removeObjectForKey:@"disabledBackGesture"];
[mergedStyle removeObjectForKey:@"disabledSimultaneousGesture"];
[mergedStyle removeObjectForKey:@"navBarCustomView"];
[mergedStyle removeObjectForKey:@"navBarComponentAlignment"];
[mergedStyle addEntriesFromDictionary:navigatorStyle];
navigatorStyle = mergedStyle;
}
}
NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:navigatorStyle];
[mergedStyle setValuesForKeysWithDictionary:actionParams];
navigatorStyle = mergedStyle;
RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component passProps:passProps navigatorStyle:navigatorStyle globalProps:nil bridge:bridge];
viewController.controllerId = passProps[@"screenInstanceID"];
[self processTitleView:viewController
props:actionParams
style:navigatorStyle];
NSString *backButtonTitle = actionParams[@"backButtonTitle"];
if (!backButtonTitle) {
NSNumber *hideBackButtonTitle = [[RCCManager sharedInstance] getAppStyle][@"hideBackButtonTitle"];
BOOL hideBackButtonTitleBool = hideBackButtonTitle ? [hideBackButtonTitle boolValue] : NO;
backButtonTitle = hideBackButtonTitleBool ? @"" : backButtonTitle;
}
if (backButtonTitle) {
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:backButtonTitle
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.topViewController.navigationItem.backBarButtonItem = backItem;
} else {
self.topViewController.navigationItem.backBarButtonItem = nil;
}
NSNumber *backButtonHidden = actionParams[@"backButtonHidden"];
BOOL backButtonHiddenBool = backButtonHidden ? [backButtonHidden boolValue] : NO;
if (backButtonHiddenBool) {
viewController.navigationItem.hidesBackButton = YES;
}
NSArray *leftButtons = actionParams[@"leftButtons"];
if (leftButtons) {
[self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
}
NSArray *rightButtons = actionParams[@"rightButtons"];
if (rightButtons) {
[self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
}
NSArray *previewActions = actionParams[@"previewActions"];
NSString *previewViewID = actionParams[@"previewViewID"];
if (previewViewID) {
if ([self.topViewController isKindOfClass:[RCCViewController class]]) {
RCCViewController *topViewController = ((RCCViewController*)self.topViewController);
topViewController.previewController = nil;
[topViewController.navigationController unregisterForPreviewingWithContext:topViewController.previewContext];
viewController.previewActions = previewActions;
viewController.previewCommit = actionParams[@"previewCommit"] ? [actionParams[@"previewCommit"] boolValue] : YES;
NSNumber *previewHeight = actionParams[@"previewHeight"];
if (previewHeight) {
viewController.preferredContentSize = CGSizeMake(viewController.view.frame.size.width, [previewHeight floatValue]);
}
if (topViewController.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
dispatch_async(RCTGetUIManagerQueue(), ^{
[bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
UIView *view = viewRegistry[previewViewID];
topViewController.previewView = view;
topViewController.previewContext = [topViewController registerForPreviewingWithDelegate:(id)topViewController sourceView:view];
}];
});
topViewController.previewController = viewController;
}
return;
}
}
NSString *animationType = actionParams[@"animationType"];
if ([animationType isEqualToString:@"fade"]) {
CATransition *transition = [CATransition animation];
transition.duration = 0.25;
transition.type = kCATransitionFade;
[self.view.layer addAnimation:transition forKey:kCATransition];
[self pushViewController:viewController animated:NO];
} else {
[self pushViewController:viewController animated:animated];
}
return;
}
// pop
if ([performAction isEqualToString:@"pop"]) {
NSString *animationType = actionParams[@"animationType"];
if ([animationType isEqualToString:@"fade"]) {
CATransition *transition = [CATransition animation];
transition.duration = 0.25;
transition.type = kCATransitionFade;
[self.view.layer addAnimation:transition forKey:kCATransition];
[self popViewControllerAnimated:NO];
} else {
[self popViewControllerAnimated:animated];
}
return;
}
// popToRoot
if ([performAction isEqualToString:@"popToRoot"]) {
NSString *animationType = actionParams[@"animationType"];
if ([animationType isEqualToString:@"fade"]) {
CATransition *transition = [CATransition animation];
transition.duration = 0.25;
transition.type = kCATransitionFade;
[self.view.layer addAnimation:transition forKey:kCATransition];
[self popToRootViewControllerAnimated:NO];
} else {
[self popToRootViewControllerAnimated:animated];
}
return;
}
// resetTo
if ([performAction isEqualToString:@"resetTo"]) {
NSArray<UIViewController *> *viewControllers;
NSString *component = actionParams[@"component"];
NSArray<NSDictionary *> *componentConfigs = actionParams[@"components"];
if (component) {
NSMutableDictionary *passProps = [actionParams[@"passProps"] mutableCopy];
passProps[@"commandType"] = @"resetTo";
NSDictionary *style = actionParams[@"style"];
NSArray *leftButtons = actionParams[@"leftButtons"];
NSArray *rightButtons = actionParams[@"rightButtons"];
UIViewController *viewController = [self viewControllerWithComponent:component
props:[passProps copy]
style:style
leftButtons:leftButtons
rightButtons:rightButtons
bridge:bridge];
NSDictionary *navigatorStyle = actionParams[@"style"];
[self processTitleView:viewController
props:actionParams
style:navigatorStyle];
viewControllers = @[viewController];
} else if (componentConfigs) {
NSMutableArray *mutableViewControllers = [NSMutableArray arrayWithCapacity:[componentConfigs count]];
[componentConfigs enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull config, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *component = config[@"component"];
NSMutableDictionary *props = [config[@"passProps"] mutableCopy];
props[@"commandType"] = @"resetTo";
NSDictionary *style = config[@"style"];
NSArray *leftButtons = config[@"leftButtons"];
NSArray *rightButtons = config[@"rightButtons"];
UIViewController *viewController = [self viewControllerWithComponent:component
props:[props copy]
style:style
leftButtons:leftButtons
rightButtons:rightButtons
bridge:bridge];
[self processTitleView:viewController
props:actionParams
style:style];
[mutableViewControllers addObject:viewController];
}];
viewControllers = [mutableViewControllers copy];
}
if (!viewControllers) return;
BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
NSString *animationType = actionParams[@"animationType"];
if ([animationType isEqualToString:@"fade"]) {
CATransition *transition = [CATransition animation];
transition.duration = 0.25;
transition.type = kCATransitionFade;
[self.view.layer addAnimation:transition forKey:kCATransition];
[self setViewControllers:viewControllers animated:NO];
} else {
[self setViewControllers:viewControllers animated:animated];
}
return;
}
// setButtons
if ([performAction isEqualToString:@"setButtons"]) {
NSArray *buttons = actionParams[@"buttons"];
BOOL animated = actionParams[@"animated"] ? [actionParams[@"animated"] boolValue] : YES;
NSString *side = actionParams[@"side"] ? actionParams[@"side"] : @"left";
[self setButtons:buttons viewController:self.topViewController side:side animated:animated];
return;
}
// setTitle
if ([performAction isEqualToString:@"setTitle"] || [performAction isEqualToString:@"setTitleImage"]) {
NSDictionary *navigatorStyle = actionParams[@"style"];
[self processTitleView:self.topViewController
props:actionParams
style:navigatorStyle];
return;
}
// toggleNavBar
if ([performAction isEqualToString:@"setHidden"]) {
NSNumber *animated = actionParams[@"animated"];
BOOL animatedBool = animated ? [animated boolValue] : YES;
NSNumber *setHidden = actionParams[@"hidden"];
BOOL isHiddenBool = setHidden ? [setHidden boolValue] : NO;
RCCViewController *topViewController = ((RCCViewController*)self.topViewController);
topViewController.navigatorStyle[@"navBarHidden"] = setHidden;
[topViewController setNavBarVisibilityChange:animatedBool];
}
// setStyle
if ([performAction isEqualToString:@"setStyle"]) {
NSDictionary *navigatorStyle = actionParams;
// merge the navigatorStyle of our parent
if ([self.topViewController isKindOfClass:[RCCViewController class]]) {
RCCViewController *parent = (RCCViewController*)self.topViewController;
NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:parent.navigatorStyle];
// there are a few styles that we don't want to remember from our parent (they should be local)
[mergedStyle setValuesForKeysWithDictionary:navigatorStyle];
navigatorStyle = mergedStyle;
parent.navigatorStyle = navigatorStyle;
[parent setStyleOnInit];
[parent updateStyle];
}
}
}
- (UIViewController *)viewControllerWithComponent:(NSString *)component
props:(NSDictionary *)props
style:(NSDictionary *)style
leftButtons:(NSArray<NSDictionary *> *)leftButtons
rightButtons:(NSArray<NSDictionary *> *)rightButtons
bridge:(RCTBridge *)bridge {
RCCViewController *viewController = [[RCCViewController alloc] initWithComponent:component
passProps:props
navigatorStyle:style
globalProps:nil
bridge:bridge];
viewController.controllerId = props[@"screenInstanceID"];
viewController.navigationItem.hidesBackButton = YES;
[self processTitleView:viewController
props:props
style:style];
if (leftButtons) {
[self setButtons:leftButtons viewController:viewController side:@"left" animated:NO];
}
if (rightButtons) {
[self setButtons:rightButtons viewController:viewController side:@"right" animated:NO];
}
return viewController;
}
-(void)onButtonPress:(UIBarButtonItem*)barButtonItem {
NSString *callbackId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY);
if (!callbackId) return;
NSString *buttonId = objc_getAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID);
[[[RCCManager sharedInstance] getBridge].eventDispatcher sendAppEventWithName:callbackId body:@
{
@"type": @"NavBarButtonPress",
@"id": buttonId ? buttonId : [NSNull null]
}];
}
-(void)setButtons:(NSArray*)buttons viewController:(UIViewController*)viewController side:(NSString*)side animated:(BOOL)animated {
NSMutableArray *barButtonItems = [NSMutableArray new];
for (NSDictionary *button in buttons) {
NSString *title = button[@"title"];
UIImage *iconImage = nil;
id icon = button[@"icon"];
if (icon) iconImage = [RCTConvert UIImage:icon];
NSString *__nullable component = button[@"component"];
NSString *__nullable systemItemName = button[@"systemItem"];
UIBarButtonSystemItem systemItem = [RCTConvert UIBarButtonSystemItem:systemItemName];
UIBarButtonItem *barButtonItem;
if (iconImage) {
barButtonItem = [[UIBarButtonItem alloc] initWithImage:iconImage style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
}
else if (title) {
barButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(onButtonPress:)];
NSMutableDictionary *buttonTextAttributes = [RCTHelpers textAttributesFromDictionary:button withPrefix:@"button"];
if (buttonTextAttributes.allKeys.count > 0) {
[barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
[barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateHighlighted];
}
} else if (component) {
RCTBridge *bridge = [[RCCManager sharedInstance] getBridge];
barButtonItem = [[RCCCustomBarButtonItem alloc] initWithComponentName:component passProps:button[@"passProps"] bridge:bridge];
} else if (systemItemName) {
barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem target:self action:@selector(onButtonPress:)];
} else continue;
objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY, button[@"onPress"], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[barButtonItems addObject:barButtonItem];
NSString *buttonId = button[@"id"];
if (buttonId) {
objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_ID, buttonId, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
NSNumber *disabled = button[@"disabled"];
BOOL disabledBool = disabled ? [disabled boolValue] : NO;
if (disabledBool) {
[barButtonItem setEnabled:NO];
}
NSNumber *disableIconTintString = button[@"disableIconTint"];
BOOL disableIconTint = disableIconTintString ? [disableIconTintString boolValue] : NO;
if (disableIconTint) {
[barButtonItem setImage:[barButtonItem.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
}
if ([viewController isKindOfClass:[RCCViewController class]]) {
RCCViewController *rccViewController = ((RCCViewController*)viewController);
NSDictionary *navigatorStyle = rccViewController.navigatorStyle;
id disabledButtonColor = navigatorStyle[@"disabledButtonColor"];
if (disabledButtonColor) {
UIColor *color = [RCTConvert UIColor:disabledButtonColor];
[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : color} forState:UIControlStateDisabled];
}
}
NSString *testID = button[@"testID"];
if (testID) {
barButtonItem.accessibilityIdentifier = testID;
}
}
if ([side isEqualToString:@"left"]) {
[viewController.navigationItem setLeftBarButtonItems:barButtonItems animated:animated];
}
if ([side isEqualToString:@"right"]) {
[viewController.navigationItem setRightBarButtonItems:barButtonItems animated:animated];
}
}
-(void)processTitleView:(UIViewController*)viewController
props:(NSDictionary*)props
style:(NSDictionary*)style {
BOOL isSetSubtitleBool = props[@"isSetSubtitle"] ? [props[@"isSetSubtitle"] boolValue] : NO;
RCCTitleViewHelper *titleViewHelper = [[RCCTitleViewHelper alloc] init:viewController
navigationController:self
title:props[@"title"]
subtitle:props[@"subtitle"]
titleImageData:props[@"titleImage"]
isSetSubtitle:isSetSubtitleBool];
[titleViewHelper setup:style];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return [self.topViewController preferredStatusBarStyle];
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (_transitioning) {
NSDictionary *pushDetails =@{ @"viewController": viewController, @"animated": @(animated) };
[_queuedViewControllers addObject:pushDetails];
return;
}
_transitioning = YES;
[super pushViewController:viewController animated:animated];
}
#pragma mark - UINavigationControllerDelegate
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[viewController setNeedsStatusBarAppearanceUpdate];
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
dispatch_async(dispatch_get_main_queue(), ^{
_transitioning = NO;
if ([_queuedViewControllers count] > 0) {
NSDictionary *toPushDetails = [_queuedViewControllers firstObject];
[_queuedViewControllers removeObjectAtIndex:0];
[self pushViewController:toPushDetails[@"viewController"] animated:[toPushDetails[@"animated"] boolValue]];
}
});
}
@end