Skip to content

[Android] Move to EventDispatcher instead of RCTEventEmitter #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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