Skip to content

Commit

Permalink
chore: refactor android event dispatching
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Dec 12, 2024
1 parent 2526db9 commit 3297059
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-terms-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-native-bottom-tabs': patch
---

chore: refactor android event dispatching
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,8 +31,8 @@ class ReactBottomNavigationView(context: Context) : BottomNavigationView(context
private val iconSources: MutableMap<Int, ImageSource> = mutableMapOf()
private var isLayoutEnqueued = false
var items: MutableList<TabInfo>? = 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)
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,12 @@ class RCTTabViewManager(context: ReactApplicationContext) : SimpleViewManager<Re
public override fun createViewInstance(context: ThemedReactContext): ReactBottomNavigationView {
eventDispatcher = context.getNativeModule(UIManagerModule::class.java)!!.eventDispatcher
val view = ReactBottomNavigationView(context)
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
}
Expand Down

0 comments on commit 3297059

Please sign in to comment.