Skip to content

Commit

Permalink
[Android] Move to EventDispatcher instead of RCTEventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
cortinico committed Apr 12, 2024
1 parent c9cfc7f commit 247e0ec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.reactnativemenu

import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.events.Event

class MenuOnPressActionEvent(
surfaceId: Int,
viewTag: Int,
private val event: String?,
private val target: Int
) : Event<MenuOnPressActionEvent>(surfaceId, viewTag) {

override fun getEventName(): String = "onPressAction"
override fun getEventData(): WritableMap = Arguments.createMap().apply {
if (event != null) {
putString("event", event)
}
putString("target", target.toString())
}
}
26 changes: 14 additions & 12 deletions android/src/main/java/com/reactnativemenu/MenuView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import android.text.style.ForegroundColorSpan
import android.view.*
import android.widget.PopupMenu
import com.facebook.react.bridge.*
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.events.Event
import com.facebook.react.uimanager.events.RCTEventEmitter
import com.facebook.react.views.view.ReactViewGroup
import java.lang.reflect.Field
Expand Down Expand Up @@ -165,14 +167,14 @@ class MenuView(private val mContext: ReactContext): ReactViewGroup(mContext) {
subMenuItem.setOnMenuItemClickListener {
if (!it.hasSubMenu()) {
mIsMenuDisplayed = false
val args: WritableMap = Arguments.createMap()
if (!subactions.isNull(it.order)) {
val selectedItem = subactions.getMap(it.order)
args.putString("event", selectedItem?.getString("id"))
args.putString("target", "$id")
mContext
.getJSModule(RCTEventEmitter::class.java)
.receiveEvent(id, "onPressAction", args)
val dispatcher =
UIManagerHelper.getEventDispatcherForReactTag(mContext, id)
val surfaceId: Int = UIManagerHelper.getSurfaceId(this)
dispatcher?.dispatchEvent(
MenuOnPressActionEvent(surfaceId, id, selectedItem.getString("id"), id)
)
}
true
} else {
Expand Down Expand Up @@ -210,14 +212,14 @@ class MenuView(private val mContext: ReactContext): ReactViewGroup(mContext) {
menuItem.setOnMenuItemClickListener {
if (!it.hasSubMenu()) {
mIsMenuDisplayed = false
val args: WritableMap = Arguments.createMap()
if (!mActions.isNull(it.order)) {
val selectedItem = mActions.getMap(it.order)
args.putString("event", selectedItem?.getString("id"))
args.putString("target", "$id")
mContext
.getJSModule(RCTEventEmitter::class.java)
.receiveEvent(id, "onPressAction", args)
val dispatcher =
UIManagerHelper.getEventDispatcherForReactTag(mContext, id)
val surfaceId: Int = UIManagerHelper.getSurfaceId(this)
dispatcher?.dispatchEvent(
MenuOnPressActionEvent(surfaceId, id, selectedItem.getString("id"), id)
)
}
true
} else {
Expand Down

0 comments on commit 247e0ec

Please sign in to comment.