diff --git a/App.tsx b/App.tsx index f53eb51..0175c62 100644 --- a/App.tsx +++ b/App.tsx @@ -1,6 +1,6 @@ import { NewAppScreen } from '@react-native/new-app-screen' import { useEffect, useState } from 'react' -import { StatusBar, Text, StyleSheet, useColorScheme, View } from 'react-native' +import { StatusBar, Text, StyleSheet, useColorScheme, ScrollView } from 'react-native' import { NativeEventEmitter, NativeModules } from 'react-native' const { Emitter } = NativeModules @@ -16,12 +16,17 @@ const BaseButton = requireNativeComponent('BaseButton') export default function App() { const isDarkMode = useColorScheme() === 'dark' const [message, setMessage] = useState(null) + const [color, setColor] = useState("#FFF") useEffect(() => { const emitter = new NativeEventEmitter(Emitter); const subscription = emitter.addListener('onMessage', (event) => { if (event?.message) { setMessage(event.message) + + if (event?.message == "hello from Swift!") { + setColor("#00F") + } } }) @@ -31,13 +36,12 @@ export default function App() { }, []) return ( - + {message ?? 'Waiting for message...'} - - + ); } diff --git a/ios/Native/Application/BaseButton/BaseButton.swift b/ios/Native/Application/BaseButton/BaseButton.swift index 09fd0d4..db53a43 100644 --- a/ios/Native/Application/BaseButton/BaseButton.swift +++ b/ios/Native/Application/BaseButton/BaseButton.swift @@ -8,9 +8,11 @@ import SwiftUI struct BaseButton: View { + @EnvironmentObject var sharedState: SharedState + var body: some View { Button("SwiftUI Button") { - + sharedState.text1 = "Clicked in SwiftUI button that was created in RN" } } } diff --git a/ios/Native/Application/BaseButton/BaseButtonManager.swift b/ios/Native/Application/BaseButton/BaseButtonManager.swift index 6138a84..ecd9b15 100644 --- a/ios/Native/Application/BaseButton/BaseButtonManager.swift +++ b/ios/Native/Application/BaseButton/BaseButtonManager.swift @@ -12,7 +12,9 @@ import SwiftUI @objc(BaseButtonManager) class BaseButtonManager: RCTViewManager { override func view() -> UIView! { - return UIHostingController(rootView: BaseButton()).view + return UIHostingController( + rootView: BaseButton() + ).view } override static func requiresMainQueueSetup() -> Bool { diff --git a/ios/Native/Application/Shared/Emitter/Emitter.swift b/ios/Native/Application/Shared/Emitter/Emitter.swift index 1f66fd1..f4f6b32 100644 --- a/ios/Native/Application/Shared/Emitter/Emitter.swift +++ b/ios/Native/Application/Shared/Emitter/Emitter.swift @@ -7,8 +7,6 @@ import Combine import React -//import React_RCTAppDelegate -//import ReactAppDependencyProvider @objc(Emitter) class Emitter: RCTEventEmitter { @@ -19,7 +17,6 @@ class Emitter: RCTEventEmitter { override init() { super.init() EventEmitter.sharedInstance.register(eventEmitter: self) - } override func supportedEvents() -> [String]! { diff --git a/ios/Native/Application/Shared/SharedState.swift b/ios/Native/Application/Shared/SharedState.swift index cc073ba..114cf64 100644 --- a/ios/Native/Application/Shared/SharedState.swift +++ b/ios/Native/Application/Shared/SharedState.swift @@ -15,6 +15,9 @@ final class SharedState: ObservableObject { var reactNativeFactory: RCTReactNativeFactory? private let emitter = EventEmitter.sharedInstance + // SwiftUI => RN => SwiftUI + @Published var text1: String = "" + func send(message: String) { emitter.send(message: message) } diff --git a/ios/Native/Application/Views/ToolboxHeader.swift b/ios/Native/Application/Views/ToolboxHeader.swift index bee5d0c..59310b1 100644 --- a/ios/Native/Application/Views/ToolboxHeader.swift +++ b/ios/Native/Application/Views/ToolboxHeader.swift @@ -11,12 +11,14 @@ struct ToolboxHeader: View { @EnvironmentObject var sharedState: SharedState var body: some View { - HStack { - Text("Actions") - Button("Make it blue") { - print("Making it blue") - sharedState.send(message: "hello from Swift!") + VStack { + HStack { + Text("Actions") + Button("Make it blue") { + sharedState.send(message: "hello from Swift!") + } } + Text("SwiftUI => RN => SwiftUI: \(sharedState.text1)") } } }