Hello for SwiftUI
This commit is contained in:
parent
14aa6a777b
commit
4e4a92c2a3
5 changed files with 75 additions and 33 deletions
24
ios/Native/Application/RootView.swift
Normal file
24
ios/Native/Application/RootView.swift
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
16
ios/Native/Application/SharedState.swift
Normal file
16
ios/Native/Application/SharedState.swift
Normal 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?
|
||||
}
|
29
ios/Native/Application/UIReactNativeView.swift
Normal file
29
ios/Native/Application/UIReactNativeView.swift
Normal 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) {
|
||||
|
||||
}
|
||||
}
|
|
@ -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")
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
import UIKit
|
||||
import SwiftUI
|
||||
import React
|
||||
import React_RCTAppDelegate
|
||||
import ReactAppDependencyProvider
|
||||
|
@ -8,7 +9,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
|||
var window: UIWindow?
|
||||
|
||||
var reactNativeDelegate: ReactNativeDelegate?
|
||||
var reactNativeFactory: RCTReactNativeFactory?
|
||||
private let sharedState = SharedState()
|
||||
|
||||
func application(
|
||||
_ application: UIApplication,
|
||||
|
@ -19,7 +20,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
|||
delegate.dependencyProvider = RCTAppDependencyProvider()
|
||||
|
||||
reactNativeDelegate = delegate
|
||||
reactNativeFactory = factory
|
||||
sharedState.reactNativeFactory = factory
|
||||
|
||||
window = UIWindow(frame: UIScreen.main.bounds)
|
||||
|
||||
|
@ -34,10 +35,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
|||
}
|
||||
|
||||
func startNativeApp() {
|
||||
guard let window, let reactNativeFactory else {
|
||||
guard let window, sharedState.reactNativeFactory != nil else {
|
||||
return
|
||||
}
|
||||
window.rootViewController = UIReactNativeHostingViewController(reactNativeFactory)
|
||||
|
||||
window.rootViewController = UIHostingController(rootView: RootView(sharedState))
|
||||
window.makeKeyAndVisible()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue