From 73a04d145251ad2aad8afb587d3fd0c746c9f00f Mon Sep 17 00:00:00 2001 From: Sota Ogo Date: Mon, 13 Dec 2021 23:12:17 -0800 Subject: [PATCH] Refactor the iOS app template to move setups to a helper class Summary: Changelog: [internal] Move initital setups in AppDelegate to util classes. This will make it easy to apply the new architecture changes in the future. Reviewed By: cortinico Differential Revision: D33051517 fbshipit-source-id: 16e326b7816fae83df65450c545e7dce1a93b9d0 --- React-Core.podspec | 2 +- React/AppSetup/RCTAppSetupUtils.h | 17 ++++++++++ React/AppSetup/RCTAppSetupUtils.mm | 47 ++++++++++++++++++++++++++ template/ios/HelloWorld/AppDelegate.mm | 29 +++------------- 4 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 React/AppSetup/RCTAppSetupUtils.h create mode 100644 React/AppSetup/RCTAppSetupUtils.mm diff --git a/React-Core.podspec b/React-Core.podspec index cd9af6991011c2..e7ea6db16160d1 100644 --- a/React-Core.podspec +++ b/React-Core.podspec @@ -47,7 +47,7 @@ Pod::Spec.new do |s| s.compiler_flags = folly_compiler_flags + ' ' + boost_compiler_flags s.header_dir = "React" s.framework = "JavaScriptCore" - s.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)/ReactCommon\" \"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/DoubleConversion\" \"$(PODS_ROOT)/RCT-Folly\" \"${PODS_ROOT}/Headers/Public/React-hermes\" \"${PODS_ROOT}/Headers/Public/hermes-engine\"", "DEFINES_MODULE" => "YES" } + s.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)/ReactCommon\" \"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/DoubleConversion\" \"$(PODS_ROOT)/RCT-Folly\" \"${PODS_ROOT}/Headers/Public/React-hermes\" \"${PODS_ROOT}/Headers/Public/hermes-engine\" \"${PODS_ROOT}/Headers/Public/FlipperKit\"", "DEFINES_MODULE" => "YES" } s.user_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/Headers/Private/React-Core\""} s.default_subspec = "Default" diff --git a/React/AppSetup/RCTAppSetupUtils.h b/React/AppSetup/RCTAppSetupUtils.h new file mode 100644 index 00000000000000..8f6865e4f4fcce --- /dev/null +++ b/React/AppSetup/RCTAppSetupUtils.h @@ -0,0 +1,17 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import +#import +#import + +@interface RCTAppSetupUtils : NSObject ++ (void)prepareApp:(UIApplication *_Nonnull)application; ++ (RCTRootView *_Nonnull)defaultRootViewWithBridge:(RCTBridge *_Nonnull)bridge + moduleName:(NSString *_Nonnull)moduleName + initialProperties:(nullable NSDictionary *)initialProperties; +@end diff --git a/React/AppSetup/RCTAppSetupUtils.mm b/React/AppSetup/RCTAppSetupUtils.mm new file mode 100644 index 00000000000000..85edbc131f82a0 --- /dev/null +++ b/React/AppSetup/RCTAppSetupUtils.mm @@ -0,0 +1,47 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import "RCTAppSetupUtils.h" + +#ifdef FB_SONARKIT_ENABLED +#import +#import +#import +#import +#import +#import + +static void InitializeFlipper(UIApplication *application) +{ + FlipperClient *client = [FlipperClient sharedClient]; + SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults]; + [client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application + withDescriptorMapper:layoutDescriptorMapper]]; + [client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]]; + [client addPlugin:[FlipperKitReactPlugin new]]; + [client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]]; + [client start]; +} +#endif + +@implementation RCTAppSetupUtils + ++ (void)prepareApp:(UIApplication *)application +{ +#ifdef FB_SONARKIT_ENABLED + InitializeFlipper(application); +#endif +} + ++ (RCTRootView *)defaultRootViewWithBridge:(RCTBridge *)bridge + moduleName:(NSString *)moduleName + initialProperties:(NSDictionary *)initialProperties +{ + return [[RCTRootView alloc] initWithBridge:bridge moduleName:moduleName initialProperties:initialProperties]; +} + +@end diff --git a/template/ios/HelloWorld/AppDelegate.mm b/template/ios/HelloWorld/AppDelegate.mm index 59b62377cd4b65..77f92aa529bffa 100644 --- a/template/ios/HelloWorld/AppDelegate.mm +++ b/template/ios/HelloWorld/AppDelegate.mm @@ -4,37 +4,18 @@ #import #import -#ifdef FB_SONARKIT_ENABLED -#import -#import -#import -#import -#import -#import - -static void InitializeFlipper(UIApplication *application) { - FlipperClient *client = [FlipperClient sharedClient]; - SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults]; - [client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application withDescriptorMapper:layoutDescriptorMapper]]; - [client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]]; - [client addPlugin:[FlipperKitReactPlugin new]]; - [client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]]; - [client start]; -} -#endif +#import @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { -#ifdef FB_SONARKIT_ENABLED - InitializeFlipper(application); -#endif + [RCTAppSetupUtils prepareApp: application]; RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; - RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge - moduleName:@"HelloWorld" - initialProperties:nil]; + RCTRootView *rootView = [RCTAppSetupUtils defaultRootViewWithBridge:bridge + moduleName:@"HelloWorld" + initialProperties:nil]; if (@available(iOS 13.0, *)) { rootView.backgroundColor = [UIColor systemBackgroundColor];