Skip to content

Commit

Permalink
fix: dimensions, use RCTMainWindow()
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Jan 18, 2024
1 parent 4a0600d commit 76e3d6a
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)bridgelessEnabled;

/// Return the bundle URL for the main bundle.
- (NSURL *)bundleURL;
- (NSURL *__nullable)bundleURL;

/// Don't use this method, it's going to be removed soon.
- (UIView *)viewWithModuleName:(NSString *)moduleName initialProperties:(NSDictionary*)initialProperties launchOptions:(NSDictionary*)launchOptions;
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ - (Class)getModuleClassFromName:(const char *)name

- (void)createReactHost
{
if (_reactHost != nil) {
return;
}
__weak __typeof(self) weakSelf = self;
_reactHost = [[RCTHost alloc] initWithBundleURL:[self bundleURL]
hostDelegate:nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import SwiftUI

/**
This SwiftUI struct returns main React Native scene. It should be used only once as it conains setup code.

Example:
```swift
@main
struct YourApp: App {
@UIApplicationDelegateAdaptor var delegate: AppDelegate

var body: some Scene {
RCTMainWindow(moduleName: "YourApp")
}
}
```

Note: If you want to create additional windows in your app, create a new `WindowGroup {}` and pass it a `RCTRootViewRepresentable`.
*/
public struct RCTMainWindow: Scene {
var moduleName: String
var initialProps: RCTRootViewRepresentable.InitialPropsType

public init(moduleName: String, initialProps: RCTRootViewRepresentable.InitialPropsType = nil) {
self.moduleName = moduleName
self.initialProps = initialProps
}

public var body: some Scene {
WindowGroup {
RCTRootViewRepresentable(moduleName: moduleName, initialProps: initialProps)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#import <UIKit/UIKit.h>

/**
A `UIViewController` responsible for embeding `RCTRootView` inside. Uses Factory pattern to retrive new view instances.
Note: Used to in `RCTRootViewRepresentable` to display React views.
*/
@interface RCTReactViewController : UIViewController

@property (nonatomic, strong) NSString *_Nonnull moduleName;
@property (nonatomic, strong) NSDictionary *_Nullable initialProps;
@property (nonatomic, strong, nonnull) NSString *moduleName;
@property (nonatomic, strong, nullable) NSDictionary *initialProps;

- (instancetype _Nonnull)initWithModuleName:(NSString *_Nonnull)moduleName
initProps:(NSDictionary *_Nullable)initProps;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "RCTReactViewController.h"
#import <React/RCTConstants.h>

@protocol RCTRootViewFactoryProtocol <NSObject>

Expand All @@ -16,6 +17,10 @@ - (instancetype)initWithModuleName:(NSString *)moduleName initProps:(NSDictionar
return self;
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[[NSNotificationCenter defaultCenter] postNotificationName:RCTWindowFrameDidChangeNotification object:self];
}

// TODO: Temporary solution for creating RCTRootView on demand. This should be done through factory pattern, see here: https://github.com/facebook/react-native/pull/42263
- (void)loadView {
id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import SwiftUI

/*
* Use this struct in SwiftUI context to present React Native rootView.
*
* Example:
* ```
* WindowGroup {
* RCTRootViewRepresentable(moduleName: "RNTesterApp", initialProps: [:])
* }
* ```
*/
/**
SwiftUI view enclosing `RCTReactViewController`. Its main purpose is to display React Native views inside of SwiftUI lifecycle.

Use it create new windows in your app:
Example:
```swift
WindowGroup {
RCTRootViewRepresentable(moduleName: "YourAppName", initialProps: [:])
}
```
*/
public struct RCTRootViewRepresentable: UIViewControllerRepresentable {
public typealias InitialPropsType = [AnyHashable: Any]?

var moduleName: String
var initialProps: [AnyHashable: Any]?
var initialProps: InitialPropsType

public init(moduleName: String, initialProps: [AnyHashable : Any]?) {
public init(moduleName: String, initialProps: InitialPropsType = nil) {
self.moduleName = moduleName
self.initialProps = initialProps
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ Pod::Spec.new do |s|
s.source = source
s.source_files = "*.{swift,h,m}"
s.frameworks = ["UIKit", "SwiftUI"]

s.dependency "React-Core"
end
2 changes: 1 addition & 1 deletion packages/react-native/React/Base/RCTBridgeDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ NS_ASSUME_NONNULL_BEGIN
* When running from a locally bundled JS file, this should be a `file://` url
* pointing to a path inside the app resources, e.g. `file://.../main.jsbundle`.
*/
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge;
- (NSURL *__nullable)sourceURLForBridge:(RCTBridge *)bridge;

@optional

Expand Down
1 change: 1 addition & 0 deletions packages/react-native/template/_gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ yarn-error.log

# testing
/coverage
.yarn
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
00E356F31AD99517003FC87E /* HelloWorldTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 00E356F21AD99517003FC87E /* HelloWorldTests.m */; };
0C80B921A6F3F58F76C31292 /* libPods-HelloWorld.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5DCACB8F33CDC322A6C60F78 /* libPods-HelloWorld.a */; };
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
767CEB962B56C0F3000139AD /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 767CEB952B56C0F3000139AD /* AppDelegate.swift */; };
767CEB982B56C0FA000139AD /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 767CEB972B56C0FA000139AD /* App.swift */; };
767CEBBC2B582F6B000139AD /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 767CEBBB2B582F6B000139AD /* AppDelegate.swift */; };
767CEBBE2B582F78000139AD /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 767CEBBD2B582F78000139AD /* App.swift */; };
7699B88040F8A987B510C191 /* libPods-HelloWorld-HelloWorldTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 19F6CBCC0A4E27FBF8BF4A61 /* libPods-HelloWorld-HelloWorldTests.a */; };
/* End PBXBuildFile section */

Expand All @@ -37,8 +37,8 @@
5709B34CF0A7D63546082F79 /* Pods-HelloWorld.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HelloWorld.release.xcconfig"; path = "Target Support Files/Pods-HelloWorld/Pods-HelloWorld.release.xcconfig"; sourceTree = "<group>"; };
5B7EB9410499542E8C5724F5 /* Pods-HelloWorld-HelloWorldTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HelloWorld-HelloWorldTests.debug.xcconfig"; path = "Target Support Files/Pods-HelloWorld-HelloWorldTests/Pods-HelloWorld-HelloWorldTests.debug.xcconfig"; sourceTree = "<group>"; };
5DCACB8F33CDC322A6C60F78 /* libPods-HelloWorld.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-HelloWorld.a"; sourceTree = BUILT_PRODUCTS_DIR; };
767CEB952B56C0F3000139AD /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AppDelegate.swift; path = VisionAppTestSUI/AppDelegate.swift; sourceTree = "<group>"; };
767CEB972B56C0FA000139AD /* App.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = App.swift; path = VisionAppTestSUI/App.swift; sourceTree = "<group>"; };
767CEBBB2B582F6B000139AD /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AppDelegate.swift; path = HelloWorld/AppDelegate.swift; sourceTree = "<group>"; };
767CEBBD2B582F78000139AD /* App.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = App.swift; path = HelloWorld/App.swift; sourceTree = "<group>"; };
89C6BE57DB24E9ADA2F236DE /* Pods-HelloWorld-HelloWorldTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HelloWorld-HelloWorldTests.release.xcconfig"; path = "Target Support Files/Pods-HelloWorld-HelloWorldTests/Pods-HelloWorld-HelloWorldTests.release.xcconfig"; sourceTree = "<group>"; };
ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -83,8 +83,8 @@
13B07FAE1A68108700A75B9A /* HelloWorld */ = {
isa = PBXGroup;
children = (
767CEB952B56C0F3000139AD /* AppDelegate.swift */,
767CEB972B56C0FA000139AD /* App.swift */,
767CEBBB2B582F6B000139AD /* AppDelegate.swift */,
767CEBBD2B582F78000139AD /* App.swift */,
13B07FB51A68108700A75B9A /* Images.xcassets */,
13B07FB61A68108700A75B9A /* Info.plist */,
);
Expand Down Expand Up @@ -386,8 +386,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
767CEB962B56C0F3000139AD /* AppDelegate.swift in Sources */,
767CEB982B56C0FA000139AD /* App.swift in Sources */,
767CEBBC2B582F6B000139AD /* AppDelegate.swift in Sources */,
767CEBBE2B582F78000139AD /* App.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 1 addition & 3 deletions packages/react-native/template/visionos/HelloWorld/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ struct HelloWorldApp: App {
@UIApplicationDelegateAdaptor var delegate: AppDelegate

var body: some Scene {
WindowGroup {
RCTRootViewRepresentable(moduleName: "HelloWorld", initialProps: nil)
}
RCTMainWindow(moduleName: "HelloWorld")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class AppDelegate: RCTAppDelegate {

override func bundleURL() -> URL? {
#if DEBUG
RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index")
RCTBundleURLProvider.sharedSettings()?.jsBundleURL(forBundleRoot: "index")
#else
Bundle.main.url(forResource: "main", withExtension: "jsbundle")
#endif
Expand Down
102 changes: 52 additions & 50 deletions packages/rn-tester/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,8 @@ PODS:
- React-jsi
- React-NativeModulesApple
- ReactCommon
- React-RCTSwiftExtensions (1000.0.0)
- React-RCTSwiftExtensions (1000.0.0):
- React-Core
- React-RCTTest (1000.0.0):
- RCT-Folly (= 2024.01.01.00)
- React-Core (= 1000.0.0)
Expand Down Expand Up @@ -1419,65 +1420,66 @@ CHECKOUT OPTIONS:
SPEC CHECKSUMS:
boost: 8f1e9b214fa11f71081fc8ecd5fad3daf221cf7f
DoubleConversion: 71bf0761505a44e4dfddc0aa04afa049fdfb63b5
FBLazyVector: 5fdead7fff97d43aacf320bcccb748cf8527af07
FBLazyVector: e34ddc4948ab5a9f857262cf68a46d39d8c61ed1
fmt: 5d9ffa7ccba126c08b730252123601d514652320
glog: 4f05d17aa39a829fee878689fc9a41af587fabba
hermes-engine: 3fed7e58e811ae8f795063cc6450714395c0276d
MyNativeView: 534e99e9c5dfd0bae242bdb06bb72e11d720c9a2
NativeCxxModuleExample: 107af3af8f5ce8802037937aabf1872ac891ad43
OCMock: 267d92c078398b7ce11d99e811e3a402744c06bc
RCT-Folly: d8c13e288297f63c0db8f083cfedebdd2649a299
RCT-Folly: 70c792c856324d6a518af75b3a307c14c226343a
RCTDeprecation: 3808e36294137f9ee5668f4df2e73dc079cd1dcf
RCTRequired: 6b32de28a5215a5990284d541bbdec8b5e3c78bf
RCTTypeSafety: 0e2bb3047d531a60b28b1d0d63e0c27a742a3019
React: a6f80cd5ba07887121f8b480991e17041a838f5c
React-callinvoker: d558dab7c4d987f1577838a50d59aeb069130d91
RCTRequired: 60c97b0b0cc33a209d33b3412eb1a218cbc1b82c
RCTTypeSafety: 2f597b431b910be1e49c6e6f40dd6879f6d1bdfd
React: dc827cd6966c06176ea9664cb4d74d63d1a4fcca
React-callinvoker: 3727946c98d5626aef39ac6a45b9d1be5db35527
React-Codegen: 04a9ac6fd2f392534ed432102c6cbf117dcbf62b
React-Core: 6403b0d9390e408017e82dc7823e059c53186141
React-CoreModules: 6406001452f46e9234a2fac3861a60786285acc0
React-cxxreact: f917d87380150aaaf8053fb4280358aad33c9b71
React-debug: c12ba28faf2c0aace7cbc81d7ac7661075976460
React-Fabric: 1d2070e13efd26fa34bb67cf484794265402ac06
React-FabricImage: 238905c6db0cf533d562b52ae936bfd7ed5f00dc
React-graphics: c430227a30690a86be35a8fb8c4c36ff312ff4bc
React-hermes: f6b54c8b4553fc7a8926f3e46459923a834a99d4
React-ImageManager: 38795ba06968bbcb11343b9e1123557af34c6270
React-jserrorhandler: fba402f8090b05e29c57c7bb111413e12e89aebe
React-jsi: d5643763df3910249aac671c9b7d634e6e96af7b
React-jsiexecutor: 847a90333a8840846652833aa4e291e433277aec
React-jsinspector: c2abef5095276eeb09c4b871f3024851d2ac3e58
React-jsitracing: 3f24b1171cc7aa986555a84e1036d3a6ba98bc84
React-logger: 4b1ded6e5707b276ffd58505987aba8dd1c0c3e4
React-Mapbuffer: b562329b31a9a5e3f18a8b4632c4fa8945a504e8
React-nativeconfig: 86b0961752d6a00ccc30418bfb5bfb5ebda84149
React-NativeModulesApple: eb4d247f32f25df62d1e979721502203db3de17c
React-perflogger: 31792e118e9c4785739c1823a481d64ba3d512b9
React-RCTActionSheet: a79621f1907340e31a66a4ad5e89175ef13f4215
React-RCTAnimation: 1f6948befc38688ecd5e78b7afd7d356aafa79b3
React-RCTAppDelegate: 9b0ecae938c894f4678a5e4726c03a06e92b0d93
React-RCTBlob: afaf18b4618c4d71095dfae9fca7c548dbcf0da0
React-RCTFabric: 0e96635634399a9ccf6c3c7fb18d5bac8e2561d5
React-RCTImage: 280f6f62a88f13253013ab5709e7bc60259cc081
React-RCTLinking: 9757d922aaa9aea2baa6e6178a8b1f08c1a85add
React-RCTNetwork: 92e96ad358d83b5a809e6222987c77106f44691e
React-RCTPushNotification: 36ec0c6d3ab1195665c49c0cfd689864401cc111
React-RCTSettings: 758a24aefbee012d903ca4f80532e95edfe2b516
React-RCTTest: a30b88c8910a259ef769af7654b1812668019b22
React-RCTText: ff85a26056ca9a34198934774e0f75ac2305c2c0
React-RCTVibration: 5086b14f98c2255e71cc6a2213b6f67338dcdff4
React-rendererdebug: 6e0021971efb40a76f807b913fdba6ec3ecabbd0
React-rncore: 27b4d75d116e9ae44292dc26aecff5bbad9e6996
React-RuntimeApple: c18cc85bf03a322efd2a76fd48f4c693d6901e67
React-RuntimeCore: 631803674e2884b5d866aacac96850191a081e1a
React-runtimeexecutor: a6b1c771b522e49e6906c5354b0e8859a64e7b3e
React-RuntimeHermes: 1473abdead583d8ab788ebd31de9c8e81e19d0fc
React-runtimescheduler: 1b905cb65942a3bfdff3f003c9c778b6b46afca6
React-utils: a5c7207b3bd558fdbfc553ea7a7731fbfc7f3552
ReactCommon: 2c87b20987de3a37d3993e4c1cab34fb97c28899
ReactCommon-Samples: e95d8d284b23ed4eda37498eaa460b6468c24699
React-Core: eb5002d75c263aaa042b97853effd3b6e2f8668c
React-CoreModules: 1eb22960e980f9e0b80542da464c5935260fd180
React-cxxreact: eb5eb55bffe0ccff4cbf192a04c978e37f137e8f
React-debug: 1d5a869d405ca2410bfc95c7fe340220331696ef
React-Fabric: 555fc115322e9b37b8cc1d4ac0ab8d4a87718077
React-FabricImage: 657d05951f1a512496713d7c2d534d347915fe3b
React-graphics: 989577b97b4d5e8f3cd3ff86b275eb7e4c7ff657
React-hermes: 0003c9f0f623ed87224f4415c567f90f0d15737f
React-ImageManager: ec80ccfd373893be8c347a23a693438b41a86e2a
React-jserrorhandler: c4126c1c2fb42540cf64fdc80917214a0c6ca905
React-jsi: 81558ed18c4e91824280a3298d75d598ef4cf3fa
React-jsiexecutor: 139ad70390e61c6b00779d82c7d5f490cd3eb632
React-jsinspector: bd27899ffc61660559bfbc8872e8d8de05b2612d
React-jsitracing: 9639d0659449fa9a71f31e28d7dde349bcec3394
React-logger: 142bc8479d40940294428f652fb016b809335121
React-Mapbuffer: 2762ea21725665466ea54f09848886f5ba7e97f7
React-nativeconfig: f984aee5151a69d520ea830756b33c62acc5c269
React-NativeModulesApple: 5fd38cf3bd4aca4adc9522a7a53681001f98f29d
React-perflogger: b19adeb816c255a04dff9528cf3f6bf209188d17
React-RCTActionSheet: c07c3924b6a94602f40effc130b776faa8b6bab1
React-RCTAnimation: 51f5a6be1479336b26d0cb282bd0e8f1feabf593
React-RCTAppDelegate: 3cac28ed63e10086624a2d965ce2c3359bbe04b0
React-RCTBlob: 0d6f17d4cacc18b6872b06b232186b176d4a8a18
React-RCTFabric: 8b955856519621a30a8425ac6c594abb66788839
React-RCTImage: c69ac72c257475c7860602ac11beaabd7862b99c
React-RCTLinking: c3f99ac575655569f5bc108ff13b816dcdf1dbfd
React-RCTNetwork: 39405b5687d5b16fdcc6466713930e71f8389cde
React-RCTPushNotification: 8b6b936f8e07096db581a446d4f9844ff8b35925
React-RCTSettings: 96dad99a283d67c573b8ba3709725901e8ac3108
React-RCTSwiftExtensions: b372ca0daf79796645c691841b470ad5d37bba64
React-RCTTest: 11be8ba66b91bc914d31f2b7c9abd0c39d47a3d7
React-RCTText: 8ca2156c782dc33ceb3fa80ce8c20cfcccbcc9d1
React-RCTVibration: 284a9575d09b5c5517d5d5b8b86d4e2d3c324b5e
React-rendererdebug: d664023e12af482bb3911ce547c522ddbbf01ac5
React-rncore: 5917d667ddd77e1938a9d1dabf5c503e427f9ec9
React-RuntimeApple: 31c6a97b53e53ea3e0d071ca45471b9152d22dc3
React-RuntimeCore: 25febc097f3f7d4043562fcf5c7bf406758ad1ce
React-runtimeexecutor: b66459278d0c6564b5d877107e0bb3ac953a8221
React-RuntimeHermes: 3f46517238fed32a04b36c6bc41a4ac8508baf6e
React-runtimescheduler: 04123aa94f0fa33398b9e58cd4345cac230eaee7
React-utils: bdfeb70a54b7b73533de6c8b679143f75c34c86a
ReactCommon: 8f6a345ebd9558892a83e1be6b83bdad4fd99078
ReactCommon-Samples: 090dcc2659d35c5577955d89efac618595a72e61
ScreenshotManager: a5f596498de38f3cf3377df636ca67355503f190
SocketRocket: 0ba3e799f983d2dfa878777017659ef6c866e5c6
Yoga: e4691eb7881cae15f847654cf06b0f7962707af0
Yoga: ac56a983bdf421a579c197143f79aa568f1c74a1

PODFILE CHECKSUM: 7e999b8158f1055609ef4491bc35f1ad658fdd6c

Expand Down
4 changes: 1 addition & 3 deletions packages/rn-tester/RNTester-visionOS/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ struct RNTesterApp: App {
@UIApplicationDelegateAdaptor var delegate: AppDelegate

var body: some Scene {
WindowGroup {
RCTRootViewRepresentable(moduleName: "RNTesterApp", initialProps: nil)
}
RCTMainWindow(moduleName: "RNTesterApp")
}
}
2 changes: 1 addition & 1 deletion packages/rn-tester/RNTester-visionOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class AppDelegate: RCTAppDelegate {
}

override func bundleURL() -> URL? {
RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "js/RNTesterApp.ios")
RCTBundleURLProvider.sharedSettings()?.jsBundleURL(forBundleRoot: "js/RNTesterApp.ios")
}
}

0 comments on commit 76e3d6a

Please sign in to comment.