From e4cfbbad6d4bc772b63e6a3a04de01308c697b91 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Mon, 17 Aug 2020 15:41:43 -0700 Subject: [PATCH] Introduce RCTFBModalComponentView Summary: Changelog: [Internal] Reviewed By: shergin Differential Revision: D23155490 fbshipit-source-id: 2c83c2ec71ca4d842275b4db6aac7169d3d77955 --- .../Modal/RCTModalHostViewComponentView.h | 14 ++++++ .../Modal/RCTModalHostViewComponentView.mm | 45 +++++++++++-------- .../RCTFabricComponentsPlugins.h | 1 - .../RCTFabricComponentsPlugins.mm | 1 - 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.h b/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.h index 8966aa11f42..16fbdfb9624 100644 --- a/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.h +++ b/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.h @@ -12,4 +12,18 @@ */ @interface RCTModalHostViewComponentView : RCTViewComponentView +/** + * Subclasses may override this method and present the modal on different view controller. + * Default implementation presents the modal on `[self reactViewController]`. + */ +- (void)presentViewController:(UIViewController *)modalViewController + animated:(BOOL)animated + completion:(void (^)(void))completion; + +/** + * Subclasses may override this method. + * Default implementation calls `[UIViewController dismissViewControllerAnimated:completion:]`. + */ +- (void)dismissViewController:(UIViewController *)modalViewController animated:(BOOL)animated; + @end diff --git a/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm b/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm index 2599f1a31fb..a1df5a6d0cc 100644 --- a/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm @@ -13,8 +13,8 @@ #import #import -#import "FBRCTFabricComponentsPlugins.h" #import "RCTConversions.h" + #import "RCTFabricModalHostViewController.h" using namespace facebook::react; @@ -120,30 +120,42 @@ static ModalHostViewEventEmitter::OnOrientationChange onOrientationChangeStruct( return self; } +- (void)presentViewController:(UIViewController *)modalViewController + animated:(BOOL)animated + completion:(void (^)(void))completion +{ + UIViewController *controller = [self reactViewController]; + [controller presentViewController:modalViewController animated:animated completion:completion]; +} + +- (void)dismissViewController:(UIViewController *)modalViewController animated:(BOOL)animated +{ + [modalViewController dismissViewControllerAnimated:animated completion:nil]; +} + - (void)ensurePresentedOnlyIfNeeded { BOOL shouldBePresented = !_isPresented && self.window; if (shouldBePresented) { - UIViewController *controller = [self reactViewController]; _isPresented = YES; - return [controller - presentViewController:_viewController - animated:_shouldAnimatePresentation - completion:^{ - if (!self->_eventEmitter) { - return; - } + [self presentViewController:_viewController + animated:_shouldAnimatePresentation + completion:^{ + if (!self->_eventEmitter) { + return; + } - assert(std::dynamic_pointer_cast(self->_eventEmitter)); - auto eventEmitter = std::static_pointer_cast(self->_eventEmitter); - eventEmitter->onShow(ModalHostViewEventEmitter::OnShow{}); - }]; + assert(std::dynamic_pointer_cast(self->_eventEmitter)); + auto eventEmitter = + std::static_pointer_cast(self->_eventEmitter); + eventEmitter->onShow(ModalHostViewEventEmitter::OnShow{}); + }]; } BOOL shouldBeHidden = _isPresented && !self.superview; if (shouldBeHidden) { _isPresented = NO; - [_viewController dismissViewControllerAnimated:_shouldAnimatePresentation completion:nil]; + [self dismissViewController:_viewController animated:_shouldAnimatePresentation]; } } @@ -224,8 +236,3 @@ static ModalHostViewEventEmitter::OnOrientationChange onOrientationChangeStruct( } @end - -Class RCTModalHostViewCls(void) -{ - return RCTModalHostViewComponentView.class; -} diff --git a/React/Fabric/Mounting/ComponentViews/RCTFabricComponentsPlugins.h b/React/Fabric/Mounting/ComponentViews/RCTFabricComponentsPlugins.h index 573e7b1fc6f..fa7984df03a 100644 --- a/React/Fabric/Mounting/ComponentViews/RCTFabricComponentsPlugins.h +++ b/React/Fabric/Mounting/ComponentViews/RCTFabricComponentsPlugins.h @@ -36,7 +36,6 @@ Class RCTActivityIndicatorViewCls(void) __attribute__( Class RCTSliderCls(void) __attribute__((used)); Class RCTSwitchCls(void) __attribute__((used)); Class RCTUnimplementedNativeViewCls(void) __attribute__((used)); -Class RCTModalHostViewCls(void) __attribute__((used)); Class RCTImageCls(void) __attribute__((used)); Class RCTParagraphCls(void) __attribute__((used)); Class RCTTextInputCls(void) __attribute__((used)); diff --git a/React/Fabric/Mounting/ComponentViews/RCTFabricComponentsPlugins.mm b/React/Fabric/Mounting/ComponentViews/RCTFabricComponentsPlugins.mm index 6107f1297fe..c8bee6acfce 100644 --- a/React/Fabric/Mounting/ComponentViews/RCTFabricComponentsPlugins.mm +++ b/React/Fabric/Mounting/ComponentViews/RCTFabricComponentsPlugins.mm @@ -25,7 +25,6 @@ Class RCTFabricComponentsProvider(const char *name) { {"Slider", RCTSliderCls}, {"Switch", RCTSwitchCls}, {"UnimplementedNativeView", RCTUnimplementedNativeViewCls}, - {"ModalHostView", RCTModalHostViewCls}, {"Image", RCTImageCls}, {"Paragraph", RCTParagraphCls}, {"TextInput", RCTTextInputCls},