Commit 99daffa
authored
[Android] Remove workaround for touch events allowing to change state too soon (#3793)
## Description
Supersedes
#2283
On Android, `onTouchesDown` was delayed to be dispatched after `onBegin`
so that the handlers had the time to initialize themselves (which
happens in `onHandle`) so that the non-touch events would have correct
data.
The old approach was saving the event, which triggered the touch event,
and using that event to initialize the handler during imperative state
change. The issues with that approach were:
1. Storing the event for the duration of the event handling
2. It wouldn't work with asynchronous state changes, which are now
possible
The new approach is to add an option to force gestures to initialize
regardless of the state they are in. This way, when the state is updated
and the current state of the handler is `UNDETERMINED`, the flag is set
on the handler, and when it starts handling the event, it will first
initialize itself.
## Test plan
Tested on the following snippet, with affected gestures
```
import React from 'react';
import { StyleSheet, View } from 'react-native';
import { usePan, NativeDetector } from 'react-native-gesture-handler';
export default function EmptyExample() {
const pan = usePan({
onTouchesDown: () => {
'worklet';
console.log('Touch down');
globalThis._setGestureStateSync(57, 4);
},
onTouchesMove: () => {
'worklet';
console.log('Touch move');
},
onTouchesUp: () => {
'worklet';
console.log('Touch up');
},
onTouchesCancelled: () => {
'worklet';
console.log('Touch cancel');
},
onBegin: () => {
'worklet';
console.log('Gesture begin');
},
onStart: (e) => {
'worklet';
console.log('Gesture start');
},
onUpdate: (e) => {
'worklet';
console.log('Gesture update', e.handlerData.translationX, e.handlerData.translationY);
},
onEnd: () => {
'worklet';
console.log('Gesture end');
},
onFinalize: () => {
'worklet';
console.log('Gesture finalize');
},
});
console.log('Rendering EmptyExamplee', pan.tag);
return (
<View style={styles.container}>
<NativeDetector gesture={pan}>
<View style={{ width: 300, height: 300, backgroundColor: 'green' }} />
</NativeDetector>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
```1 parent 2d739ec commit 99daffa
File tree
8 files changed
+85
-42
lines changed- packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler
- core
- react
8 files changed
+85
-42
lines changedLines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
85 | 86 | | |
86 | 87 | | |
87 | 88 | | |
| |||
755 | 756 | | |
756 | 757 | | |
757 | 758 | | |
| 759 | + | |
758 | 760 | | |
759 | 761 | | |
760 | 762 | | |
| |||
Lines changed: 1 addition & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
287 | 287 | | |
288 | 288 | | |
289 | 289 | | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
| 290 | + | |
299 | 291 | | |
300 | 292 | | |
301 | 293 | | |
| |||
317 | 309 | | |
318 | 310 | | |
319 | 311 | | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | 312 | | |
325 | 313 | | |
326 | 314 | | |
| |||
Lines changed: 11 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
68 | 73 | | |
69 | 74 | | |
70 | 75 | | |
71 | 76 | | |
72 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
73 | 83 | | |
74 | | - | |
75 | | - | |
| 84 | + | |
76 | 85 | | |
77 | 86 | | |
78 | 87 | | |
| |||
Lines changed: 15 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
151 | 160 | | |
152 | 161 | | |
153 | 162 | | |
154 | 163 | | |
155 | 164 | | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
156 | 170 | | |
157 | 171 | | |
158 | 172 | | |
| |||
174 | 188 | | |
175 | 189 | | |
176 | 190 | | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
| 191 | + | |
183 | 192 | | |
184 | 193 | | |
185 | 194 | | |
| |||
Lines changed: 18 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
59 | 58 | | |
60 | | - | |
61 | | - | |
62 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
64 | 72 | | |
65 | 73 | | |
66 | 74 | | |
| |||
Lines changed: 15 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
46 | 45 | | |
47 | | - | |
48 | | - | |
49 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
50 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
51 | 59 | | |
52 | 60 | | |
53 | 61 | | |
| |||
Lines changed: 13 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
84 | 91 | | |
85 | 92 | | |
86 | 93 | | |
87 | 94 | | |
88 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
89 | 101 | | |
90 | 102 | | |
91 | 103 | | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
| 104 | + | |
96 | 105 | | |
97 | 106 | | |
98 | 107 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
143 | 153 | | |
144 | 154 | | |
145 | 155 | | |
| |||
0 commit comments