Skip to content

Commit 34b8876

Browse files
fkgozalifacebook-github-bot
authored andcommitted
iOS: Introduced RCTSurfaceHostingProxyRootView for migration to RCTSurfaceHostingView
Summary: To help with migration from direct usages of RCTRootView to RCTSurfaceHostingView, RCTSurfaceHostingProxyRootView is added, which is simply a custom impl of RCTSurfaceHostingView, but will all RCTRootView APIs preserved. This makes it easy to do a drop-in replacement in native callsites: ``` // before: RCTRootView *rootView = [[RCTRootView alloc] init...]; // after: RCTRootView *rootView = (RCTRootView *)[[RCTSurfaceHostingProxyRootView alloc] init...]; ``` Reviewed By: shergin Differential Revision: D7141696 fbshipit-source-id: db8c447749eaa896efaa37774a9babef132128eb
1 parent ffcd067 commit 34b8876

File tree

4 files changed

+295
-7
lines changed

4 files changed

+295
-7
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
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 <UIKit/UIKit.h>
9+
10+
#import <React/RCTRootView.h>
11+
12+
#import "RCTSurfaceHostingView.h"
13+
14+
NS_ASSUME_NONNULL_BEGIN
15+
16+
/**
17+
* This is a RCTRootView-compatible implementation of RCTSurfaceHostingView.
18+
* Use this class to replace all usages of RCTRootView in the app for easier migration
19+
* to RCTSurfaceHostingView.
20+
*
21+
* WARNING: In the future, RCTRootView will be deprecated in favor of RCTSurfaceHostingView.
22+
*/
23+
@interface RCTSurfaceHostingProxyRootView : RCTSurfaceHostingView
24+
25+
#pragma mark RCTRootView compatibility - keep these sync'ed with RCTRootView.h
26+
27+
@property (nonatomic, copy, readonly) NSString *moduleName;
28+
@property (nonatomic, strong, readonly) RCTBridge *bridge;
29+
@property (nonatomic, copy, readwrite) NSDictionary *appProperties;
30+
@property (nonatomic, assign) RCTRootViewSizeFlexibility sizeFlexibility;
31+
@property (nonatomic, weak) id<RCTRootViewDelegate> delegate;
32+
@property (nonatomic, weak) UIViewController *reactViewController;
33+
@property (nonatomic, strong, readonly) UIView *contentView;
34+
@property (nonatomic, strong) UIView *loadingView;
35+
@property (nonatomic, assign) BOOL passThroughTouches;
36+
@property (nonatomic, assign) NSTimeInterval loadingViewFadeDelay;
37+
@property (nonatomic, assign) NSTimeInterval loadingViewFadeDuration;
38+
@property (nonatomic, assign) BOOL fabric;
39+
40+
- (instancetype)initWithBridge:(RCTBridge *)bridge
41+
moduleName:(NSString *)moduleName
42+
initialProperties:(NSDictionary *)initialProperties
43+
fabric:(BOOL)fabric NS_DESIGNATED_INITIALIZER;
44+
45+
- (instancetype)initWithBridge:(RCTBridge *)bridge
46+
moduleName:(NSString *)moduleName
47+
initialProperties:(NSDictionary *)initialProperties;
48+
49+
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
50+
moduleName:(NSString *)moduleName
51+
initialProperties:(NSDictionary *)initialProperties
52+
launchOptions:(NSDictionary *)launchOptions;
53+
54+
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
55+
moduleName:(NSString *)moduleName
56+
initialProperties:(NSDictionary *)initialProperties
57+
launchOptions:(NSDictionary *)launchOptions
58+
fabric:(BOOL)fabric;
59+
60+
- (void)cancelTouches;
61+
62+
@end
63+
64+
NS_ASSUME_NONNULL_END
65+
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
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 "RCTSurfaceHostingProxyRootView.h"
9+
10+
#import <objc/runtime.h>
11+
12+
#import "RCTAssert.h"
13+
#import "RCTBridge.h"
14+
#import "RCTLog.h"
15+
#import "RCTPerformanceLogger.h"
16+
#import "RCTProfile.h"
17+
#import "RCTRootContentView.h"
18+
#import "RCTRootViewDelegate.h"
19+
#import "RCTSurface.h"
20+
#import "UIView+React.h"
21+
22+
static RCTSurfaceSizeMeasureMode convertToSurfaceSizeMeasureMode(RCTRootViewSizeFlexibility sizeFlexibility) {
23+
switch (sizeFlexibility) {
24+
case RCTRootViewSizeFlexibilityWidthAndHeight:
25+
return RCTSurfaceSizeMeasureModeWidthUndefined | RCTSurfaceSizeMeasureModeHeightUndefined;
26+
case RCTRootViewSizeFlexibilityWidth:
27+
return RCTSurfaceSizeMeasureModeWidthUndefined | RCTSurfaceSizeMeasureModeHeightExact;
28+
case RCTRootViewSizeFlexibilityHeight:
29+
return RCTSurfaceSizeMeasureModeWidthExact | RCTSurfaceSizeMeasureModeHeightUndefined;
30+
case RCTRootViewSizeFlexibilityNone:
31+
return RCTSurfaceSizeMeasureModeWidthExact | RCTSurfaceSizeMeasureModeHeightExact;
32+
}
33+
}
34+
35+
static RCTRootViewSizeFlexibility convertToRootViewSizeFlexibility(RCTSurfaceSizeMeasureMode sizeMeasureMode) {
36+
switch (sizeMeasureMode) {
37+
case RCTSurfaceSizeMeasureModeWidthUndefined | RCTSurfaceSizeMeasureModeHeightUndefined:
38+
return RCTRootViewSizeFlexibilityWidthAndHeight;
39+
case RCTSurfaceSizeMeasureModeWidthUndefined | RCTSurfaceSizeMeasureModeHeightExact:
40+
return RCTRootViewSizeFlexibilityWidth;
41+
case RCTSurfaceSizeMeasureModeWidthExact | RCTSurfaceSizeMeasureModeHeightUndefined:
42+
return RCTRootViewSizeFlexibilityHeight;
43+
case RCTSurfaceSizeMeasureModeWidthExact | RCTSurfaceSizeMeasureModeHeightExact:
44+
default:
45+
return RCTRootViewSizeFlexibilityNone;
46+
}
47+
}
48+
49+
@implementation RCTSurfaceHostingProxyRootView
50+
51+
- (instancetype)initWithBridge:(RCTBridge *)bridge
52+
moduleName:(NSString *)moduleName
53+
initialProperties:(NSDictionary *)initialProperties
54+
fabric:(BOOL)fabric
55+
{
56+
RCTAssertMainQueue();
57+
RCTAssert(bridge, @"A bridge instance is required to create an RCTSurfaceHostingProxyRootView");
58+
RCTAssert(moduleName, @"A moduleName is required to create an RCTSurfaceHostingProxyRootView");
59+
60+
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[RCTSurfaceHostingProxyRootView init]", nil);
61+
if (!bridge.isLoading) {
62+
[bridge.performanceLogger markStartForTag:RCTPLTTI];
63+
}
64+
65+
if (self = [super initWithBridge:bridge moduleName:moduleName initialProperties:initialProperties fabric:fabric]) {
66+
self.backgroundColor = [UIColor whiteColor];
67+
}
68+
69+
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
70+
71+
return self;
72+
}
73+
74+
- (instancetype)initWithBridge:(RCTBridge *)bridge
75+
moduleName:(NSString *)moduleName
76+
initialProperties:(NSDictionary *)initialProperties
77+
{
78+
return [self initWithBridge:bridge moduleName:moduleName initialProperties:initialProperties fabric:NO];
79+
}
80+
81+
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
82+
moduleName:(NSString *)moduleName
83+
initialProperties:(NSDictionary *)initialProperties
84+
launchOptions:(NSDictionary *)launchOptions
85+
fabric:(BOOL)fabric
86+
{
87+
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:bundleURL
88+
moduleProvider:nil
89+
launchOptions:launchOptions];
90+
91+
return [self initWithBridge:bridge moduleName:moduleName initialProperties:initialProperties fabric:fabric];
92+
}
93+
94+
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
95+
moduleName:(NSString *)moduleName
96+
initialProperties:(NSDictionary *)initialProperties
97+
launchOptions:(NSDictionary *)launchOptions
98+
{
99+
return [self initWithBundleURL:bundleURL moduleName:moduleName initialProperties:initialProperties launchOptions:launchOptions fabric:NO];
100+
}
101+
102+
RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
103+
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
104+
105+
# pragma mark proxy methods to RCTSurfaceHostingView
106+
107+
- (NSString *)moduleName
108+
{
109+
return super.surface.moduleName;
110+
}
111+
112+
- (RCTBridge *)bridge
113+
{
114+
return super.surface.bridge;
115+
}
116+
117+
- (BOOL)fabric
118+
{
119+
return super.surface.fabric;
120+
}
121+
122+
- (UIView *)contentView
123+
{
124+
return self;
125+
}
126+
127+
- (NSNumber *)reactTag
128+
{
129+
return super.surface.rootViewTag;
130+
}
131+
132+
- (RCTRootViewSizeFlexibility)sizeFlexibility
133+
{
134+
return convertToRootViewSizeFlexibility(super.sizeMeasureMode);
135+
}
136+
137+
- (void)setSizeFlexibility:(RCTRootViewSizeFlexibility)sizeFlexibility
138+
{
139+
super.sizeMeasureMode = convertToSurfaceSizeMeasureMode(sizeFlexibility);
140+
}
141+
142+
- (NSDictionary *)appProperties
143+
{
144+
return super.surface.properties;
145+
}
146+
147+
- (void)setAppProperties:(NSDictionary *)appProperties
148+
{
149+
[super.surface setProperties:appProperties];
150+
}
151+
152+
- (CGSize)intrinsicContentSize
153+
{
154+
return super.surface.intrinsicSize;
155+
}
156+
157+
- (UIView *)loadingView
158+
{
159+
return super.activityIndicatorViewFactory ? super.activityIndicatorViewFactory() : nil;
160+
}
161+
162+
- (void)setLoadingView:(UIView *)loadingView
163+
{
164+
super.activityIndicatorViewFactory = ^UIView *(void) {
165+
return loadingView;
166+
};
167+
}
168+
169+
#pragma mark RCTSurfaceDelegate proxying
170+
171+
- (void)surface:(RCTSurface *)surface didChangeStage:(RCTSurfaceStage)stage
172+
{
173+
[super surface:surface didChangeStage:stage];
174+
if (RCTSurfaceStageIsRunning(stage)) {
175+
[super.surface.bridge.performanceLogger markStopForTag:RCTPLTTI];
176+
dispatch_async(dispatch_get_main_queue(), ^{
177+
[[NSNotificationCenter defaultCenter] postNotificationName:RCTContentDidAppearNotification
178+
object:self];
179+
});
180+
}
181+
}
182+
183+
- (void)surface:(RCTSurface *)surface didChangeIntrinsicSize:(CGSize)intrinsicSize
184+
{
185+
[super surface:surface didChangeIntrinsicSize:intrinsicSize];
186+
187+
[_delegate rootViewDidChangeIntrinsicSize:(RCTRootView *)self];
188+
}
189+
190+
#pragma mark legacy
191+
192+
- (UIViewController *)reactViewController
193+
{
194+
return _reactViewController ?: [super reactViewController];
195+
}
196+
197+
#pragma mark unsupported
198+
199+
- (void)cancelTouches
200+
{
201+
// Not supported.
202+
}
203+
204+
@end
205+

