Add emitter so I can now send events to the RN core
This commit is contained in:
parent
4e4a92c2a3
commit
7177b9e927
7 changed files with 111 additions and 14 deletions
35
App.tsx
35
App.tsx
|
@ -1,19 +1,30 @@
|
||||||
/**
|
import { NewAppScreen } from '@react-native/new-app-screen'
|
||||||
* Sample React Native App
|
import { useEffect, useState } from 'react'
|
||||||
* https://github.com/facebook/react-native
|
import { StatusBar, Text, StyleSheet, useColorScheme, View } from 'react-native'
|
||||||
*
|
import { NativeEventEmitter, NativeModules } from 'react-native'
|
||||||
* @format
|
const { Emitter } = NativeModules;
|
||||||
*/
|
|
||||||
|
|
||||||
import { NewAppScreen } from '@react-native/new-app-screen';
|
export default function App() {
|
||||||
import { StatusBar, StyleSheet, useColorScheme, View } from 'react-native';
|
const isDarkMode = useColorScheme() === 'dark'
|
||||||
|
const [message, setMessage] = useState(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const emitter = new NativeEventEmitter(Emitter);
|
||||||
|
const subscription = emitter.addListener('onMessage', (event) => {
|
||||||
|
if (event?.message) {
|
||||||
|
setMessage(event.message)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
function App() {
|
return () => {
|
||||||
const isDarkMode = useColorScheme() === 'dark';
|
subscription.remove()
|
||||||
|
};
|
||||||
|
}, [])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
<StatusBar barStyle={isDarkMode ? 'light-content' : 'dark-content'} />
|
<StatusBar barStyle={isDarkMode ? 'light-content' : 'dark-content'} />
|
||||||
|
<Text>{message ?? 'Waiting for message...'}</Text>
|
||||||
<NewAppScreen templateFileName="App.tsx" />
|
<NewAppScreen templateFileName="App.tsx" />
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
|
@ -23,6 +34,4 @@ const styles = StyleSheet.create({
|
||||||
container: {
|
container: {
|
||||||
flex: 1,
|
flex: 1,
|
||||||
},
|
},
|
||||||
});
|
})
|
||||||
|
|
||||||
export default App;
|
|
||||||
|
|
|
@ -15,10 +15,11 @@ struct RootView: View {
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
VStack {
|
||||||
Text("Hello to SwiftUI!")
|
ToolboxHeader()
|
||||||
if let reactNativeFactory = sharedState.reactNativeFactory {
|
if let reactNativeFactory = sharedState.reactNativeFactory {
|
||||||
UIReactNativeView(reactNativeFactory)
|
UIReactNativeView(reactNativeFactory)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.environmentObject(sharedState)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
32
ios/Native/Application/Shared/Emitter/Emitter.swift
Normal file
32
ios/Native/Application/Shared/Emitter/Emitter.swift
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
//
|
||||||
|
// Emitter.swift
|
||||||
|
// RNPlayground
|
||||||
|
//
|
||||||
|
// Created by Artur Gurgul on 02/08/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Combine
|
||||||
|
import React
|
||||||
|
//import React_RCTAppDelegate
|
||||||
|
//import ReactAppDependencyProvider
|
||||||
|
|
||||||
|
@objc(Emitter)
|
||||||
|
class Emitter: RCTEventEmitter {
|
||||||
|
override static func requiresMainQueueSetup() -> Bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
override init() {
|
||||||
|
super.init()
|
||||||
|
EventEmitter.sharedInstance.register(eventEmitter: self)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override func supportedEvents() -> [String]! {
|
||||||
|
return ["onMessage"]
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc func send(message: String) {
|
||||||
|
sendEvent(withName: "onMessage", body: ["message": message])
|
||||||
|
}
|
||||||
|
}
|
28
ios/Native/Application/Shared/Emitter/EventEmitter.swift
Normal file
28
ios/Native/Application/Shared/Emitter/EventEmitter.swift
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
//
|
||||||
|
// EventEmitter.swift
|
||||||
|
// RNPlayground
|
||||||
|
//
|
||||||
|
// Created by Artur Gurgul on 02/08/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
class EventEmitter {
|
||||||
|
static let sharedInstance = EventEmitter()
|
||||||
|
|
||||||
|
private var eventEmitter: Emitter?
|
||||||
|
|
||||||
|
private init() {}
|
||||||
|
|
||||||
|
func register(eventEmitter: Emitter) {
|
||||||
|
self.eventEmitter = eventEmitter
|
||||||
|
}
|
||||||
|
|
||||||
|
func send(message: String) {
|
||||||
|
eventEmitter?.send(message: message)
|
||||||
|
}
|
||||||
|
|
||||||
|
var isReady: Bool {
|
||||||
|
return eventEmitter != nil
|
||||||
|
}
|
||||||
|
}
|
0
ios/Native/Application/Shared/ModuleProvider.swift
Normal file
0
ios/Native/Application/Shared/ModuleProvider.swift
Normal file
|
@ -13,4 +13,9 @@ import ReactAppDependencyProvider
|
||||||
|
|
||||||
final class SharedState: ObservableObject {
|
final class SharedState: ObservableObject {
|
||||||
var reactNativeFactory: RCTReactNativeFactory?
|
var reactNativeFactory: RCTReactNativeFactory?
|
||||||
|
private let emitter = EventEmitter.sharedInstance
|
||||||
|
|
||||||
|
func send(message: String) {
|
||||||
|
emitter.send(message: message)
|
||||||
|
}
|
||||||
}
|
}
|
22
ios/Native/Application/Views/ToolboxHeader.swift
Normal file
22
ios/Native/Application/Views/ToolboxHeader.swift
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
//
|
||||||
|
// ToolboxHeader.swift
|
||||||
|
// RNPlayground
|
||||||
|
//
|
||||||
|
// Created by Artur Gurgul on 02/08/2025.
|
||||||
|
//
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
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!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue