Skip to content
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

chore: refactor android event dispatching #197

Merged
merged 1 commit into from
Dec 12, 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
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
Loading