Skip to content

Commit 49709ad

Browse files
authored
Merge lib package into the main one in the Android implementation (#2270)
## Description This PR changes the structure of the Gesture Handler's Android code. For some time GH could have been used as a native Android library for handling gestures, but it has been removed since. The split into two packages that was required by it is still there, along some of the now redundant files (another `build.gradle` and `AndroidManifest`). This PR removes the two-package split and moves the core files into a new `core` subpackage, like the `react` one. ## Test plan Build the example app.
1 parent 1091a01 commit 49709ad

36 files changed

+150
-149
lines changed

android/build.gradle

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,8 @@ android {
172172
exclude "**/libreact_render*.so"
173173
}
174174

175-
// Include "lib/" as sources, unfortunately react-native link can't handle
176-
// setting up alternative gradle modules. We still have "lib" defined as a
177-
// standalone gradle module just to be used in AndroidNativeExample
178175
sourceSets.main {
179176
java {
180-
srcDirs += 'lib/src/main/java'
181-
182177
// Include "common/" only when it's not provided by Reanimated to mitigate
183178
// multiple definitions of the same class preventing build
184179
if (shouldUseCommonInterfaceFromReanimated()) {

android/lib/src/main/AndroidManifest.xml

Lines changed: 0 additions & 3 deletions
This file was deleted.

android/lib/src/test/java/com/swmansion/gesturehandler/ExampleUnitTest.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

android/noreanimated/src/main/java/com/swmansion/gesturehandler/ReanimatedEventDispatcher.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.facebook.react.bridge.ReactContext
44
import com.facebook.react.uimanager.events.Event
55

66
class ReanimatedEventDispatcher {
7-
fun <T : Event<T>>sendEvent(event: T, reactApplicationContext: ReactContext) {
8-
// no-op
9-
}
7+
fun <T : Event<T>>sendEvent(event: T, reactApplicationContext: ReactContext) {
8+
// no-op
9+
}
1010
}

android/reanimated/src/main/java/com/swmansion/gesturehandler/ReanimatedEventDispatcher.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import com.facebook.react.uimanager.events.Event
55
import com.swmansion.reanimated.ReanimatedModule
66

77
class ReanimatedEventDispatcher {
8-
private var reanimatedModule: ReanimatedModule? = null
8+
private var reanimatedModule: ReanimatedModule? = null
99

10-
fun <T : Event<T>>sendEvent(event: T, reactApplicationContext: ReactContext) {
11-
if (reanimatedModule == null) {
12-
reanimatedModule = reactApplicationContext.getNativeModule(ReanimatedModule::class.java)
13-
}
14-
15-
reanimatedModule?.nodesManager?.onEventDispatch(event)
10+
fun <T : Event<T>>sendEvent(event: T, reactApplicationContext: ReactContext) {
11+
if (reanimatedModule == null) {
12+
reanimatedModule = reactApplicationContext.getNativeModule(ReanimatedModule::class.java)
1613
}
14+
15+
reanimatedModule?.nodesManager?.onEventDispatch(event)
16+
}
1717
}

android/spotless.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: "com.diffplug.spotless"
22

33
spotless {
44
kotlin {
5-
target "src/**/*.kt"
5+
target "src/**/*.kt", "reanimated/**/*.kt", "noreanimated/**/*.kt", "common/**/*.kt"
66
ktlint().editorConfigOverride([indent_size: 2])
77
trimTrailingWhitespace()
88
indentWithSpaces()

android/lib/src/main/java/com/swmansion/gesturehandler/FlingGestureHandler.kt renamed to android/src/main/java/com/swmansion/gesturehandler/core/FlingGestureHandler.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.swmansion.gesturehandler
1+
package com.swmansion.gesturehandler.core
22

33
import android.os.Handler
44
import android.os.Looper
@@ -37,14 +37,17 @@ class FlingGestureHandler : GestureHandler<FlingGestureHandler>() {
3737

3838
private fun tryEndFling(event: MotionEvent) = if (
3939
maxNumberOfPointersSimultaneously == numberOfPointersRequired &&
40-
(direction and DIRECTION_RIGHT != 0 &&
41-
event.rawX - startX > minAcceptableDelta ||
42-
direction and DIRECTION_LEFT != 0 &&
43-
startX - event.rawX > minAcceptableDelta ||
44-
direction and DIRECTION_UP != 0 &&
45-
startY - event.rawY > minAcceptableDelta ||
46-
direction and DIRECTION_DOWN != 0 &&
47-
event.rawY - startY > minAcceptableDelta)) {
40+
(
41+
direction and DIRECTION_RIGHT != 0 &&
42+
event.rawX - startX > minAcceptableDelta ||
43+
direction and DIRECTION_LEFT != 0 &&
44+
startX - event.rawX > minAcceptableDelta ||
45+
direction and DIRECTION_UP != 0 &&
46+
startY - event.rawY > minAcceptableDelta ||
47+
direction and DIRECTION_DOWN != 0 &&
48+
event.rawY - startY > minAcceptableDelta
49+
)
50+
) {
4851
handler!!.removeCallbacksAndMessages(null)
4952
activate()
5053
true

android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandler.kt renamed to android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.swmansion.gesturehandler
1+
package com.swmansion.gesturehandler.core
22

33
import android.app.Activity
44
import android.content.Context
@@ -14,6 +14,7 @@ import com.facebook.react.bridge.Arguments
1414
import com.facebook.react.bridge.UiThreadUtil
1515
import com.facebook.react.bridge.WritableArray
1616
import com.facebook.react.uimanager.PixelUtil
17+
import com.swmansion.gesturehandler.BuildConfig
1718
import com.swmansion.gesturehandler.react.RNGestureHandlerTouchEvent
1819
import java.lang.IllegalStateException
1920
import java.util.*
@@ -48,7 +49,6 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
4849
private val trackedPointers: Array<PointerData?> = Array(MAX_POINTERS_COUNT) { null }
4950
var needsPointerData = false
5051

51-
5252
private var hitSlop: FloatArray? = null
5353
var eventCoalescingKey: Short = 0
5454
private set
@@ -127,11 +127,15 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
127127
}
128128

129129
fun setManualActivation(manualActivation: Boolean): ConcreteGestureHandlerT =
130-
applySelf { this.manualActivation = manualActivation }
130+
applySelf { this.manualActivation = manualActivation }
131131

132132
fun setHitSlop(
133-
leftPad: Float, topPad: Float, rightPad: Float, bottomPad: Float,
134-
width: Float, height: Float,
133+
leftPad: Float,
134+
topPad: Float,
135+
rightPad: Float,
136+
bottomPad: Float,
137+
width: Float,
138+
height: Float,
135139
): ConcreteGestureHandlerT = applySelf {
136140
if (hitSlop == null) {
137141
hitSlop = FloatArray(6)
@@ -278,7 +282,7 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
278282
}
279283

280284
// introduced in 1.11.0, remove if crashes are not reported
281-
if(pointerProps.isEmpty()|| pointerCoords.isEmpty()){
285+
if (pointerProps.isEmpty() || pointerCoords.isEmpty()) {
282286
throw IllegalStateException("pointerCoords.size=${pointerCoords.size}, pointerProps.size=${pointerProps.size}")
283287
}
284288

@@ -289,8 +293,8 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
289293
event.eventTime,
290294
action,
291295
count,
292-
pointerProps, /* props are copied and hence it is safe to use static array here */
293-
pointerCoords, /* same applies to coords */
296+
pointerProps, /* props are copied and hence it is safe to use static array here */
297+
pointerCoords, /* same applies to coords */
294298
event.metaState,
295299
event.buttonState,
296300
event.xPrecision,
@@ -313,7 +317,8 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
313317
handler: GestureHandler<*>,
314318
event: MotionEvent,
315319
e: IllegalArgumentException
316-
) : Exception("""
320+
) : Exception(
321+
"""
317322
handler: ${handler::class.simpleName}
318323
state: ${handler.state}
319324
view: ${handler.view}
@@ -324,14 +329,17 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
324329
trackedPointersCount: ${handler.trackedPointersIDsCount}
325330
trackedPointers: ${handler.trackedPointerIDs.joinToString(separator = ", ")}
326331
while handling event: $event
327-
""".trimIndent(), e) {}
332+
""".trimIndent(),
333+
e
334+
)
328335

329336
fun handle(transformedEvent: MotionEvent, sourceEvent: MotionEvent) {
330-
if (!isEnabled
331-
|| state == STATE_CANCELLED
332-
|| state == STATE_FAILED
333-
|| state == STATE_END
334-
|| trackedPointersIDsCount < 1) {
337+
if (!isEnabled ||
338+
state == STATE_CANCELLED ||
339+
state == STATE_FAILED ||
340+
state == STATE_END ||
341+
trackedPointersIDsCount < 1
342+
) {
335343
return
336344
}
337345

@@ -380,11 +388,11 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
380388
val offsetY = event.rawY - event.y
381389

382390
trackedPointers[pointerId] = PointerData(
383-
pointerId,
384-
event.getX(event.actionIndex),
385-
event.getY(event.actionIndex),
386-
event.getX(event.actionIndex) + offsetX - windowOffset[0],
387-
event.getY(event.actionIndex) + offsetY - windowOffset[1],
391+
pointerId,
392+
event.getX(event.actionIndex),
393+
event.getY(event.actionIndex),
394+
event.getX(event.actionIndex) + offsetX - windowOffset[0],
395+
event.getY(event.actionIndex) + offsetY - windowOffset[1],
388396
)
389397
trackedPointersCount++
390398
addChangedPointer(trackedPointers[pointerId]!!)
@@ -402,11 +410,11 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
402410
val offsetY = event.rawY - event.y
403411

404412
trackedPointers[pointerId] = PointerData(
405-
pointerId,
406-
event.getX(event.actionIndex),
407-
event.getY(event.actionIndex),
408-
event.getX(event.actionIndex) + offsetX - windowOffset[0],
409-
event.getY(event.actionIndex) + offsetY - windowOffset[1],
413+
pointerId,
414+
event.getX(event.actionIndex),
415+
event.getY(event.actionIndex),
416+
event.getX(event.actionIndex) + offsetX - windowOffset[0],
417+
event.getY(event.actionIndex) + offsetY - windowOffset[1],
410418
)
411419
addChangedPointer(trackedPointers[pointerId]!!)
412420
trackedPointers[pointerId] = null
@@ -545,11 +553,11 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
545553
}
546554

547555
fun wantEvents(): Boolean {
548-
return isEnabled
549-
&& state != STATE_FAILED
550-
&& state != STATE_CANCELLED
551-
&& state != STATE_END
552-
&& trackedPointersIDsCount > 0
556+
return isEnabled &&
557+
state != STATE_FAILED &&
558+
state != STATE_CANCELLED &&
559+
state != STATE_END &&
560+
trackedPointersIDsCount > 0
553561
}
554562

555563
open fun shouldRequireToWaitForFailure(handler: GestureHandler<*>): Boolean {
@@ -745,7 +753,7 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
745753
private lateinit var pointerCoords: Array<PointerCoords?>
746754
private fun initPointerProps(size: Int) {
747755
var size = size
748-
if (!::pointerProps.isInitialized) {
756+
if (!Companion::pointerProps.isInitialized) {
749757
pointerProps = arrayOfNulls(MAX_POINTERS_COUNT)
750758
pointerCoords = arrayOfNulls(MAX_POINTERS_COUNT)
751759
}

android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandlerInteractionController.kt renamed to android/src/main/java/com/swmansion/gesturehandler/core/GestureHandlerInteractionController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.swmansion.gesturehandler
1+
package com.swmansion.gesturehandler.core
22

33
interface GestureHandlerInteractionController {
44
fun shouldWaitForHandlerFailure(handler: GestureHandler<*>, otherHandler: GestureHandler<*>): Boolean

android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandlerOrchestrator.kt renamed to android/src/main/java/com/swmansion/gesturehandler/core/GestureHandlerOrchestrator.kt

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.swmansion.gesturehandler
1+
package com.swmansion.gesturehandler.core
22

33
import android.graphics.Matrix
44
import android.graphics.PointF
@@ -134,7 +134,10 @@ class GestureHandlerOrchestrator(
134134
// their state is set to END and when the gesture they are waiting for activates they
135135
// should be cancelled, however `cancel` was never sent as gestures were already in the END state.
136136
// Send synthetic BEGAN -> CANCELLED to properly handle JS logic
137-
otherHandler.dispatchStateChange(GestureHandler.STATE_CANCELLED, GestureHandler.STATE_BEGAN)
137+
otherHandler.dispatchStateChange(
138+
GestureHandler.STATE_CANCELLED,
139+
GestureHandler.STATE_BEGAN
140+
)
138141
}
139142
otherHandler.isAwaiting = false
140143
} else {
@@ -253,10 +256,10 @@ class GestureHandlerOrchestrator(
253256

254257
val action = sourceEvent.actionMasked
255258
val event = transformEventToViewCoords(handler.view, MotionEvent.obtain(sourceEvent))
256-
259+
257260
// Touch events are sent before the handler itself has a chance to process them,
258261
// mainly because `onTouchesUp` shoul be send befor gesture finishes. This means that
259-
// the first `onTouchesDown` event is sent before a gesture begins, activation in
262+
// the first `onTouchesDown` event is sent before a gesture begins, activation in
260263
// callback for this event causes problems because the handler doesn't have a chance
261264
// to initialize itself with starting values of pointer (in pan this causes translation
262265
// to be equal to the coordinates of the pointer). The simplest solution is to send
@@ -475,9 +478,10 @@ class GestureHandlerOrchestrator(
475478

476479
// if the pointer is inside the view but it overflows its parent, handlers attached to the parent
477480
// might not have been extracted (pointer might be in a child, but may be outside parent)
478-
if (coords[0] in 0f..view.width.toFloat() && coords[1] in 0f..view.height.toFloat()
479-
&& isViewOverflowingParent(view) && extractAncestorHandlers(view, coords, pointerId)) {
480-
found = true
481+
if (coords[0] in 0f..view.width.toFloat() && coords[1] in 0f..view.height.toFloat() &&
482+
isViewOverflowingParent(view) && extractAncestorHandlers(view, coords, pointerId)
483+
) {
484+
found = true
481485
}
482486

483487
return found
@@ -527,8 +531,10 @@ class GestureHandlerOrchestrator(
527531
}
528532
PointerEventsConfig.BOX_ONLY -> {
529533
// This view is the target, its children don't matter
530-
(recordViewHandlersForPointer(view, coords, pointerId)
531-
|| shouldHandlerlessViewBecomeTouchTarget(view, coords))
534+
(
535+
recordViewHandlersForPointer(view, coords, pointerId) ||
536+
shouldHandlerlessViewBecomeTouchTarget(view, coords)
537+
)
532538
}
533539
PointerEventsConfig.BOX_NONE -> {
534540
// This view can't be the target, but its children might
@@ -557,8 +563,10 @@ class GestureHandlerOrchestrator(
557563
extractGestureHandlers(view, coords, pointerId)
558564
} else false
559565

560-
(recordViewHandlersForPointer(view, coords, pointerId)
561-
|| found || shouldHandlerlessViewBecomeTouchTarget(view, coords))
566+
(
567+
recordViewHandlersForPointer(view, coords, pointerId) ||
568+
found || shouldHandlerlessViewBecomeTouchTarget(view, coords)
569+
)
562570
}
563571
}
564572

@@ -570,7 +578,6 @@ class GestureHandlerOrchestrator(
570578
private fun isClipping(view: View) =
571579
view !is ViewGroup || viewConfigHelper.isViewClippingChildren(view)
572580

573-
574581
companion object {
575582
// The limit doesn't necessarily need to exists, it was just simpler to implement it that way
576583
// it is also more allocation-wise efficient to have a fixed limit
@@ -637,14 +644,15 @@ class GestureHandlerOrchestrator(
637644
x in 0f..child.width.toFloat() && y in 0f..child.height.toFloat()
638645

639646
private fun shouldHandlerWaitForOther(handler: GestureHandler<*>, other: GestureHandler<*>): Boolean {
640-
return handler !== other && (handler.shouldWaitForHandlerFailure(other)
641-
|| other.shouldRequireToWaitForFailure(handler))
647+
return handler !== other && (
648+
handler.shouldWaitForHandlerFailure(other) ||
649+
other.shouldRequireToWaitForFailure(handler)
650+
)
642651
}
643652

644653
private fun canRunSimultaneously(a: GestureHandler<*>, b: GestureHandler<*>) =
645654
a === b || a.shouldRecognizeSimultaneously(b) || b.shouldRecognizeSimultaneously(a)
646655

647-
648656
private fun shouldHandlerBeCancelledBy(handler: GestureHandler<*>, other: GestureHandler<*>): Boolean {
649657
if (!handler.hasCommonPointers(other)) {
650658
// if two handlers share no common pointer one can never trigger cancel for the other
@@ -656,16 +664,17 @@ class GestureHandlerOrchestrator(
656664
return false
657665
}
658666
return if (handler !== other &&
659-
(handler.isAwaiting || handler.state == GestureHandler.STATE_ACTIVE)) {
667+
(handler.isAwaiting || handler.state == GestureHandler.STATE_ACTIVE)
668+
) {
660669
// in every other case as long as the handler is about to be activated or already in active
661670
// state, we delegate the decision to the implementation of GestureHandler#shouldBeCancelledBy
662671
handler.shouldBeCancelledBy(other)
663672
} else true
664673
}
665674

666675
private fun isFinished(state: Int) =
667-
state == GestureHandler.STATE_CANCELLED
668-
|| state == GestureHandler.STATE_FAILED
669-
|| state == GestureHandler.STATE_END
676+
state == GestureHandler.STATE_CANCELLED ||
677+
state == GestureHandler.STATE_FAILED ||
678+
state == GestureHandler.STATE_END
670679
}
671680
}

0 commit comments

Comments
 (0)