Hello for SwiftUI

This commit is contained in:
Artur Gurgul 2025-08-02 13:00:06 +02:00
parent 14aa6a777b
commit 4e4a92c2a3
5 changed files with 75 additions and 33 deletions

View file

@ -0,0 +1,24 @@
//
// RootView.swift
// RNPlayground
//
// Created by Artur Gurgul on 02/08/2025.
//
import SwiftUI
struct RootView: View {
private let sharedState: SharedState
init(_ sharedState: SharedState) {
self.sharedState = sharedState
}
var body: some View {
VStack {
Text("Hello to SwiftUI!")
if let reactNativeFactory = sharedState.reactNativeFactory {
UIReactNativeView(reactNativeFactory)
}
}
}
}

View file

@ -0,0 +1,16 @@
//
// SharedState.swift
// RNPlayground
//
// Created by Artur Gurgul on 02/08/2025.
//
import SwiftUI
import Combine
import React
import React_RCTAppDelegate
import ReactAppDependencyProvider
final class SharedState: ObservableObject {
var reactNativeFactory: RCTReactNativeFactory?
}

View file

@ -0,0 +1,29 @@
//
// UIReactNativeView.swift
// RNPlayground
//
// Created by Artur Gurgul on 01/08/2025.
//
import UIKit
import SwiftUI
import React
import React_RCTAppDelegate
import ReactAppDependencyProvider
struct UIReactNativeView: UIViewRepresentable {
var factory: RCTReactNativeFactory
init(_ reactNativeFactory: RCTReactNativeFactory) {
factory = reactNativeFactory
}
func makeUIView(context: Context) -> UIView {
return factory.rootViewFactory.view(withModuleName: "RNPlayground")
}
func updateUIView(_ uiView: UIView, context: Context) {
}
}

View file

@ -1,29 +0,0 @@
//
// UIReactNativeHostingViewController.swift
// RNPlayground
//
// Created by Artur Gurgul on 01/08/2025.
//
import UIKit
import React
import React_RCTAppDelegate
import ReactAppDependencyProvider
class UIReactNativeHostingViewController: UIViewController {
var factory: RCTReactNativeFactory
init(_ reactNativeFactory: RCTReactNativeFactory) {
self.factory = reactNativeFactory
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
view = factory.rootViewFactory.view(withModuleName: "RNPlayground")
}
}

View file

@ -1,4 +1,5 @@
import UIKit import UIKit
import SwiftUI
import React import React
import React_RCTAppDelegate import React_RCTAppDelegate
import ReactAppDependencyProvider import ReactAppDependencyProvider
@ -8,7 +9,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? var window: UIWindow?
var reactNativeDelegate: ReactNativeDelegate? var reactNativeDelegate: ReactNativeDelegate?
var reactNativeFactory: RCTReactNativeFactory? private let sharedState = SharedState()
func application( func application(
_ application: UIApplication, _ application: UIApplication,
@ -19,7 +20,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
delegate.dependencyProvider = RCTAppDependencyProvider() delegate.dependencyProvider = RCTAppDependencyProvider()
reactNativeDelegate = delegate reactNativeDelegate = delegate
reactNativeFactory = factory sharedState.reactNativeFactory = factory
window = UIWindow(frame: UIScreen.main.bounds) window = UIWindow(frame: UIScreen.main.bounds)
@ -34,10 +35,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
} }
func startNativeApp() { func startNativeApp() {
guard let window, let reactNativeFactory else { guard let window, sharedState.reactNativeFactory != nil else {
return return
} }
window.rootViewController = UIReactNativeHostingViewController(reactNativeFactory)
window.rootViewController = UIHostingController(rootView: RootView(sharedState))
window.makeKeyAndVisible() window.makeKeyAndVisible()
} }
} }