React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingView.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#import <UIKit/UIKit.h>
99

10+
#import <React/RCTSurfaceDelegate.h>
1011
#import <React/RCTSurfaceSizeMeasureMode.h>
1112
#import <React/RCTSurfaceStage.h>
1213

@@ -23,7 +24,7 @@ NS_ASSUME_NONNULL_BEGIN
2324
* This class can be used as easy-to-use general purpose integration point
2425
* of ReactNative-powered experiences in UIKit based apps.
2526
*/
26-
@interface RCTSurfaceHostingView : UIView
27+
@interface RCTSurfaceHostingView : UIView <RCTSurfaceDelegate>
2728

2829
/**
2930
* Designated initializer.
@@ -41,6 +42,15 @@ NS_ASSUME_NONNULL_BEGIN
4142
moduleName:(NSString *)moduleName
4243
initialProperties:(NSDictionary *)initialProperties;
4344

45+
/**
46+
* Convenience initializer.
47+
* To control toggle Fabric for the Surface.
48+
*/
49+
- (instancetype)initWithBridge:(RCTBridge *)bridge
50+
moduleName:(NSString *)moduleName
51+
initialProperties:(NSDictionary *)initialProperties
52+
fabric:(BOOL)fabric;
53+
4454
/**
4555
* Surface object which is currently using to power the view.
4656
* Read-only.

React/Base/Surface/SurfaceHostingView/RCTSurfaceHostingView.mm

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#import "RCTSurfaceView.h"
1414
#import "RCTUtils.h"
1515

16-
@interface RCTSurfaceHostingView () <RCTSurfaceDelegate>
16+
@interface RCTSurfaceHostingView ()
1717

1818
@property (nonatomic, assign) BOOL isActivityIndicatorViewVisible;
1919
@property (nonatomic, assign) BOOL isSurfaceViewVisible;
@@ -33,13 +33,21 @@ @implementation RCTSurfaceHostingView {
3333
- (instancetype)initWithBridge:(RCTBridge *)bridge
3434
moduleName:(NSString *)moduleName
3535
initialProperties:(NSDictionary *)initialProperties
36+
fabric:(BOOL)fabric
3637
{
37-
RCTSurface *surface =
38-
[[RCTSurface alloc] initWithBridge:bridge
39-
moduleName:moduleName
40-
initialProperties:initialProperties];
41-
38+
RCTSurface *surface = [[RCTSurface alloc] initWithBridge:bridge
39+
moduleName:moduleName
40+
initialProperties:initialProperties
41+
fabric:fabric];
4242
return [self initWithSurface:surface];
43+
44+
}
45+
46+
- (instancetype)initWithBridge:(RCTBridge *)bridge
47+
moduleName:(NSString *)moduleName
48+
initialProperties:(NSDictionary *)initialProperties
49+
{
50+
return [self initWithBridge:bridge moduleName:moduleName initialProperties:initialProperties fabric:NO];
4351
}
4452

4553
- (instancetype)initWithSurface:(RCTSurface *)surface

0 commit comments

Comments
 (0)