Skip to content

Commit 5acd2c5

Browse files
chore: refactor
1 parent d181c65 commit 5acd2c5

14 files changed

Lines changed: 189 additions & 171 deletions

example/app.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"name": "MendixNativeExample",
3-
"displayName": "MendixNativeExample"
2+
"name": "App",
3+
"displayName": "App"
44
}

example/ios/MendixNativeExample/AppDelegate.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ import React
33
import MendixNative
44

55
@main
6-
class AppDelegate: ReactNative {
6+
class AppDelegate: ReactAppProvider {
7+
78
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
9+
super.setUpProvider()
810
super.application(application, didFinishLaunchingWithOptions: launchOptions)
911
changeRoot(to: Home())
1012
return true
1113
}
14+
15+
open override func bundleURL() -> URL? {
16+
return RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index")
17+
}
1218
}
1319

1420
class Home: UIViewController {
@@ -32,12 +38,6 @@ class Home: UIViewController {
3238
}
3339

3440
@objc func openApp() {
35-
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
36-
return
37-
}
38-
let reactRootView = appDelegate.rootViewFactory.view(withModuleName: "MendixNativeExample")
39-
let controller = UIViewController()
40-
controller.view = reactRootView
41-
appDelegate.changeRoot(to: controller)
41+
ReactAppProvider.shared()?.setReactViewController(UIViewController())
4242
}
4343
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Foundation
2+
3+
public class BundleHelper {
4+
public static func getJSBundleFile() -> URL? {
5+
if hasNativeOtaBundle(), let bundleUrl = OtaJSBundleFileProvider.getBundleUrl() {
6+
return bundleUrl
7+
}
8+
return Bundle.main.url(forResource: "index.ios", withExtension: "bundle", subdirectory: "Bundle")
9+
}
10+
11+
public static func hasNativeOtaBundle() -> Bool {
12+
return FileManager.default.contents(atPath: OtaHelpers.getOtaManifestFilepath()) != nil
13+
}
14+
}

ios/Modules/Helper/DevHelper.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React
2+
3+
public class DevHelper {
4+
5+
public static func showAppMenu() {
6+
devMenu?.show()
7+
}
8+
9+
public static func toggleElementInspector() {
10+
devSettings?.toggleElementInspector()
11+
}
12+
13+
public static var devSettings: RCTDevSettings? {
14+
return ReactHostManager.module(type: RCTDevSettings.self)
15+
}
16+
17+
public static var devMenu: RCTDevMenu? {
18+
return ReactHostManager.module(type: RCTDevMenu.self)
19+
}
20+
21+
public static func setDebugMode(enabled: Bool) {
22+
AppPreferences.remoteDebuggingEnabled = enabled
23+
devSettings?.isDebuggingRemotely = enabled
24+
}
25+
}

ios/Modules/Helper/KeychainHelper.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import Foundation
22

33
public class KeychainHelper {
44

5+
public static func clear() {
6+
clear(scopeKey: MxConfiguration.appName)
7+
}
8+
59
public static func clear(scopeKey: String?) {
610
let keys = [
711
Self.toAppScopeKey("token", scope: scopeKey),
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import UIKit
2+
import React
3+
import React_RCTAppDelegate
4+
import ReactAppDependencyProvider
5+
6+
open class ReactAppProvider: RCTAppDelegate {
7+
8+
var reactRootViewName: String = "App"
9+
10+
public func setUpProvider(moduleName: String = "App", reactRootViewName: String = "App") {
11+
self.moduleName = moduleName
12+
self.reactRootViewName = reactRootViewName
13+
automaticallyLoadReactNativeWindow = false
14+
dependencyProvider = RCTAppDependencyProvider()
15+
initialProps = [:]
16+
window = UIApplication
17+
.shared
18+
.connectedScenes
19+
.compactMap { $0 as? UIWindowScene }
20+
.first?
21+
.windows
22+
.first { $0.isKeyWindow } ?? UIWindow(frame: UIScreen.main.bounds)
23+
}
24+
25+
public override func sourceURL(for bridge: RCTBridge) -> URL? {
26+
return self.bundleURL()
27+
}
28+
29+
open override func bundleURL() -> URL? {
30+
return ReactNative.shared.bundleURL()
31+
}
32+
33+
public func setReactViewController(_ controller: UIViewController) {
34+
controller.view = reactAppView()
35+
changeRoot(to: controller)
36+
}
37+
38+
public func reactAppView() -> UIView? {
39+
let view = rootViewFactory.view(withModuleName: reactRootViewName)
40+
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
41+
view.frame = window.rootViewController?.view.frame ?? .zero
42+
return view
43+
}
44+
45+
public func startReactApp() {
46+
47+
}
48+
49+
public func stopReactApp() {
50+
}
51+
52+
public static func shared() -> ReactAppProvider? {
53+
return UIApplication.shared.delegate as? ReactAppProvider
54+
}
55+
56+
public func isReactAppActive() -> Bool {
57+
return bridge != nil
58+
}
59+
60+
public func changeRoot(to controller: UIViewController) {
61+
window.rootViewController = controller
62+
window.makeKeyAndVisible()
63+
}
64+
65+
public var rootView: UIView? {
66+
return window.rootViewController?.view
67+
}
68+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React
2+
3+
public class ReactHostManager {
4+
5+
public static var bridge: RCTBridge? {
6+
return ReactAppProvider.shared()?.bridge
7+
}
8+
9+
public static func module<T: NSObject>(type: T.Type) -> T? {
10+
return bridge?.moduleRegistry.module(for: type.self) as? T
11+
}
12+
13+
public static func module(name: String) -> Any? {
14+
return bridge?.moduleRegistry.module(forName: name)
15+
}
16+
17+
public static var isActive: Bool {
18+
return ReactAppProvider.shared()?.isReactAppActive() ?? false
19+
}
20+
}
21+

ios/Modules/Helper/StorageHelper.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import Foundation
22
import RNCAsyncStorage
33

4-
class StorageHelper {
4+
public class StorageHelper {
55

6-
static func clearAll() {
6+
public static func clearAll() {
77
if let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
88
clearDataAt(url: documentPath, component: MxConfiguration.filesDirectoryName)
99
}
@@ -17,7 +17,7 @@ class StorageHelper {
1717
}
1818
}
1919

20-
static func clearDataAt(url: URL, component: String) {
20+
public static func clearDataAt(url: URL, component: String) {
2121
let path = url.appendingPathComponent(component).path
2222
_ = NativeFsModule.remove(path, error: nil)
2323
}

ios/Modules/Helper/TapGestureRecognizerHelper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TapGestureRecognizerHelper {
1717
private var tapGestureRecognizer: UITapGestureRecognizer?
1818

1919
@objc private func handleTapAction(_ gestureRecognizer: UITapGestureRecognizer) {
20-
if gestureRecognizer.state == .ended && ReactNative.instance.isActive() , let onTapAction {
20+
if gestureRecognizer.state == .ended && ReactAppProvider.shared()?.isReactAppActive() == true , let onTapAction {
2121
onTapAction(gestureRecognizer)
2222
}
2323
}

ios/Modules/Helper/UnsafeMxFunction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ enum UnsafeMxFunction {
2020
}
2121

2222
var target: NSObject? {
23-
return ReactNative.instance.rootViewFactory.bridge?.moduleRegistry.module(forName: className) as? NSObject
23+
return ReactHostManager.module(name: className) as? NSObject
2424
}
2525

2626
func perform() {

0 commit comments

Comments
 (0)