forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCCManagerModule.m
executable file
·462 lines (392 loc) · 19.8 KB
/
RCCManagerModule.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
#import "RCCManagerModule.h"
#import "RCCManager.h"
#import <UIKit/UIKit.h>
#import "RCCNavigationController.h"
#import "RCCViewController.h"
#import "RCCDrawerController.h"
#import "RCCLightBox.h"
#import <React/RCTConvert.h>
#import "RCCTabBarController.h"
#import "RCCTheSideBarManagerViewController.h"
#import "RCCNotification.h"
#import "RCTHelpers.h"
#define kSlideDownAnimationDuration 0.35
typedef NS_ENUM(NSInteger, RCCManagerModuleErrorCode)
{
RCCManagerModuleCantCreateControllerErrorCode = -100,
RCCManagerModuleCantFindTabControllerErrorCode = -200,
RCCManagerModuleMissingParamsErrorCode = -300
};
@implementation RCTConvert (RCCManagerModuleErrorCode)
RCT_ENUM_CONVERTER(RCCManagerModuleErrorCode,
(@{@"RCCManagerModuleCantCreateControllerErrorCode": @(RCCManagerModuleCantCreateControllerErrorCode),
@"RCCManagerModuleCantFindTabControllerErrorCode": @(RCCManagerModuleCantFindTabControllerErrorCode),
}), RCCManagerModuleCantCreateControllerErrorCode, integerValue)
@end
@implementation RCTConvert (UIModalPresentationStyle)
RCT_ENUM_CONVERTER(UIModalPresentationStyle,
(@{@"fullScreen": @(UIModalPresentationFullScreen),
@"pageSheet": @(UIModalPresentationPageSheet),
@"formSheet": @(UIModalPresentationFormSheet),
@"currentContext": @(UIModalPresentationCurrentContext),
@"custom": @(UIModalPresentationCustom),
@"overFullScreen": @(UIModalPresentationOverFullScreen),
@"overCurrentContext": @(UIModalPresentationOverCurrentContext),
@"popover": @(UIModalPresentationPopover),
@"none": @(UIModalPresentationNone)
}), UIModalPresentationFullScreen, integerValue)
@end
@implementation RCCManagerModule
RCT_EXPORT_MODULE(RCCManager);
#pragma mark - constatnts export
- (NSDictionary *)constantsToExport
{
return @{
//Error codes
@"RCCManagerModuleCantCreateControllerErrorCode" : @(RCCManagerModuleCantCreateControllerErrorCode),
@"RCCManagerModuleCantFindTabControllerErrorCode" : @(RCCManagerModuleCantFindTabControllerErrorCode),
@"PresentFullScreen": @"fullScreen",
@"PresentPageSheet": @"pageSheet",
@"PresentFormSheet": @"formSheet",
@"PresentCurrentContext": @"currentContext",
@"PresentCustom": @"custom",
@"PresentOverFullScreen": @"overFullScreen",
@"PresentOverCurrentContext": @"overCurrentContext",
@"PresentPopover": @"popover",
@"PresentNone": @"none"
};
}
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
#pragma mark - helper methods
+(UIViewController*)modalPresenterViewControllers:(NSMutableArray*)returnAllPresenters
{
UIViewController *modalPresenterViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
if ((returnAllPresenters != nil) && (modalPresenterViewController != nil))
{
[returnAllPresenters addObject:modalPresenterViewController];
}
while (modalPresenterViewController.presentedViewController != nil)
{
modalPresenterViewController = modalPresenterViewController.presentedViewController;
if (returnAllPresenters != nil)
{
[returnAllPresenters addObject:modalPresenterViewController];
}
}
return modalPresenterViewController;
}
+(UIViewController*)lastModalPresenterViewController
{
return [self modalPresenterViewControllers:nil];
}
+(NSError*)rccErrorWithCode:(NSInteger)code description:(NSString*)description
{
NSString *safeDescription = (description == nil) ? @"" : description;
return [NSError errorWithDomain:@"RCCControllers" code:code userInfo:@{NSLocalizedDescriptionKey: safeDescription}];
}
+(void)handleRCTPromiseRejectBlock:(RCTPromiseRejectBlock)reject error:(NSError*)error
{
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
}
+(void)cancelAllRCCViewControllerReactTouches
{
[[NSNotificationCenter defaultCenter] postNotificationName:RCCViewControllerCancelReactTouchesNotification object:nil];
}
-(void)animateSnapshot:(UIView*)snapshot animationType:(NSString*)animationType resolver:(RCTPromiseResolveBlock)resolve
{
[UIView animateWithDuration:kSlideDownAnimationDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^()
{
if (animationType == nil || [animationType isEqualToString:@"slide-down"])
{
snapshot.transform = CGAffineTransformMakeTranslation(0, snapshot.frame.size.height);
}
else if ([animationType isEqualToString:@"fade"])
{
snapshot.alpha = 0;
}
}
completion:^(BOOL finished)
{
[snapshot removeFromSuperview];
if (resolve != nil)
{
resolve(nil);
}
}];
}
-(void)dismissAllModalPresenters:(NSMutableArray*)allPresentedViewControllers resolver:(RCTPromiseResolveBlock)resolve
{
if (allPresentedViewControllers.count > 0)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^
{
__block NSUInteger counter = 0;
for (UIViewController *viewController in allPresentedViewControllers)
{
counter++;
[[RCCManager sharedIntance] unregisterController:viewController];
if (viewController.presentedViewController != nil)
{
dispatch_semaphore_t dismiss_sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^
{
[viewController dismissViewControllerAnimated:NO completion:^()
{
if (counter == allPresentedViewControllers.count && allPresentedViewControllers.count > 0)
{
[allPresentedViewControllers removeAllObjects];
if (resolve != nil)
{
resolve(nil);
}
}
dispatch_semaphore_signal(dismiss_sema);
}];
});
dispatch_semaphore_wait(dismiss_sema, DISPATCH_TIME_FOREVER);
}
else if (counter == allPresentedViewControllers.count && allPresentedViewControllers.count > 0)
{
[allPresentedViewControllers removeAllObjects];
if (resolve != nil)
{
dispatch_async(dispatch_get_main_queue(), ^
{
resolve(nil);
});
}
}
}
});
}
else if (resolve != nil)
{
resolve(nil);
}
}
#pragma mark - RCT exported methods
RCT_EXPORT_METHOD(
setRootController:(NSDictionary*)layout animationType:(NSString*)animationType globalProps:(NSDictionary*)globalProps)
{
if ([[RCCManager sharedInstance] getBridge].loading) {
[self deferSetRootControllerWhileBridgeLoading:layout animationType:animationType globalProps:globalProps];
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self performSetRootController:layout animationType:animationType globalProps:globalProps];
});
}
/**
* on RN31 there's a timing issue, we must wait for the bridge to finish loading
*/
-(void)deferSetRootControllerWhileBridgeLoading:(NSDictionary*)layout animationType:(NSString*)animationType globalProps:(NSDictionary*)globalProps
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0001 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self setRootController:layout animationType:animationType globalProps:globalProps];
});
}
-(void)performSetRootController:(NSDictionary*)layout animationType:(NSString*)animationType globalProps:(NSDictionary*)globalProps
{
NSMutableDictionary *modifiedGloablProps = [globalProps mutableCopy];
modifiedGloablProps[GLOBAL_SCREEN_ACTION_COMMAND_TYPE] = COMMAND_TYPE_INITIAL_SCREEN;
// first clear the registry to remove any refernece to the previous controllers
[[RCCManager sharedInstance] clearModuleRegistry];
[[RCCManager sharedInstance] setAppStyle:nil];
NSDictionary *appStyle = layout[@"props"][@"appStyle"];
if (appStyle) {
[[RCCManager sharedIntance] setAppStyle:appStyle];
}
// create the new controller
UIViewController *controller = [RCCViewController controllerWithLayout:layout globalProps:modifiedGloablProps bridge:[[RCCManager sharedInstance] getBridge]];
if (controller == nil) return;
id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
BOOL animated = !((appDelegate.window.rootViewController == nil) || ([animationType isEqualToString:@"none"]));
// if we're animating - add a snapshot now
UIViewController *presentedViewController = nil;
UIView *snapshot = nil;
if (animated)
{
if(appDelegate.window.rootViewController.presentedViewController != nil)
presentedViewController = appDelegate.window.rootViewController.presentedViewController;
else
presentedViewController = appDelegate.window.rootViewController;
snapshot = [presentedViewController.view snapshotViewAfterScreenUpdates:NO];
[appDelegate.window.rootViewController.view addSubview:snapshot];
}
// dismiss the modal controllers without animation just so they can be released
[self dismissAllControllers:@"none" resolver:^(id result)
{
// set the new controller as the root
appDelegate.window.rootViewController = controller;
[appDelegate.window makeKeyAndVisible];
[presentedViewController dismissViewControllerAnimated:NO completion:nil];
if (animated)
{
// move the snaphot to the new root and animate it
[appDelegate.window.rootViewController.view addSubview:snapshot];
[self animateSnapshot:snapshot animationType:animationType resolver:nil];
}
} rejecter:nil];
}
RCT_EXPORT_METHOD(
NavigationControllerIOS:(NSString*)controllerId performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams)
{
if (!controllerId || !performAction) return;
RCCNavigationController* controller = [[RCCManager sharedInstance] getControllerWithId:controllerId componentType:@"NavigationControllerIOS"];
if (!controller || ![controller isKindOfClass:[RCCNavigationController class]]) return;
return [controller performAction:performAction actionParams:actionParams bridge:[[RCCManager sharedInstance] getBridge]];
}
RCT_EXPORT_METHOD(
DrawerControllerIOS:(NSString*)controllerId performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams)
{
if (!controllerId || !performAction) return;
id<RCCDrawerDelegate> controller = [[RCCManager sharedIntance] getControllerWithId:controllerId componentType:@"DrawerControllerIOS"];
if (!controller || (![controller isKindOfClass:[RCCDrawerController class]] && ![controller isKindOfClass:[RCCTheSideBarManagerViewController class]])) return;
return [controller performAction:performAction actionParams:actionParams bridge:[[RCCManager sharedIntance] getBridge]];
}
RCT_EXPORT_METHOD(
TabBarControllerIOS:(NSString*)controllerId performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
if (!controllerId || !performAction)
{
[RCCManagerModule handleRCTPromiseRejectBlock:reject
error:[RCCManagerModule rccErrorWithCode:RCCManagerModuleMissingParamsErrorCode description:@"missing params"]];
return;
}
RCCTabBarController* controller = [[RCCManager sharedInstance] getControllerWithId:controllerId componentType:@"TabBarControllerIOS"];
if (!controller || ![controller isKindOfClass:[RCCTabBarController class]])
{
[RCCManagerModule handleRCTPromiseRejectBlock:reject
error:[RCCManagerModule rccErrorWithCode:RCCManagerModuleCantFindTabControllerErrorCode description:@"could not find UITabBarController"]];
return;
}
[controller performAction:performAction actionParams:actionParams bridge:[[RCCManager sharedInstance] getBridge] completion:^(){ resolve(nil); }];
}
RCT_EXPORT_METHOD(
modalShowLightBox:(NSDictionary*)params)
{
[RCCLightBox showWithParams:params];
}
RCT_EXPORT_METHOD(
modalDismissLightBox)
{
[RCCLightBox dismiss];
}
RCT_EXPORT_METHOD(
showController:(NSDictionary*)layout animationType:(NSString*)animationType globalProps:(NSDictionary*)globalProps resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
NSMutableDictionary *modifiedGlobalProps = [globalProps mutableCopy];
modifiedGlobalProps[GLOBAL_SCREEN_ACTION_COMMAND_TYPE] = COMMAND_TYPE_SHOW_MODAL;
UIViewController *controller = [RCCViewController controllerWithLayout:layout globalProps:modifiedGlobalProps bridge:[[RCCManager sharedInstance] getBridge]];
if (controller == nil)
{
[RCCManagerModule handleRCTPromiseRejectBlock:reject
error:[RCCManagerModule rccErrorWithCode:RCCManagerModuleCantCreateControllerErrorCode description:@"could not create controller"]];
return;
}
if (layout[@"props"] && [layout[@"props"] isKindOfClass:[NSDictionary class]] && layout[@"props"][@"style"] && [layout[@"props"][@"style"] isKindOfClass: [NSDictionary class]]) {
NSDictionary *style = layout[@"props"][@"style"];
if (style[@"modalPresentationStyle"] && [style[@"modalPresentationStyle"] isKindOfClass:[NSString class]]) {
NSString *presentationStyle = style[@"modalPresentationStyle"];
UIModalPresentationStyle modalPresentationStyle = [RCTConvert UIModalPresentationStyle:presentationStyle];
controller.modalPresentationStyle = modalPresentationStyle;
}
}
[[RCCManagerModule lastModalPresenterViewController] presentViewController:controller
animated:![animationType isEqualToString:@"none"]
completion:^(){ resolve(nil); }];
}
- (UIViewController *) getVisibleViewControllerFor:(UIViewController *)vc
{
if ([vc isKindOfClass:[UINavigationController class]])
{
return [self getVisibleViewControllerFor:[((UINavigationController*)vc) visibleViewController]];
}
else if ([vc isKindOfClass:[UITabBarController class]])
{
return [self getVisibleViewControllerFor:[((UITabBarController*)vc) selectedViewController]];
}
else if (vc.presentedViewController)
{
return [self getVisibleViewControllerFor:vc.presentedViewController];
}
else
{
return vc;
}
}
RCT_EXPORT_METHOD(getCurrentlyVisibleScreenId:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
UIViewController *rootVC = [UIApplication sharedApplication].delegate.window.rootViewController;
UIViewController *visibleVC = [self getVisibleViewControllerFor:rootVC];
NSString *controllerId = [[RCCManager sharedIntance] getIdForController:visibleVC];
id result = (controllerId != nil) ? @{@"screenId": controllerId} : nil;
resolve(result);
}
-(BOOL)viewControllerIsModal:(UIViewController*)viewController
{
BOOL viewControllerIsModal = (viewController.presentingViewController.presentedViewController == viewController)
|| ((viewController.navigationController != nil) && (viewController.navigationController.presentingViewController.presentedViewController == viewController.navigationController) && (viewController == viewController.navigationController.viewControllers[0]))
|| ([viewController.tabBarController.presentingViewController isKindOfClass:[UITabBarController class]]);
return viewControllerIsModal;
}
RCT_EXPORT_METHOD(
dismissController:(NSString*)animationType resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
UIViewController* vc = [RCCManagerModule lastModalPresenterViewController];
if ([self viewControllerIsModal:vc])
{
[[RCCManager sharedIntance] unregisterController:vc];
[vc dismissViewControllerAnimated:![animationType isEqualToString:@"none"]
completion:^(){ resolve(nil); }];
}
else
{
resolve(nil);
}
}
RCT_EXPORT_METHOD(
dismissAllControllers:(NSString*)animationType resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
if([UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil)
{//if there are no modal - do nothing
resolve(nil);
return;
}
NSMutableArray *allPresentedViewControllers = [NSMutableArray array];
[RCCManagerModule modalPresenterViewControllers:allPresentedViewControllers];
BOOL animated = ![animationType isEqualToString:@"none"];
if (animated)
{
id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
UIView *snapshot = [appDelegate.window snapshotViewAfterScreenUpdates:NO];
[appDelegate.window addSubview:snapshot];
[self dismissAllModalPresenters:allPresentedViewControllers resolver:^(id result)
{
[self animateSnapshot:snapshot animationType:animationType resolver:resolve];
}];
}
else
{
[self dismissAllModalPresenters:allPresentedViewControllers resolver:resolve];
}
}
RCT_EXPORT_METHOD(
showNotification:(NSDictionary*)params resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
[RCCNotification showWithParams:params resolver:resolve rejecter:reject];
}
RCT_EXPORT_METHOD(
dismissNotification:(NSDictionary*)params resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
[RCCNotification dismissWithParams:params resolver:resolve rejecter:reject];
}
RCT_EXPORT_METHOD(
cancelAllReactTouches)
{
[RCCManagerModule cancelAllRCCViewControllerReactTouches];
}
@end