Skip to content

Commit 73a04d1

Browse files
sota000facebook-github-bot
authored andcommitted
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
1 parent 24e07bc commit 73a04d1

File tree

4 files changed

+70
-25
lines changed

4 files changed

+70
-25
lines changed

React-Core.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Pod::Spec.new do |s|
4747
s.compiler_flags = folly_compiler_flags + ' ' + boost_compiler_flags
4848
s.header_dir = "React"
4949
s.framework = "JavaScriptCore"
50-
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" }
50+
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" }
5151
s.user_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/Headers/Private/React-Core\""}
5252
s.default_subspec = "Default"
5353

React/AppSetup/RCTAppSetupUtils.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <Foundation/Foundation.h>
9+
#import <React/RCTBridge.h>
10+
#import <React/RCTRootView.h>
11+
12+
@interface RCTAppSetupUtils : NSObject
13+
+ (void)prepareApp:(UIApplication *_Nonnull)application;
14+
+ (RCTRootView *_Nonnull)defaultRootViewWithBridge:(RCTBridge *_Nonnull)bridge
15+
moduleName:(NSString *_Nonnull)moduleName
16+
initialProperties:(nullable NSDictionary *)initialProperties;
17+
@end

React/AppSetup/RCTAppSetupUtils.mm

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import "RCTAppSetupUtils.h"
9+
10+
#ifdef FB_SONARKIT_ENABLED
11+
#import <FlipperKit/FlipperClient.h>
12+
#import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
13+
#import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h>
14+
#import <FlipperKitReactPlugin/FlipperKitReactPlugin.h>
15+
#import <FlipperKitUserDefaultsPlugin/FKUserDefaultsPlugin.h>
16+
#import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
17+
18+
static void InitializeFlipper(UIApplication *application)
19+
{
20+
FlipperClient *client = [FlipperClient sharedClient];
21+
SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults];
22+
[client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application
23+
withDescriptorMapper:layoutDescriptorMapper]];
24+
[client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]];
25+
[client addPlugin:[FlipperKitReactPlugin new]];
26+
[client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]];
27+
[client start];
28+
}
29+
#endif
30+
31+
@implementation RCTAppSetupUtils
32+
33+
+ (void)prepareApp:(UIApplication *)application
34+
{
35+
#ifdef FB_SONARKIT_ENABLED
36+
InitializeFlipper(application);
37+
#endif
38+
}
39+
40+
+ (RCTRootView *)defaultRootViewWithBridge:(RCTBridge *)bridge
41+
moduleName:(NSString *)moduleName
42+
initialProperties:(NSDictionary *)initialProperties
43+
{
44+
return [[RCTRootView alloc] initWithBridge:bridge moduleName:moduleName initialProperties:initialProperties];
45+
}
46+
47+
@end

template/ios/HelloWorld/AppDelegate.mm

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,18 @@
44
#import <React/RCTBundleURLProvider.h>
55
#import <React/RCTRootView.h>
66

7-
#ifdef FB_SONARKIT_ENABLED
8-
#import <FlipperKit/FlipperClient.h>
9-
#import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
10-
#import <FlipperKitUserDefaultsPlugin/FKUserDefaultsPlugin.h>
11-
#import <FlipperKitNetworkPlugin/FlipperKitNetworkPlugin.h>
12-
#import <SKIOSNetworkPlugin/SKIOSNetworkAdapter.h>
13-
#import <FlipperKitReactPlugin/FlipperKitReactPlugin.h>
14-
15-
static void InitializeFlipper(UIApplication *application) {
16-
FlipperClient *client = [FlipperClient sharedClient];
17-
SKDescriptorMapper *layoutDescriptorMapper = [[SKDescriptorMapper alloc] initWithDefaults];
18-
[client addPlugin:[[FlipperKitLayoutPlugin alloc] initWithRootNode:application withDescriptorMapper:layoutDescriptorMapper]];
19-
[client addPlugin:[[FKUserDefaultsPlugin alloc] initWithSuiteName:nil]];
20-
[client addPlugin:[FlipperKitReactPlugin new]];
21-
[client addPlugin:[[FlipperKitNetworkPlugin alloc] initWithNetworkAdapter:[SKIOSNetworkAdapter new]]];
22-
[client start];
23-
}
24-
#endif
7+
#import <React/RCTAppSetupUtils.h>
258

269
@implementation AppDelegate
2710

2811
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2912
{
30-
#ifdef FB_SONARKIT_ENABLED
31-
InitializeFlipper(application);
32-
#endif
13+
[RCTAppSetupUtils prepareApp: application];
3314

3415
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
35-
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
36-
moduleName:@"HelloWorld"
37-
initialProperties:nil];
16+
RCTRootView *rootView = [RCTAppSetupUtils defaultRootViewWithBridge:bridge
17+
moduleName:@"HelloWorld"
18+
initialProperties:nil];
3819

3920
if (@available(iOS 13.0, *)) {
4021
rootView.backgroundColor = [UIColor systemBackgroundColor];

0 commit comments

Comments
 (0)