-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
RNNCommandsHandler.m
513 lines (437 loc) · 20.6 KB
/
RNNCommandsHandler.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
#import "RNNCommandsHandler.h"
#import "AnimationObserver.h"
#import "RNNAssert.h"
#import "RNNComponentViewController.h"
#import "RNNConvert.h"
#import "RNNDefaultOptionsHelper.h"
#import "RNNErrorHandler.h"
#import "React/RCTI18nUtil.h"
#import "UINavigationController+RNNCommands.h"
#import "UIViewController+RNNOptions.h"
static NSString *const setRoot = @"setRoot";
static NSString *const setStackRoot = @"setStackRoot";
static NSString *const push = @"push";
static NSString *const preview = @"preview";
static NSString *const pop = @"pop";
static NSString *const popTo = @"popTo";
static NSString *const popToRoot = @"popToRoot";
static NSString *const showModal = @"showModal";
static NSString *const dismissModal = @"dismissModal";
static NSString *const dismissAllModals = @"dismissAllModals";
static NSString *const showOverlay = @"showOverlay";
static NSString *const dismissOverlay = @"dismissOverlay";
static NSString *const dismissAllOverlays = @"dismissAllOverlays";
static NSString *const mergeOptions = @"mergeOptions";
static NSString *const setDefaultOptions = @"setDefaultOptions";
@interface RNNCommandsHandler ()
@end
@implementation RNNCommandsHandler {
RNNControllerFactory *_controllerFactory;
RNNLayoutManager *_layoutManager;
RNNModalManager *_modalManager;
RNNOverlayManager *_overlayManager;
RNNEventEmitter *_eventEmitter;
UIWindow *_mainWindow;
RNNSetRootAnimator *_setRootAnimator;
}
- (instancetype)initWithControllerFactory:(RNNControllerFactory *)controllerFactory
layoutManager:(RNNLayoutManager *)layoutManager
eventEmitter:(RNNEventEmitter *)eventEmitter
modalManager:(RNNModalManager *)modalManager
overlayManager:(RNNOverlayManager *)overlayManager
setRootAnimator:(RNNSetRootAnimator *)setRootAnimator
mainWindow:(UIWindow *)mainWindow {
self = [super init];
_controllerFactory = controllerFactory;
_layoutManager = layoutManager;
_eventEmitter = eventEmitter;
_modalManager = modalManager;
_overlayManager = overlayManager;
_setRootAnimator = setRootAnimator;
_mainWindow = mainWindow;
return self;
}
#pragma mark - public
- (void)setRoot:(NSDictionary *)layout
commandId:(NSString *)commandId
completion:(RNNTransitionWithComponentIdCompletionBlock)completion {
[self assertReady];
RNNAssertMainQueue();
if (_controllerFactory.defaultOptions.layout.direction.hasValue) {
if ([_controllerFactory.defaultOptions.layout.direction.get isEqualToString:@"rtl"]) {
[[RCTI18nUtil sharedInstance] allowRTL:YES];
[[RCTI18nUtil sharedInstance] forceRTL:YES];
[[UIView appearance]
setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
[[UINavigationBar appearance]
setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
} else {
[[RCTI18nUtil sharedInstance] allowRTL:NO];
[[RCTI18nUtil sharedInstance] forceRTL:NO];
[[UIView appearance]
setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
[[UINavigationBar appearance]
setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
}
}
[_modalManager reset];
UIViewController *vc = [_controllerFactory createLayout:layout[@"root"]];
[_layoutManager addPendingViewController:vc];
RNNNavigationOptions *optionsWithDefault = vc.resolveOptionsWithDefault;
vc.waitForRender = [optionsWithDefault.animations.setRoot.waitForRender withDefault:NO];
__weak UIViewController *weakVC = vc;
[vc setReactViewReadyCallback:^{
[self->_mainWindow.rootViewController destroy];
self->_mainWindow.rootViewController = weakVC;
[self->_setRootAnimator
animate:self->_mainWindow
duration:[optionsWithDefault.animations.setRoot.alpha.duration withDefault:0]
completion:^{
[self->_layoutManager removePendingViewController:weakVC];
[self->_eventEmitter sendOnNavigationCommandCompletion:setRoot commandId:commandId];
completion(weakVC.layoutInfo.componentId);
}];
}];
[vc render];
}
- (void)mergeOptions:(NSString *)componentId
options:(NSDictionary *)mergeOptions
completion:(RNNTransitionCompletionBlock)completion {
[self assertReady];
RNNAssertMainQueue();
UIViewController<RNNLayoutProtocol> *vc = [_layoutManager findComponentForId:componentId];
RNNNavigationOptions *newOptions = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
if ([vc conformsToProtocol:@protocol(RNNLayoutProtocol)] ||
[vc isKindOfClass:[RNNComponentViewController class]]) {
[CATransaction begin];
[CATransaction setCompletionBlock:completion];
[vc mergeOptions:newOptions];
[CATransaction commit];
}
}
- (void)setDefaultOptions:(NSDictionary *)optionsDict
completion:(RNNTransitionCompletionBlock)completion {
RNNAssertMainQueue();
RNNNavigationOptions *defaultOptions = [[RNNNavigationOptions alloc] initWithDict:optionsDict];
[_controllerFactory setDefaultOptions:defaultOptions];
UIViewController *rootViewController =
UIApplication.sharedApplication.delegate.window.rootViewController;
[RNNDefaultOptionsHelper recursivelySetDefaultOptions:defaultOptions
onRootViewController:rootViewController];
completion();
}
- (void)push:(NSString *)componentId
commandId:(NSString *)commandId
layout:(NSDictionary *)layout
completion:(RNNTransitionWithComponentIdCompletionBlock)completion
rejection:(RCTPromiseRejectBlock)rejection {
[self assertReady];
RNNAssertMainQueue();
UIViewController *newVc = [_controllerFactory createLayout:layout];
[_layoutManager addPendingViewController:newVc];
UIViewController *fromVC = [_layoutManager findComponentForId:componentId];
RNNNavigationOptions *optionsWithDefault = newVc.resolveOptionsWithDefault;
if ([[optionsWithDefault.preview.reactTag withDefault:@(0)] floatValue] > 0) {
if ([fromVC isKindOfClass:[RNNComponentViewController class]]) {
RNNComponentViewController *rootVc = (RNNComponentViewController *)fromVC;
rootVc.previewController = newVc;
[newVc render];
rootVc.previewCallback = ^(UIViewController *vcc) {
RNNComponentViewController *rvc = (RNNComponentViewController *)vcc;
[self->_eventEmitter sendOnPreviewCompleted:componentId
previewComponentId:newVc.layoutInfo.componentId];
if ([newVc.resolveOptionsWithDefault.preview.commit withDefault:NO]) {
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[self->_layoutManager removePendingViewController:newVc];
[self->_eventEmitter sendOnNavigationCommandCompletion:push
commandId:commandId];
completion(newVc.layoutInfo.componentId);
}];
[rvc.navigationController pushViewController:newVc animated:YES];
[CATransaction commit];
}
};
CGSize size = CGSizeMake(rootVc.view.frame.size.width, rootVc.view.frame.size.height);
if (optionsWithDefault.preview.width.hasValue) {
size.width = [newVc.resolveOptionsWithDefault.preview.width.get floatValue];
}
if (optionsWithDefault.preview.height.hasValue) {
size.height = [newVc.resolveOptionsWithDefault.preview.height.get floatValue];
}
if (optionsWithDefault.preview.width.hasValue ||
optionsWithDefault.preview.height.hasValue) {
newVc.preferredContentSize = size;
}
RCTExecuteOnMainQueue(^{
UIView *view = [[ReactNativeNavigation getBridge].uiManager
viewForReactTag:optionsWithDefault.preview.reactTag.get];
[rootVc registerForPreviewingWithDelegate:(id)rootVc sourceView:view];
});
}
} else {
BOOL animated = [optionsWithDefault.animations.push.enable withDefault:YES];
BOOL waitForRender = optionsWithDefault.animations.push.shouldWaitForRender;
newVc.waitForRender = waitForRender;
__weak UIViewController *weakNewVC = newVc;
[newVc setReactViewReadyCallback:^{
if (animated && !waitForRender)
[[AnimationObserver sharedObserver] beginAnimation];
[fromVC.stack push:weakNewVC
onTop:fromVC
animated:animated
completion:^{
[self->_layoutManager removePendingViewController:weakNewVC];
[self->_eventEmitter sendOnNavigationCommandCompletion:push
commandId:commandId];
completion(weakNewVC.layoutInfo.componentId);
}
rejection:rejection];
}];
[newVc render];
}
}
- (void)setStackRoot:(NSString *)componentId
commandId:(NSString *)commandId
children:(NSArray *)children
completion:(RNNTransitionCompletionBlock)completion
rejection:(RCTPromiseRejectBlock)rejection {
[self assertReady];
RNNAssertMainQueue();
NSArray<UIViewController *> *childViewControllers =
[_controllerFactory createChildrenLayout:children];
for (UIViewController<RNNLayoutProtocol> *viewController in childViewControllers) {
if (![viewController isEqual:childViewControllers.lastObject]) {
[viewController render];
}
}
UIViewController *newVC = childViewControllers.lastObject;
[_layoutManager addPendingViewController:newVC];
UIViewController *fromVC = [_layoutManager findComponentForId:componentId];
RNNNavigationOptions *options = newVC.resolveOptionsWithDefault;
newVC.waitForRender = ([options.animations.setStackRoot.waitForRender withDefault:NO]);
__weak typeof(RNNEventEmitter *) weakEventEmitter = _eventEmitter;
__weak UIViewController *weakNewVC = newVC;
[newVC setReactViewReadyCallback:^{
[fromVC.stack setStackChildren:childViewControllers
fromViewController:fromVC
animated:[options.animations.setStackRoot.enable withDefault:YES]
completion:^{
[self->_layoutManager removePendingViewController:weakNewVC];
[weakEventEmitter sendOnNavigationCommandCompletion:setStackRoot
commandId:commandId];
completion();
}
rejection:rejection];
}];
[newVC render];
}
- (void)pop:(NSString *)componentId
commandId:(NSString *)commandId
mergeOptions:(NSDictionary *)mergeOptions
completion:(RNNTransitionCompletionBlock)completion
rejection:(RCTPromiseRejectBlock)rejection {
[self assertReady];
RNNAssertMainQueue();
RNNComponentViewController *vc =
(RNNComponentViewController *)[_layoutManager findComponentForId:componentId];
if (vc) {
RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
[vc mergeOptions:options];
[vc.stack popAnimated:[vc.resolveOptionsWithDefault.animations.pop.enable withDefault:YES]
completion:^{
[self->_eventEmitter sendOnNavigationCommandCompletion:pop
commandId:commandId];
completion();
}
rejection:rejection];
} else {
[RNNErrorHandler
reject:rejection
withErrorCode:1012
errorDescription:
[NSString stringWithFormat:@"Popping component failed - componentId '%@' not found",
componentId]];
}
}
- (void)popTo:(NSString *)componentId
commandId:(NSString *)commandId
mergeOptions:(NSDictionary *)mergeOptions
completion:(RNNTransitionCompletionBlock)completion
rejection:(RCTPromiseRejectBlock)rejection {
[self assertReady];
RNNAssertMainQueue();
RNNComponentViewController *vc =
(RNNComponentViewController *)[_layoutManager findComponentForId:componentId];
RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
[vc mergeOptions:options];
if (vc) {
[vc.stack popTo:vc
animated:[vc.resolveOptionsWithDefault.animations.pop.enable withDefault:YES]
completion:^(NSArray *poppedViewControllers) {
[self->_eventEmitter sendOnNavigationCommandCompletion:popTo commandId:commandId];
completion();
}
rejection:rejection];
} else {
[RNNErrorHandler
reject:rejection
withErrorCode:1012
errorDescription:
[NSString stringWithFormat:@"PopTo component failed - componentId '%@' not found",
componentId]];
}
}
- (void)popToRoot:(NSString *)componentId
commandId:(NSString *)commandId
mergeOptions:(NSDictionary *)mergeOptions
completion:(RNNTransitionCompletionBlock)completion
rejection:(RCTPromiseRejectBlock)rejection {
[self assertReady];
RNNAssertMainQueue();
RNNComponentViewController *vc =
(RNNComponentViewController *)[_layoutManager findComponentForId:componentId];
RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
[vc mergeOptions:options];
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[self->_eventEmitter sendOnNavigationCommandCompletion:popToRoot commandId:commandId];
completion();
}];
[vc.stack popToRoot:vc
animated:[vc.resolveOptionsWithDefault.animations.pop.enable withDefault:YES]
completion:^(NSArray *poppedViewControllers) {
}
rejection:^(NSString *code, NSString *message, NSError *error){
}];
[CATransaction commit];
}
- (void)showModal:(NSDictionary *)layout
commandId:(NSString *)commandId
completion:(RNNTransitionWithComponentIdCompletionBlock)completion {
[self assertReady];
RNNAssertMainQueue();
UIViewController *newVc = [_controllerFactory createLayout:layout];
RNNNavigationOptions *withDefault = newVc.resolveOptionsWithDefault;
[_layoutManager addPendingViewController:newVc];
__weak UIViewController *weakNewVC = newVc;
BOOL animated = [withDefault.animations.showModal.enter.enable withDefault:YES];
BOOL waitForRender = [withDefault.animations.showModal.enter shouldWaitForRender];
newVc.waitForRender = waitForRender;
newVc.modalPresentationStyle = [RNNConvert
UIModalPresentationStyle:[withDefault.modalPresentationStyle withDefault:@"default"]];
newVc.modalTransitionStyle = [RNNConvert
UIModalTransitionStyle:[withDefault.modalTransitionStyle withDefault:@"coverVertical"]];
if (animated && !waitForRender)
[[AnimationObserver sharedObserver] beginAnimation];
[newVc setReactViewReadyCallback:^{
[self->_modalManager showModal:weakNewVC
animated:animated
completion:^(NSString *componentId) {
[self->_layoutManager removePendingViewController:weakNewVC];
[self->_eventEmitter sendOnNavigationCommandCompletion:showModal
commandId:commandId];
completion(weakNewVC.layoutInfo.componentId);
}];
}];
[newVc render];
}
- (void)dismissModal:(NSString *)componentId
commandId:(NSString *)commandId
mergeOptions:(NSDictionary *)mergeOptions
completion:(RNNTransitionWithComponentIdCompletionBlock)completion
rejection:(RNNTransitionRejectionBlock)reject {
[self assertReady];
RNNAssertMainQueue();
UIViewController *modalToDismiss =
(UIViewController *)[_layoutManager findComponentForId:componentId];
if (!modalToDismiss.isModal) {
[RNNErrorHandler
reject:reject
withErrorCode:1013
errorDescription:[NSString stringWithFormat:@"component with id: '%@' is not a modal",
componentId]];
return;
}
RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
[modalToDismiss.presentedComponentViewController mergeOptions:options];
[_modalManager
dismissModal:modalToDismiss
animated:[modalToDismiss.resolveOptionsWithDefault.animations.dismissModal.exit.enable
withDefault:YES]
completion:^{
[self->_eventEmitter sendOnNavigationCommandCompletion:dismissModal
commandId:commandId];
completion(modalToDismiss.topMostViewController.layoutInfo.componentId);
}];
}
- (void)dismissAllModals:(NSDictionary *)mergeOptions
commandId:(NSString *)commandId
completion:(RNNTransitionCompletionBlock)completion {
[self assertReady];
RNNAssertMainQueue();
RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
[_modalManager
dismissAllModalsAnimated:[options.animations.dismissModal.exit.enable withDefault:YES]
completion:^{
[self->_eventEmitter sendOnNavigationCommandCompletion:dismissAllModals
commandId:commandId];
completion();
}];
}
- (void)showOverlay:(NSDictionary *)layout
commandId:(NSString *)commandId
completion:(RNNTransitionWithComponentIdCompletionBlock)completion {
[self assertReady];
RNNAssertMainQueue();
UIViewController *overlayVC = [_controllerFactory createLayout:layout];
[_layoutManager addPendingViewController:overlayVC];
__weak UIViewController *weakOverlayVC = overlayVC;
[overlayVC setReactViewReadyCallback:^{
UIWindow *overlayWindow =
[[RNNOverlayWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
overlayWindow.rootViewController = weakOverlayVC;
if ([weakOverlayVC.resolveOptionsWithDefault.overlay.handleKeyboardEvents withDefault:NO]) {
[self->_overlayManager showOverlayWindowAsKeyWindow:overlayWindow];
} else {
[self->_overlayManager showOverlayWindow:overlayWindow];
}
[self->_layoutManager removePendingViewController:weakOverlayVC];
[self->_eventEmitter sendOnNavigationCommandCompletion:showOverlay commandId:commandId];
completion(weakOverlayVC.layoutInfo.componentId);
}];
[overlayVC render];
}
- (void)dismissOverlay:(NSString *)componentId
commandId:(NSString *)commandId
completion:(RNNTransitionCompletionBlock)completion
rejection:(RNNTransitionRejectionBlock)reject {
[self assertReady];
RNNAssertMainQueue();
UIViewController *viewController = [_layoutManager findComponentForId:componentId];
if (viewController) {
[_overlayManager dismissOverlay:viewController];
[_eventEmitter sendOnNavigationCommandCompletion:dismissOverlay commandId:commandId];
completion();
} else {
[RNNErrorHandler reject:reject
withErrorCode:1010
errorDescription:@"ComponentId not found"];
}
}
- (void)dismissAllOverlays:(NSString *)commandId {
[self assertReady];
RNNAssertMainQueue();
[_overlayManager dismissAllOverlays];
[self->_eventEmitter sendOnNavigationCommandCompletion:dismissAllOverlays commandId:commandId];
}
#pragma mark - private
- (void)assertReady {
if (!self.readyToReceiveCommands) {
[[NSException exceptionWithName:@"BridgeNotLoadedError"
reason:@"Bridge not yet loaded! Send commands after "
@"Navigation.events().onAppLaunched() has been called."
userInfo:nil] raise];
}
}
@end