1- import React
2- import React_RCTAppDelegate
3- import ReactAppDependencyProvider
1+ import UIKit
2+ internal import React
3+ internal import React_RCTAppDelegate
4+ internal import ReactAppDependencyProvider
45
5- @objc public class ReactNativeBrownfield : RCTDefaultReactNativeFactoryDelegate {
6- @objc public static let shared = ReactNativeBrownfield ( )
6+ class ReactNativeBrownfieldDelegate : RCTDefaultReactNativeFactoryDelegate {
7+ var entryFile = " index "
8+
9+ // MARK: - RCTReactNativeFactoryDelegate Methods
10+ override func sourceURL( for bridge: RCTBridge ) -> URL ? {
11+ return bundleURL ( )
12+ }
13+
14+ public override func bundleURL( ) -> URL ? {
15+ #if DEBUG
16+ return RCTBundleURLProvider . sharedSettings ( ) . jsBundleURL ( forBundleRoot: entryFile)
17+ #else
18+ let resourceURLComponents = bundlePath. components ( separatedBy: " . " )
19+ let withoutLast = resourceURLComponents [ ..< ( resourceURLComponents. count - 1 ) ]
20+ let resourceName = withoutLast. joined ( )
21+ let fileExtension = resourceURLComponents. last ?? " "
22+
23+ return Bundle . main. url ( forResource: resourceName, withExtension: fileExtension)
24+ #endif
25+ }
26+ }
27+
28+ public class ReactNativeBrownfield {
29+ public static let shared = ReactNativeBrownfield ( )
730 private var onBundleLoaded : ( ( ) -> Void ) ?
31+ private var delegate = ReactNativeBrownfieldDelegate ( )
832
933 /**
1034 * Path to JavaScript root.
1135 * Default value: "index"
1236 */
13- @objc public var entryFile : String = " index "
37+ public var entryFile : String = " index " {
38+ didSet {
39+ delegate. entryFile = entryFile
40+ }
41+ }
1442 /**
1543 * Path to bundle fallback resource.
1644 * Default value: nil
1745 */
18- @ objc public var fallbackResource : String ? = nil
46+ public var fallbackResource : String ? = nil
1947 /**
2048 * Path to JavaScript bundle file.
2149 * Default value: "main.jsbundle"
2250 */
23- @ objc public var bundlePath : String = " main.jsbundle "
51+ public var bundlePath : String = " main.jsbundle "
2452 /**
2553 * React Native factory instance created when starting React Native.
2654 * Default value: nil
2755 */
28- @ objc public var reactNativeFactory : RCTReactNativeFactory ? = nil
56+ private var reactNativeFactory : RCTReactNativeFactory ? = nil
2957 /**
3058 * Root view factory used to create React Native views.
3159 */
32- @ objc lazy public var rootViewFactory : RCTRootViewFactory ? = {
60+ lazy private var rootViewFactory : RCTRootViewFactory ? = {
3361 return reactNativeFactory? . rootViewFactory
3462 } ( )
3563
3664 /**
3765 * Starts React Native with default parameters.
3866 */
39- @ objc public func startReactNative( ) {
67+ public func startReactNative( ) {
4068 startReactNative ( onBundleLoaded: nil )
4169 }
42-
70+
71+ public func view(
72+ moduleName: String , initialProps: [ AnyHashable : Any ] ? ,
73+ launchOptions: [ AnyHashable : Any ] ?
74+ ) -> UIView ? {
75+ reactNativeFactory? . rootViewFactory. view (
76+ withModuleName: moduleName, initialProperties: initialProps,
77+ launchOptions: launchOptions
78+ )
79+ }
80+
4381 /**
4482 * Starts React Native with optional callback when bundle is loaded.
45- *
83+ *
4684 * @param onBundleLoaded Optional callback invoked after JS bundle is fully loaded.
4785 */
48- @ objc public func startReactNative( onBundleLoaded: ( ( ) -> Void ) ? ) {
86+ public func startReactNative( onBundleLoaded: ( ( ) -> Void ) ? ) {
4987 startReactNative ( onBundleLoaded: onBundleLoaded, launchOptions: nil )
5088 }
51-
89+
5290 /**
5391 * Starts React Native with optional callback and launch options.
54- *
92+ *
5593 * @param onBundleLoaded Optional callback invoked after JS bundle is fully loaded.
5694 * @param launchOptions Launch options, typically passed from AppDelegate.
5795 */
58- @ objc public func startReactNative( onBundleLoaded: ( ( ) -> Void ) ? , launchOptions: [ AnyHashable : Any ] ? ) {
96+ public func startReactNative( onBundleLoaded: ( ( ) -> Void ) ? , launchOptions: [ AnyHashable : Any ] ? ) {
5997 guard reactNativeFactory == nil else { return }
60-
61- self . dependencyProvider = RCTAppDependencyProvider ( )
62- self . reactNativeFactory = RCTReactNativeFactory ( delegate: self )
63-
98+
99+ delegate . dependencyProvider = RCTAppDependencyProvider ( )
100+ self . reactNativeFactory = RCTReactNativeFactory ( delegate: delegate )
101+
64102 if let onBundleLoaded {
65103 self . onBundleLoaded = onBundleLoaded
66104 if RCTIsNewArchEnabled ( ) {
@@ -80,31 +118,14 @@ import ReactAppDependencyProvider
80118 }
81119 }
82120 }
83-
121+
84122 @objc private func jsLoaded( _ notification: Notification ) {
85123 onBundleLoaded ? ( )
86124 onBundleLoaded = nil
87125 NotificationCenter . default. removeObserver ( self )
88126 }
89-
90- // MARK: - RCTReactNativeFactoryDelegate Methods
91-
92- @objc public override func sourceURL( for bridge: RCTBridge ) -> URL ? {
93- return bundleURL ( )
94- }
95-
96- public override func bundleURL( ) -> URL ? {
97- #if DEBUG
98- return RCTBundleURLProvider . sharedSettings ( ) . jsBundleURL ( forBundleRoot: entryFile)
99- #else
100- let resourceURLComponents = bundlePath. components ( separatedBy: " . " )
101- let withoutLast = resourceURLComponents [ ..< ( resourceURLComponents. count - 1 ) ]
102- let resourceName = withoutLast. joined ( )
103- let fileExtension = resourceURLComponents. last ?? " "
104-
105- return Bundle . main. url ( forResource: resourceName, withExtension: fileExtension)
106- #endif
107- }
127+
128+
108129}
109130
110131extension Notification . Name {
0 commit comments