123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- #import "RNNRootViewController.h"
- #import <React/RCTConvert.h>
- #import "RNNAnimator.h"
- #import "RNNPushAnimation.h"
- #import "RNNReactView.h"
- #import "RNNParentProtocol.h"
- #import "RNNAnimationsTransitionDelegate.h"
-
- @implementation RNNRootViewController
-
- @synthesize previewCallback;
-
- - (instancetype)initWithLayoutInfo:(RNNLayoutInfo *)layoutInfo rootViewCreator:(id<RNNRootViewCreator>)creator eventEmitter:(RNNEventEmitter *)eventEmitter presenter:(RNNViewControllerPresenter *)presenter options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions {
- self = [super init];
-
- self.layoutInfo = layoutInfo;
- self.creator = creator;
-
- self.eventEmitter = eventEmitter;
- self.presenter = presenter;
- [self.presenter bindViewController:self];
- self.options = options;
- self.defaultOptions = defaultOptions;
-
- [self.presenter applyOptionsOnInit:self.resolveOptions];
-
- self.animator = [[RNNAnimator alloc] initWithTransitionOptions:self.resolveOptions.customTransition];
-
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(onJsReload)
- name:RCTJavaScriptWillStartLoadingNotification
- object:nil];
- self.navigationController.delegate = self;
-
- return self;
- }
-
- - (instancetype)initExternalComponentWithLayoutInfo:(RNNLayoutInfo *)layoutInfo eventEmitter:(RNNEventEmitter *)eventEmitter presenter:(RNNViewControllerPresenter *)presenter options:(RNNNavigationOptions *)options defaultOptions:(RNNNavigationOptions *)defaultOptions {
- self = [self initWithLayoutInfo:layoutInfo rootViewCreator:nil eventEmitter:eventEmitter presenter:presenter options:options defaultOptions:defaultOptions];
- return self;
- }
-
- - (void)bindViewController:(UIViewController *)viewController {
- [self addChildViewController:viewController];
- [self.view addSubview:viewController.view];
- [viewController didMoveToParentViewController:self];
- }
-
- - (void)willMoveToParentViewController:(UIViewController *)parent {
- if (parent) {
- [_presenter applyOptionsOnWillMoveToParentViewController:self.resolveOptions];
- }
- }
-
- - (void)mergeOptions:(RNNNavigationOptions *)options {
- [_presenter mergeOptions:options currentOptions:self.options defaultOptions:self.defaultOptions];
- [((UIViewController<RNNLayoutProtocol> *)self.parentViewController) mergeOptions:options];
- }
-
- - (void)overrideOptions:(RNNNavigationOptions *)options {
- [self.options overrideOptions:options];
- }
-
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
-
- [_presenter applyOptions:self.resolveOptions];
- [_presenter renderComponents:self.resolveOptions perform:nil];
-
- [((UIViewController<RNNParentProtocol> *)self.parentViewController) onChildWillAppear];
- }
-
- - (RNNNavigationOptions *)resolveOptions {
- return [self.options withDefault:self.defaultOptions];
- }
-
- -(void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- [self.eventEmitter sendComponentDidAppear:self.layoutInfo.componentId componentName:self.layoutInfo.name];
- }
-
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- }
-
- - (void)viewDidDisappear:(BOOL)animated {
- [super viewDidDisappear:animated];
- [self.eventEmitter sendComponentDidDisappear:self.layoutInfo.componentId componentName:self.layoutInfo.name];
- }
-
- - (void)renderTreeAndWait:(BOOL)wait perform:(RNNReactViewReadyCompletionBlock)readyBlock {
- if (self.isExternalViewController) {
- if (readyBlock) {
- readyBlock();
- }
- return;
- }
-
- __block RNNReactViewReadyCompletionBlock readyBlockCopy = readyBlock;
- UIView* reactView = [_creator createRootView:self.layoutInfo.name rootViewId:self.layoutInfo.componentId availableSize:[UIScreen mainScreen].bounds.size reactViewReadyBlock:^{
- [_presenter renderComponents:self.resolveOptions perform:^{
- if (readyBlockCopy) {
- readyBlockCopy();
- readyBlockCopy = nil;
- }
- }];
- }];
-
- self.view = reactView;
-
- if (!wait && readyBlock) {
- readyBlockCopy();
- readyBlockCopy = nil;
- }
- }
-
- - (UIViewController *)getCurrentChild {
- return self;
- }
-
- -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
- [self.eventEmitter sendOnSearchBarUpdated:self.layoutInfo.componentId
- text:searchController.searchBar.text
- isFocused:searchController.searchBar.isFirstResponder];
- }
-
- - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
- [self.eventEmitter sendOnSearchBarCancelPressed:self.layoutInfo.componentId];
- }
-
- -(BOOL)isCustomTransitioned {
- return self.resolveOptions.customTransition.animations != nil;
- }
-
- - (BOOL)isExternalViewController {
- return !self.creator;
- }
-
- - (BOOL)prefersStatusBarHidden {
- if (self.resolveOptions.statusBar.visible.hasValue) {
- return ![self.resolveOptions.statusBar.visible get];
- } else if ([self.resolveOptions.statusBar.hideWithTopBar getWithDefaultValue:NO]) {
- return self.navigationController.isNavigationBarHidden;
- }
-
- return NO;
- }
-
- - (UIStatusBarStyle)preferredStatusBarStyle {
- if ([[self.resolveOptions.statusBar.style getWithDefaultValue:@"default"] isEqualToString:@"light"]) {
- return UIStatusBarStyleLightContent;
- } else {
- return UIStatusBarStyleDefault;
- }
- }
-
- - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
- return self.resolveOptions.layout.supportedOrientations;
- }
-
- - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
- RNNRootViewController* vc = (RNNRootViewController*)viewController;
- if (![[vc.self.resolveOptions.topBar.backButton.transition getWithDefaultValue:@""] isEqualToString:@"custom"]){
- navigationController.delegate = nil;
- }
- }
-
- - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
- animationControllerForOperation:(UINavigationControllerOperation)operation
- fromViewController:(UIViewController*)fromVC
- toViewController:(UIViewController*)toVC {
- if (self.animator) {
- return self.animator;
- } else if (operation == UINavigationControllerOperationPush && self.resolveOptions.animations.push.hasCustomAnimation) {
- return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.push isDismiss:NO];
- } else if (operation == UINavigationControllerOperationPop && self.resolveOptions.animations.pop.hasCustomAnimation) {
- return [[RNNAnimationsTransitionDelegate alloc] initWithScreenTransition:self.resolveOptions.animations.pop isDismiss:YES];
- } else {
- return nil;
- }
-
- return nil;
- }
-
- - (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
- return self.previewController;
- }
-
- - (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
- if (self.previewCallback) {
- self.previewCallback(self);
- }
- }
-
- - (void)onActionPress:(NSString *)id {
- [_eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:id];
- }
-
- - (UIPreviewAction *) convertAction:(NSDictionary *)action {
- NSString *actionId = action[@"id"];
- NSString *actionTitle = action[@"title"];
- UIPreviewActionStyle actionStyle = UIPreviewActionStyleDefault;
- if ([action[@"style"] isEqualToString:@"selected"]) {
- actionStyle = UIPreviewActionStyleSelected;
- } else if ([action[@"style"] isEqualToString:@"destructive"]) {
- actionStyle = UIPreviewActionStyleDestructive;
- }
-
- return [UIPreviewAction actionWithTitle:actionTitle style:actionStyle handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
- [self onActionPress:actionId];
- }];
- }
-
- - (NSArray<id<UIPreviewActionItem>> *)previewActionItems {
- NSMutableArray *actions = [[NSMutableArray alloc] init];
- for (NSDictionary *previewAction in self.resolveOptions.preview.actions) {
- UIPreviewAction *action = [self convertAction:previewAction];
- NSDictionary *actionActions = previewAction[@"actions"];
- if (actionActions.count > 0) {
- NSMutableArray *group = [[NSMutableArray alloc] init];
- for (NSDictionary *previewGroupAction in actionActions) {
- [group addObject:[self convertAction:previewGroupAction]];
- }
- UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:action.title style:UIPreviewActionStyleDefault actions:group];
- [actions addObject:actionGroup];
- } else {
- [actions addObject:action];
- }
- }
- return actions;
- }
-
- -(void)onButtonPress:(RNNUIBarButtonItem *)barButtonItem {
- [self.eventEmitter sendOnNavigationButtonPressed:self.layoutInfo.componentId buttonId:barButtonItem.buttonId];
- }
-
- /**
- * fix for #877, #878
- */
- -(void)onJsReload {
- [self cleanReactLeftovers];
- }
-
- /**
- * fix for #880
- */
- -(void)dealloc {
- [self cleanReactLeftovers];
- }
-
- -(void)cleanReactLeftovers {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- [[NSNotificationCenter defaultCenter] removeObserver:self.view];
- self.view = nil;
- self.navigationItem.titleView = nil;
- self.navigationItem.rightBarButtonItems = nil;
- self.navigationItem.leftBarButtonItems = nil;
- }
-
- @end
|