From 1a7b392b9ad6db16c6055d8f0032a549f7f675f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwa=C5=9Bniewski?= Date: Thu, 12 Dec 2024 18:13:43 +0100 Subject: [PATCH] chore: refactor android event dispatching (#197) --- .changeset/smooth-terms-compete.md | 5 +++++ .../src/main/java/com/rcttabview/RCTTabView.kt | 16 ++++------------ .../android/src/newarch/RCTTabViewManager.kt | 12 ++++-------- .../android/src/oldarch/RCTTabViewManager.kt | 12 ++++-------- 4 files changed, 17 insertions(+), 28 deletions(-) create mode 100644 .changeset/smooth-terms-compete.md diff --git a/.changeset/smooth-terms-compete.md b/.changeset/smooth-terms-compete.md new file mode 100644 index 0000000..ff92c07 --- /dev/null +++ b/.changeset/smooth-terms-compete.md @@ -0,0 +1,5 @@ +--- +'react-native-bottom-tabs': patch +--- + +chore: refactor android event dispatching diff --git a/packages/react-native-bottom-tabs/android/src/main/java/com/rcttabview/RCTTabView.kt b/packages/react-native-bottom-tabs/android/src/main/java/com/rcttabview/RCTTabView.kt index 1ffdff5..08ef3cc 100644 --- a/packages/react-native-bottom-tabs/android/src/main/java/com/rcttabview/RCTTabView.kt +++ b/packages/react-native-bottom-tabs/android/src/main/java/com/rcttabview/RCTTabView.kt @@ -18,9 +18,7 @@ import android.widget.TextView import androidx.appcompat.content.res.AppCompatResources import coil3.ImageLoader import coil3.asDrawable -import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.ReadableArray -import com.facebook.react.bridge.WritableMap import com.facebook.react.common.assets.ReactFontManager import com.facebook.react.modules.core.ReactChoreographer import com.facebook.react.views.text.ReactTypefaceUtils @@ -33,8 +31,8 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context private val iconSources: MutableMap = mutableMapOf() private var isLayoutEnqueued = false var items: MutableList? = null - var onTabSelectedListener: ((WritableMap) -> Unit)? = null - var onTabLongPressedListener: ((WritableMap) -> Unit)? = null + var onTabSelectedListener: ((key: String) -> Unit)? = null + var onTabLongPressedListener: ((key: String) -> Unit)? = null private var activeTintColor: Int? = null private var inactiveTintColor: Int? = null private val checkedStateSet = intArrayOf(android.R.attr.state_checked) @@ -62,10 +60,7 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context private fun onTabLongPressed(item: MenuItem) { val longPressedItem = items?.firstOrNull { it.title == item.title } longPressedItem?.let { - val event = Arguments.createMap().apply { - putString("key", longPressedItem.key) - } - onTabLongPressedListener?.invoke(event) + onTabLongPressedListener?.invoke(longPressedItem.key) emitHapticFeedback(HapticFeedbackConstants.LONG_PRESS) } } @@ -93,10 +88,7 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context } val selectedItem = items?.first { it.title == item.title } selectedItem?.let { - val event = Arguments.createMap().apply { - putString("key", selectedItem.key) - } - onTabSelectedListener?.invoke(event) + onTabSelectedListener?.invoke(selectedItem.key) emitHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK) } } diff --git a/packages/react-native-bottom-tabs/android/src/newarch/RCTTabViewManager.kt b/packages/react-native-bottom-tabs/android/src/newarch/RCTTabViewManager.kt index a53c490..0d3935d 100644 --- a/packages/react-native-bottom-tabs/android/src/newarch/RCTTabViewManager.kt +++ b/packages/react-native-bottom-tabs/android/src/newarch/RCTTabViewManager.kt @@ -30,16 +30,12 @@ class RCTTabViewManager(context: ReactApplicationContext) : override fun createViewInstance(context: ThemedReactContext): ReactBottomNavigationView { val view = ReactBottomNavigationView(context) val eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag(context, view.id) - view.onTabSelectedListener = { data -> - data.getString("key")?.let { - eventDispatcher?.dispatchEvent(PageSelectedEvent(viewTag = view.id, key = it)) - } + view.onTabSelectedListener = { key -> + eventDispatcher?.dispatchEvent(PageSelectedEvent(viewTag = view.id, key)) } - view.onTabLongPressedListener = { data -> - data.getString("key")?.let { - eventDispatcher?.dispatchEvent(TabLongPressEvent(viewTag = view.id, key = it)) - } + view.onTabLongPressedListener = { key -> + eventDispatcher?.dispatchEvent(TabLongPressEvent(viewTag = view.id, key)) } return view diff --git a/packages/react-native-bottom-tabs/android/src/oldarch/RCTTabViewManager.kt b/packages/react-native-bottom-tabs/android/src/oldarch/RCTTabViewManager.kt index bb63108..8fce7a3 100644 --- a/packages/react-native-bottom-tabs/android/src/oldarch/RCTTabViewManager.kt +++ b/packages/react-native-bottom-tabs/android/src/oldarch/RCTTabViewManager.kt @@ -27,16 +27,12 @@ class RCTTabViewManager(context: ReactApplicationContext) : SimpleViewManager - data.getString("key")?.let { - eventDispatcher.dispatchEvent(PageSelectedEvent(viewTag = view.id, key = it)) - } + view.onTabSelectedListener = { key -> + eventDispatcher.dispatchEvent(PageSelectedEvent(viewTag = view.id, key)) } - view.onTabLongPressedListener = { data -> - data.getString("key")?.let { - eventDispatcher.dispatchEvent(TabLongPressEvent(viewTag = view.id, key = it)) - } + view.onTabLongPressedListener = { key -> + eventDispatcher.dispatchEvent(TabLongPressEvent(viewTag = view.id, key)) } return view }