Skip to content

Commit

Permalink
Address compilation warnings (#2597)
Browse files Browse the repository at this point in the history
## Description

It turns out that our code uses some deprecated functions. Also there were few more warnings, such as shadowed name or unused variables.

Some of included changes require digging into `react-native` source code to find alternative approach.

Fixes #2460 

## `dispatch` method

Based on @kkafar research in [screens](software-mansion/react-native-screens#1867), I've replaced `dispatch` method with `getEventData`.

- [Here](https://github.com/facebook/react-native/blob/e2ef6b98a061508bdd70a4c2747fe061c49b5c39/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/Event.java#L154-L168) you can find how `dispatch` looks like in the source code. 
- [Here](facebook/react-native@72d0ddc#diff-afc6f25f459dcf1e24a012e16862c869aa04b0de5795171efe14b7a88be08e64) you can see example change in react-native

## Test plan

Tested that our example apps (paper and fabric) work as expected.
  • Loading branch information
m-bert authored Mar 12, 2024
1 parent 6c924a4 commit 7e490f7
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.facebook.react.bridge.ReactContext
import com.facebook.react.uimanager.events.Event

class ReanimatedEventDispatcher {
@Suppress("UNUSED_PARAMETER", "COMMENT_IN_SUPPRESSION")
// This is necessary on new architecture
fun <T : Event<T>>sendEvent(event: T, reactApplicationContext: ReactContext) {
// no-op
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class RNGestureHandlerPackage : TurboReactPackage(), ViewManagerOnDemandReactPac
try {
val reactModuleInfoProviderClass =
Class.forName("com.swmansion.gesturehandler.RNGestureHandlerPackage$\$ReactModuleInfoProvider")
return reactModuleInfoProviderClass.newInstance() as ReactModuleInfoProvider
return reactModuleInfoProviderClass.getDeclaredConstructor().newInstance() as ReactModuleInfoProvider
} catch (e: ClassNotFoundException) {
return ReactModuleInfoProvider {
val reactModule: ReactModule = RNGestureHandlerModule::class.java.getAnnotation(ReactModule::class.java)!!
Expand All @@ -71,7 +71,7 @@ class RNGestureHandlerPackage : TurboReactPackage(), ViewManagerOnDemandReactPac
RNGestureHandlerModule::class.java.name,
reactModule.canOverrideExistingModule,
reactModule.needsEagerInit,
reactModule.hasConstants,
true, // Has constants is hardcoded to return true, so replacing it with `true` changes nothing.
reactModule.isCxxModule,
true
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,15 +835,15 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
private lateinit var pointerProps: Array<PointerProperties?>
private lateinit var pointerCoords: Array<PointerCoords?>
private fun initPointerProps(size: Int) {
var size = size
var pointerPropsSize = size
if (!Companion::pointerProps.isInitialized) {
pointerProps = arrayOfNulls(MAX_POINTERS_COUNT)
pointerCoords = arrayOfNulls(MAX_POINTERS_COUNT)
}
while (size > 0 && pointerProps[size - 1] == null) {
pointerProps[size - 1] = PointerProperties()
pointerCoords[size - 1] = PointerCoords()
size--
while (pointerPropsSize > 0 && pointerProps[pointerPropsSize - 1] == null) {
pointerProps[pointerPropsSize - 1] = PointerProperties()
pointerCoords[pointerPropsSize - 1] = PointerCoords()
pointerPropsSize--
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import com.facebook.react.ReactRootView

@Deprecated(message = "Use <GestureHandlerRootView /> component instead. Check gesture handler installation instructions in documentation for more information.")
class RNGestureHandlerEnabledRootView : ReactRootView {
constructor(context: Context?) : super(context) {}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

init {
throw UnsupportedOperationException("Your application is configured to use RNGestureHandlerEnabledRootView which is no longer supported. You can see how to migrate to <GestureHandlerRootView /> here: https://docs.swmansion.com/react-native-gesture-handler/docs/guides/migrating-off-rnghenabledroot")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.core.util.Pools
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.events.Event
import com.facebook.react.uimanager.events.RCTEventEmitter
import com.swmansion.gesturehandler.core.GestureHandler
import com.swmansion.gesturehandler.react.eventbuilders.GestureHandlerEventDataBuilder

Expand Down Expand Up @@ -47,9 +46,7 @@ class RNGestureHandlerEvent private constructor() : Event<RNGestureHandlerEvent>

override fun getCoalescingKey() = coalescingKey

override fun dispatch(rctEventEmitter: RCTEventEmitter) {
rctEventEmitter.receiveEvent(viewTag, EVENT_NAME, createEventData(dataBuilder!!))
}
override fun getEventData(): WritableMap = createEventData(dataBuilder!!)

companion object {
const val EVENT_NAME = "onGestureHandlerEvent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ class RNGestureHandlerRootHelper(private val context: ReactContext, wrappedView:
action = MotionEvent.ACTION_CANCEL
}
if (rootView is RootView) {
rootView.onChildStartedNativeGesture(event)
rootView.onChildStartedNativeGesture(rootView, event)
}
event.recycle()
}
}

fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
fun requestDisallowInterceptTouchEvent() {
// If this method gets called it means that some native view is attempting to grab lock for
// touch event delivery. In that case we cancel all gesture recognizers
if (orchestrator != null && !passingTouch) {
Expand Down Expand Up @@ -116,6 +116,8 @@ class RNGestureHandlerRootHelper(private val context: ReactContext, wrappedView:
}

/*package*/
@Suppress("UNUSED_PARAMETER", "COMMENT_IN_SUPPRESSION")
// We want to keep order of parameters, so instead of removing viewTag we suppress the warning
fun handleSetJSResponder(viewTag: Int, blockNativeResponder: Boolean) {
if (blockNativeResponder) {
UiThreadUtil.runOnUiThread { tryCancelAllHandlers() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class RNGestureHandlerRootView(context: Context?) : ReactViewGroup(context) {

override fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
if (_enabled) {
rootHelper!!.requestDisallowInterceptTouchEvent(disallowIntercept)
rootHelper!!.requestDisallowInterceptTouchEvent()
}
super.requestDisallowInterceptTouchEvent(disallowIntercept)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.core.util.Pools
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.events.Event
import com.facebook.react.uimanager.events.RCTEventEmitter
import com.swmansion.gesturehandler.core.GestureHandler
import com.swmansion.gesturehandler.react.eventbuilders.GestureHandlerEventDataBuilder

Expand Down Expand Up @@ -46,9 +45,7 @@ class RNGestureHandlerStateChangeEvent private constructor() : Event<RNGestureHa
// TODO: coalescing
override fun getCoalescingKey(): Short = 0

override fun dispatch(rctEventEmitter: RCTEventEmitter) {
rctEventEmitter.receiveEvent(viewTag, EVENT_NAME, createEventData(dataBuilder!!, newState, oldState))
}
override fun getEventData(): WritableMap = createEventData(dataBuilder!!, newState, oldState)

companion object {
const val EVENT_NAME = "onGestureHandlerStateChange"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package com.swmansion.gesturehandler.react
import androidx.core.util.Pools
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.WritableMap
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.events.Event
import com.facebook.react.uimanager.events.RCTEventEmitter
import com.swmansion.gesturehandler.core.GestureHandler

class RNGestureHandlerTouchEvent private constructor() : Event<RNGestureHandlerTouchEvent>() {
private var extraData: WritableMap? = null
private var coalescingKey: Short = 0
private fun <T : GestureHandler<T>> init(handler: T) {
super.init(handler.view!!.id)
super.init(UIManagerHelper.getSurfaceId(handler.view), handler.view!!.id)
extraData = createEventData(handler)
coalescingKey = handler.eventCoalescingKey
}
Expand All @@ -26,10 +26,7 @@ class RNGestureHandlerTouchEvent private constructor() : Event<RNGestureHandlerT
override fun canCoalesce() = true

override fun getCoalescingKey() = coalescingKey

override fun dispatch(rctEventEmitter: RCTEventEmitter) {
rctEventEmitter.receiveEvent(viewTag, EVENT_NAME, extraData)
}
override fun getEventData(): WritableMap? = extraData

companion object {
const val EVENT_UNDETERMINED = 0
Expand All @@ -47,7 +44,7 @@ class RNGestureHandlerTouchEvent private constructor() : Event<RNGestureHandlerT
init(handler)
}

fun <T : GestureHandler<T>> createEventData(handler: T,): WritableMap = Arguments.createMap().apply {
fun <T : GestureHandler<T>> createEventData(handler: T): WritableMap = Arguments.createMap().apply {
putInt("handlerTag", handler.tag)
putInt("state", handler.state)
putInt("numberOfTouches", handler.trackedPointersCount)
Expand Down

0 comments on commit 7e490f7

Please sign in to comment.