|
| 1 | +import React |
| 2 | +import React |
| 3 | +import React_RCTAppDelegate |
| 4 | +import ReactAppDependencyProvider |
| 5 | + |
| 6 | +@objc public class ReactNativeBrownfield: RCTDefaultReactNativeFactoryDelegate { |
| 7 | + @objc public static let shared = ReactNativeBrownfield() |
| 8 | + |
| 9 | + @objc public var entryFile: String |
| 10 | + @objc public var fallbackResource: String? |
| 11 | + @objc public var bundlePath: String |
| 12 | + @objc public var reactNativeFactory: RCTReactNativeFactory? |
| 13 | + private var onBundleLoaded: (() -> Void)? |
| 14 | + |
| 15 | + private override init() { |
| 16 | + self.entryFile = "index" |
| 17 | + self.fallbackResource = nil |
| 18 | + self.bundlePath = "main.jsbundle" |
| 19 | + super.init() |
| 20 | + } |
| 21 | + |
| 22 | + @objc public func startReactNative() { |
| 23 | + startReactNative(onBundleLoaded: nil) |
| 24 | + } |
| 25 | + |
| 26 | + @objc public func startReactNative(onBundleLoaded: (() -> Void)?) { |
| 27 | + startReactNative(onBundleLoaded: onBundleLoaded, launchOptions: nil) |
| 28 | + } |
| 29 | + |
| 30 | + @objc public func startReactNative(onBundleLoaded: (() -> Void)?, launchOptions: [AnyHashable: Any]?) { |
| 31 | + guard reactNativeFactory == nil else { return } |
| 32 | + |
| 33 | + self.dependencyProvider = RCTAppDependencyProvider() |
| 34 | + self.reactNativeFactory = RCTReactNativeFactory(delegate: self) |
| 35 | + |
| 36 | + if let onBundleLoaded = onBundleLoaded { |
| 37 | + self.onBundleLoaded = onBundleLoaded |
| 38 | + NotificationCenter.default.addObserver(self, |
| 39 | + selector: #selector(jsLoaded), |
| 40 | + name: NSNotification.Name("RCTJavaScriptDidLoadNotification"), |
| 41 | + object: nil) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @objc private func jsLoaded(_ notification: Notification) { |
| 46 | + onBundleLoaded?() |
| 47 | + onBundleLoaded = nil |
| 48 | + NotificationCenter.default.removeObserver(self) |
| 49 | + } |
| 50 | + |
| 51 | + // MARK: - RCTBridgeDelegate Methods |
| 52 | + |
| 53 | + @objc public override func sourceURL(for bridge: RCTBridge) -> URL? { |
| 54 | + return bundleURL() |
| 55 | + } |
| 56 | + |
| 57 | + public override func bundleURL() -> URL? { |
| 58 | +#if DEBUG |
| 59 | + return RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: entryFile) |
| 60 | +#else |
| 61 | + let resourceURLComponents = bundlePath.components(separatedBy: ".") |
| 62 | + let withoutLast = resourceURLComponents[..<(resourceURLComponents.count - 1)] |
| 63 | + let resourceName = withoutLast.joined() |
| 64 | + let fileExtension = resourceURLComponents.last ?? "" |
| 65 | + |
| 66 | + return Bundle.main.url(forResource: resourceName, withExtension: fileExtension) |
| 67 | +#endif |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +extension Notification.Name { |
| 72 | + public static let popToNative = Notification.Name("PopToNativeNotification") |
| 73 | + public static let togglePopGestureRecognizer = Notification.Name("TogglePopGestureRecognizerNotification") |
| 74 | +} |
0 commit comments