123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- #import "RNNCommandsHandler.h"
- #import "RNNComponentViewController.h"
- #import "RNNErrorHandler.h"
- #import "RNNDefaultOptionsHelper.h"
- #import "UIViewController+RNNOptions.h"
- #import "React/RCTI18nUtil.h"
- #import "UIViewController+LayoutProtocol.h"
- #import "RNNLayoutManager.h"
- #import "UIViewController+Utils.h"
- #import "RNNAssert.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 mergeOptions = @"mergeOptions";
- static NSString* const setDefaultOptions = @"setDefaultOptions";
-
- @interface RNNCommandsHandler() <RNNModalManagerDelegate>
-
- @end
-
- @implementation RNNCommandsHandler {
- RNNControllerFactory *_controllerFactory;
- RNNModalManager* _modalManager;
- RNNOverlayManager* _overlayManager;
- RNNNavigationStackManager* _stackManager;
- RNNEventEmitter* _eventEmitter;
- UIWindow* _mainWindow;
- }
-
- - (instancetype)initWithControllerFactory:(RNNControllerFactory*)controllerFactory eventEmitter:(RNNEventEmitter *)eventEmitter stackManager:(RNNNavigationStackManager *)stackManager modalManager:(RNNModalManager *)modalManager overlayManager:(RNNOverlayManager *)overlayManager mainWindow:(UIWindow *)mainWindow {
- self = [super init];
- _controllerFactory = controllerFactory;
- _eventEmitter = eventEmitter;
- _modalManager = modalManager;
- _modalManager.delegate = self;
- _stackManager = stackManager;
- _overlayManager = overlayManager;
- _mainWindow = mainWindow;
- return self;
- }
-
- #pragma mark - public
-
- - (void)setRoot:(NSDictionary*)layout commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion {
- [self assertReady];
- RNNAssertMainQueue();
-
- if (@available(iOS 9, *)) {
- 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 dismissAllModalsAnimated:NO completion:nil];
-
- UIViewController *vc = [_controllerFactory createLayout:layout[@"root"]];
- vc.waitForRender = [vc.resolveOptionsWithDefault.animations.setRoot.waitForRender getWithDefaultValue:NO];
-
- [vc setReactViewReadyCallback:^{
- _mainWindow.rootViewController = vc;
- [_eventEmitter sendOnNavigationCommandCompletion:setRoot commandId:commandId params:@{@"layout": layout}];
- completion();
- }];
-
- [vc render];
- }
-
- - (void)mergeOptions:(NSString*)componentId options:(NSDictionary*)mergeOptions completion:(RNNTransitionCompletionBlock)completion {
- [self assertReady];
- RNNAssertMainQueue();
-
- UIViewController<RNNLayoutProtocol>* vc = [RNNLayoutManager 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 {
- [self assertReady];
- RNNAssertMainQueue();
-
- RNNNavigationOptions* defaultOptions = [[RNNNavigationOptions alloc] initWithDict:optionsDict];
- [_controllerFactory setDefaultOptions:defaultOptions];
-
- UIViewController *rootViewController = UIApplication.sharedApplication.delegate.window.rootViewController;
- [RNNDefaultOptionsHelper recrusivelySetDefaultOptions:defaultOptions onRootViewController:rootViewController];
-
- completion();
- }
-
- - (void)push:(NSString*)componentId commandId:(NSString*)commandId layout:(NSDictionary*)layout completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
- [self assertReady];
- RNNAssertMainQueue();
-
- UIViewController *newVc = [_controllerFactory createLayout:layout];
- UIViewController *fromVC = [RNNLayoutManager findComponentForId:componentId];
-
- if ([[newVc.resolveOptionsWithDefault.preview.reactTag getWithDefaultValue:@(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 getWithDefaultValue:NO]) {
- [CATransaction begin];
- [CATransaction setCompletionBlock:^{
- [self->_eventEmitter sendOnNavigationCommandCompletion:push commandId:commandId params:@{@"componentId": componentId}];
- completion();
- }];
- [rvc.navigationController pushViewController:newVc animated:YES];
- [CATransaction commit];
- }
- };
-
- CGSize size = CGSizeMake(rootVc.view.frame.size.width, rootVc.view.frame.size.height);
-
- if (newVc.resolveOptionsWithDefault.preview.width.hasValue) {
- size.width = [newVc.resolveOptionsWithDefault.preview.width.get floatValue];
- }
-
- if (newVc.resolveOptionsWithDefault.preview.height.hasValue) {
- size.height = [newVc.resolveOptionsWithDefault.preview.height.get floatValue];
- }
-
- if (newVc.resolveOptionsWithDefault.preview.width.hasValue || newVc.resolveOptionsWithDefault.preview.height.hasValue) {
- newVc.preferredContentSize = size;
- }
- RCTExecuteOnMainQueue(^{
- UIView *view = [[ReactNativeNavigation getBridge].uiManager viewForReactTag:newVc.resolveOptionsWithDefault.preview.reactTag.get];
- [rootVc registerForPreviewingWithDelegate:(id)rootVc sourceView:view];
- });
- }
- } else {
- newVc.waitForRender = newVc.resolveOptionsWithDefault.animations.push.shouldWaitForRender;
- [newVc setReactViewReadyCallback:^{
- [self->_stackManager push:newVc onTop:fromVC animated:[newVc.resolveOptionsWithDefault.animations.push.enable getWithDefaultValue:YES] completion:^{
- [self->_eventEmitter sendOnNavigationCommandCompletion:push commandId:commandId params:@{@"componentId": componentId}];
- completion();
- } 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;
- UIViewController *fromVC = [RNNLayoutManager findComponentForId:componentId];
- RNNNavigationOptions* options = newVC.resolveOptionsWithDefault;
- __weak typeof(RNNEventEmitter*) weakEventEmitter = _eventEmitter;
-
- newVC.waitForRender = ([options.animations.setStackRoot.waitForRender getWithDefaultValue:NO]);
- [newVC setReactViewReadyCallback:^{
- [self->_stackManager setStackChildren:childViewControllers fromViewController:fromVC animated:[options.animations.setStackRoot.enable getWithDefaultValue:YES] completion:^{
- [weakEventEmitter sendOnNavigationCommandCompletion:setStackRoot commandId:commandId params:@{@"componentId": componentId}];
- 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*)[RNNLayoutManager findComponentForId:componentId];
- RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
- [vc overrideOptions:options];
-
- [_stackManager pop:vc animated:[vc.resolveOptionsWithDefault.animations.pop.enable getWithDefaultValue:YES] completion:^{
- [_eventEmitter sendOnNavigationCommandCompletion:pop commandId:commandId params:@{@"componentId": componentId}];
- completion();
- } rejection:rejection];
- }
-
- - (void)popTo:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
- [self assertReady];
- RNNAssertMainQueue();
-
- RNNComponentViewController *vc = (RNNComponentViewController*)[RNNLayoutManager findComponentForId:componentId];
- RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
- [vc overrideOptions:options];
-
- [_stackManager popTo:vc animated:[vc.resolveOptionsWithDefault.animations.pop.enable getWithDefaultValue:YES] completion:^(NSArray *poppedViewControllers) {
- [_eventEmitter sendOnNavigationCommandCompletion:popTo commandId:commandId params:@{@"componentId": componentId}];
- completion();
- } rejection:rejection];
- }
-
- - (void)popToRoot:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RCTPromiseRejectBlock)rejection {
- [self assertReady];
- RNNAssertMainQueue();
-
- RNNComponentViewController *vc = (RNNComponentViewController*)[RNNLayoutManager findComponentForId:componentId];
- RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
- [vc overrideOptions:options];
-
- [CATransaction begin];
- [CATransaction setCompletionBlock:^{
- [_eventEmitter sendOnNavigationCommandCompletion:popToRoot commandId:commandId params:@{@"componentId": componentId}];
- completion();
- }];
-
- [_stackManager popToRoot:vc animated:[vc.resolveOptionsWithDefault.animations.pop.enable getWithDefaultValue: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];
- newVc.waitForRender = [newVc.resolveOptionsWithDefault.animations.showModal.waitForRender getWithDefaultValue:NO];
- [newVc setReactViewReadyCallback:^{
- [_modalManager showModal:newVc animated:[newVc.resolveOptionsWithDefault.animations.showModal.enable getWithDefaultValue:YES] completion:^(NSString *componentId) {
- [self->_eventEmitter sendOnNavigationCommandCompletion:showModal commandId:commandId params:@{@"layout": layout}];
- completion(newVc.layoutInfo.componentId);
- }];
- }];
- [newVc render];
- }
-
- - (void)dismissModal:(NSString*)componentId commandId:(NSString*)commandId mergeOptions:(NSDictionary *)mergeOptions completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
- [self assertReady];
- RNNAssertMainQueue();
-
- UIViewController *modalToDismiss = (UIViewController *)[RNNLayoutManager findComponentForId:componentId];
-
- if (!modalToDismiss.isModal) {
- [RNNErrorHandler reject:reject withErrorCode:1013 errorDescription:@"component is not a modal"];
- return;
- }
-
- RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
- [modalToDismiss.getCurrentChild overrideOptions:options];
-
- [CATransaction begin];
- [CATransaction setCompletionBlock:^{
- [self->_eventEmitter sendOnNavigationCommandCompletion:dismissModal commandId:commandId params:@{@"componentId": componentId}];
- }];
-
- [_modalManager dismissModal:modalToDismiss completion:completion];
-
- [CATransaction commit];
- }
-
- - (void)dismissAllModals:(NSDictionary *)mergeOptions commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion {
- [self assertReady];
- RNNAssertMainQueue();
-
- [CATransaction begin];
- [CATransaction setCompletionBlock:^{
- [_eventEmitter sendOnNavigationCommandCompletion:dismissAllModals commandId:commandId params:@{}];
- completion();
- }];
- RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:mergeOptions];
- [_modalManager dismissAllModalsAnimated:[options.animations.dismissModal.enable getWithDefaultValue:YES] completion:nil];
-
- [CATransaction commit];
- }
-
- - (void)showOverlay:(NSDictionary *)layout commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion {
- [self assertReady];
- RNNAssertMainQueue();
-
- UIViewController* overlayVC = [_controllerFactory createLayout:layout];
- [overlayVC setReactViewReadyCallback:^{UIWindow* overlayWindow = [[RNNOverlayWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- overlayWindow.rootViewController = overlayVC;
- if ([overlayVC.resolveOptionsWithDefault.overlay.handleKeyboardEvents getWithDefaultValue:NO]) {
- [self->_overlayManager showOverlayWindowAsKeyWindow:overlayWindow];
- } else {
- [self->_overlayManager showOverlayWindow:overlayWindow];
- }
-
- [self->_eventEmitter sendOnNavigationCommandCompletion:showOverlay commandId:commandId params:@{@"layout": layout}];
- completion();
-
- }];
-
- [overlayVC render];
- }
-
- - (void)dismissOverlay:(NSString*)componentId commandId:(NSString*)commandId completion:(RNNTransitionCompletionBlock)completion rejection:(RNNTransitionRejectionBlock)reject {
- [self assertReady];
- RNNAssertMainQueue();
-
- UIViewController* viewController = [RNNLayoutManager findComponentForId:componentId];
- if (viewController) {
- [_overlayManager dismissOverlay:viewController];
- [_eventEmitter sendOnNavigationCommandCompletion:dismissOverlay commandId:commandId params:@{@"componentId": componentId}];
- completion();
- } else {
- [RNNErrorHandler reject:reject withErrorCode:1010 errorDescription:@"ComponentId not found"];
- }
- }
-
- #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];
- }
- }
-
- #pragma mark - RNNModalManagerDelegate
-
- - (void)dismissedModal:(UIViewController *)viewController {
- [_eventEmitter sendModalsDismissedEvent:viewController.layoutInfo.componentId numberOfModalsDismissed:@(1)];
- }
-
- - (void)attemptedToDismissModal:(UIViewController *)viewController {
- [_eventEmitter sendModalAttemptedToDismissEvent:viewController.layoutInfo.componentId];
- }
-
- - (void)dismissedMultipleModals:(NSArray *)viewControllers {
- if (viewControllers && viewControllers.count) {
- [_eventEmitter sendModalsDismissedEvent:((UIViewController *)viewControllers.lastObject).layoutInfo.componentId numberOfModalsDismissed:@(viewControllers.count)];
- }
- }
-
- @end
|