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
|
@ -15,10 +15,11 @@ struct RootView: View {
|
|||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Text("Hello to SwiftUI!")
|
||||
ToolboxHeader()
|
||||
if let reactNativeFactory = sharedState.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 {
|
||||
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