Skip to content

Commit 30a914f

Browse files
committed
feat: implement RCTReactController and RCTSwiftUIAppDelegate
1 parent defc3d3 commit 30a914f

File tree

10 files changed

+451
-53
lines changed

10 files changed

+451
-53
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#import <React/RCTBridgeDelegate.h>
2+
#import <UIKit/UIKit.h>
3+
4+
@interface RCTReactController : UIViewController
5+
6+
@property (nonatomic, strong) NSString * _Nonnull moduleName;
7+
@property (nonatomic, strong) NSDictionary * _Nullable initialProps;
8+
9+
- (instancetype _Nonnull)initWithModuleName:(NSString * _Nonnull)moduleName
10+
initProps:(NSDictionary * _Nullable)initProps;
11+
12+
@end
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#import "RCTReactController.h"
2+
#import <React/RCTCxxBridgeDelegate.h>
3+
#import <React/RCTLog.h>
4+
#import <React/RCTRootView.h>
5+
#import <React/RCTSurfacePresenterBridgeAdapter.h>
6+
#import <React/RCTUtils.h>
7+
#import <react/renderer/runtimescheduler/RuntimeScheduler.h>
8+
#import "RCTAppSetupUtils.h"
9+
#import <React/RCTUtils.h>
10+
11+
#if RN_DISABLE_OSS_PLUGIN_HEADER
12+
#import <RCTTurboModulePlugin/RCTTurboModulePlugin.h>
13+
#else
14+
#import <React/CoreModulesPlugins.h>
15+
#endif
16+
#import <React/RCTBundleURLProvider.h>
17+
#import <React/RCTComponentViewFactory.h>
18+
#import <React/RCTComponentViewProtocol.h>
19+
#import <React/RCTFabricSurface.h>
20+
#import <React/RCTSurfaceHostingProxyRootView.h>
21+
#import <React/RCTSurfacePresenter.h>
22+
#import <ReactCommon/RCTContextContainerHandling.h>
23+
#if USE_HERMES
24+
#import <ReactCommon/RCTHermesInstance.h>
25+
#else
26+
#import <ReactCommon/RCTJscInstance.h>
27+
#endif
28+
#import <ReactCommon/RCTHost+Internal.h>
29+
#import <ReactCommon/RCTHost.h>
30+
#import <ReactCommon/RCTTurboModuleManager.h>
31+
#import <react/config/ReactNativeConfig.h>
32+
#import <react/renderer/runtimescheduler/RuntimeScheduler.h>
33+
#import <react/renderer/runtimescheduler/RuntimeSchedulerCallInvoker.h>
34+
#import <react/runtime/JSRuntimeFactory.h>
35+
36+
static NSString *const kRNConcurrentRoot = @"concurrentRoot";
37+
38+
static NSDictionary *updateInitialProps(NSDictionary *initialProps, BOOL isFabricEnabled)
39+
{
40+
NSMutableDictionary *mutableProps = [initialProps mutableCopy] ?: [NSMutableDictionary new];
41+
// Hardcoding the Concurrent Root as it it not recommended to
42+
// have the concurrentRoot turned off when Fabric is enabled.
43+
mutableProps[kRNConcurrentRoot] = @(isFabricEnabled);
44+
return mutableProps;
45+
}
46+
47+
48+
@implementation RCTReactController
49+
50+
- (instancetype)initWithModuleName:(NSString *)moduleName initProps:(NSDictionary *)initProps {
51+
if (self = [super init]) {
52+
_moduleName = moduleName;
53+
_initialProps = initProps;
54+
}
55+
return self;
56+
}
57+
58+
- (void)loadView {
59+
BOOL fabricEnabled = self.fabricEnabled;
60+
BOOL enableBridgeless = self.bridgelessEnabled;
61+
62+
NSDictionary *initProps = updateInitialProps([self prepareInitialProps], fabricEnabled);
63+
64+
UIView *rootView;
65+
if (enableBridgeless) {
66+
// TODO: Fix Bridgeless mode
67+
// RCTFabricSurface *surface = [_reactHost createSurfaceWithModuleName:self.moduleName initialProperties:initProps];
68+
69+
// RCTSurfaceHostingProxyRootView *surfaceHostingProxyRootView = [[RCTSurfaceHostingProxyRootView alloc]
70+
// initWithSurface:surface
71+
// sizeMeasureMode:RCTSurfaceSizeMeasureModeWidthExact | RCTSurfaceSizeMeasureModeHeightExact];
72+
73+
// rootView = (RCTRootView *)surfaceHostingProxyRootView;
74+
} else {
75+
RCTBridge* bridge = [RCTSharedApplication().delegate performSelector:@selector(bridge)];
76+
rootView = [self createRootViewWithBridge:bridge moduleName:self.moduleName initProps:initProps];
77+
}
78+
79+
// Set rootView to the main of this view controller.
80+
self.view = rootView;
81+
}
82+
83+
- (UIView *)createRootViewWithBridge:(RCTBridge *)bridge
84+
moduleName:(NSString *)moduleName
85+
initProps:(NSDictionary *)initProps
86+
{
87+
BOOL enableFabric = self.fabricEnabled;
88+
UIView *rootView = RCTAppSetupDefaultRootView(bridge, moduleName, initProps, enableFabric);
89+
90+
rootView.backgroundColor = [UIColor systemBackgroundColor];
91+
92+
return rootView;
93+
}
94+
95+
- (NSDictionary *)prepareInitialProps
96+
{
97+
return self.initialProps;
98+
}
99+
100+
- (BOOL)newArchEnabled
101+
{
102+
return [RCTSharedApplication().delegate performSelector:@selector(newArchEnabled)];
103+
}
104+
105+
- (BOOL)turboModuleEnabled
106+
{
107+
return [self newArchEnabled];
108+
}
109+
110+
- (BOOL)fabricEnabled
111+
{
112+
return [self newArchEnabled];
113+
}
114+
115+
- (BOOL)bridgelessEnabled
116+
{
117+
return [RCTSharedApplication().delegate performSelector:@selector(bridgelessEnabled)];
118+
}
119+
120+
@end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#import <React/RCTBridgeDelegate.h>
2+
#import <UIKit/UIKit.h>
3+
4+
@class RCTBridge;
5+
@protocol RCTBridgeDelegate;
6+
@protocol RCTComponentViewProtocol;
7+
@class RCTRootView;
8+
@class RCTSurfacePresenterBridgeAdapter;
9+
10+
NS_ASSUME_NONNULL_BEGIN
11+
12+
@interface RCTSwiftUIAppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate>
13+
14+
@property (nonatomic, strong, nonnull) UIWindow *window;
15+
@property (nonatomic, strong, nullable) RCTBridge *bridge;
16+
@property (nonatomic, strong, nullable) NSDictionary *initialProps;
17+
18+
@property (nonatomic, strong, nullable) RCTSurfacePresenterBridgeAdapter *bridgeAdapter;
19+
20+
/// This method returns a map of Component Descriptors and Components classes that needs to be registered in the
21+
/// new renderer. The Component Descriptor is a string which represent the name used in JS to refer to the native
22+
/// component. The default implementation returns an empty dictionary. Subclasses can override this method to register
23+
/// the required components.
24+
///
25+
/// @return a dictionary that associate a component for the new renderer with his descriptor.
26+
- (NSDictionary<NSString *, Class<RCTComponentViewProtocol>> *_Nullable)thirdPartyFabricComponents;
27+
28+
/// This method controls whether the `turboModules` feature of the New Architecture is turned on or off.
29+
///
30+
/// @note: This is required to be rendering on Fabric (i.e. on the New Architecture).
31+
/// @return: `true` if the Turbo Native Module are enabled. Otherwise, it returns `false`.
32+
- (BOOL)turboModuleEnabled;
33+
34+
/// This method controls whether the App will use the Fabric renderer of the New Architecture or not.
35+
///
36+
/// @return: `true` if the Fabric Renderer is enabled. Otherwise, it returns `false`.
37+
- (BOOL)fabricEnabled;
38+
39+
/// This method controls whether React Native's new initialization layer is enabled.
40+
///
41+
/// @return: `true` if the new initialization layer is enabled. Otherwise returns `false`.
42+
- (BOOL)bridgelessEnabled;
43+
44+
- (NSURL *)bundleURL;
45+
46+
@end
47+
48+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)