Commit 8a4d05d
authored
[Android] Initialize the
## Description
`Tap`, `LongPress`, `Fling`, `Pan` and `Hover` gesture handlers kept their `Handler` as a nullable field created on first use, guarded by null checks and `!!` at every call site (with a `TODO: lazy init` left in `Tap` since the Kotlin conversion).
`Handler` object was never cleared anyway (except `LongPress`, where it used to serve as a flag, and `Hover`).
Also adds a comment explaining the 4 ms delay in `HoverGestureHandler`.
## Test plan
- `:react-native-gesture-handler:compileDebugKotlin` builds clean in basic-example
- `yarn format:android` passes
<details>
<summary>Tested on the following code:</summary>
```tsx
import React, { useCallback, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import {
Directions,
GestureDetector,
useFlingGesture,
useHoverGesture,
useLongPressGesture,
usePanGesture,
useTapGesture,
} from 'react-native-gesture-handler';
import { scheduleOnRN } from 'react-native-worklets';
type Counters = { begin: number; act: number; end: number; fail: number };
type Results = Record<string, Counters>;
const EMPTY: Counters = { begin: 0, act: 0, end: 0, fail: 0 };
export default function EmptyExample() {
const [results, setResults] = useState<Results>({});
const report = useCallback((key: string, kind: keyof Counters) => {
setResults((prev) => {
const current = prev[key] ?? EMPTY;
return { ...prev, [key]: { ...current, [kind]: current[kind] + 1 } };
});
}, []);
const tap = useTapGesture({
onBegin: () => scheduleOnRN(report, 'tap', 'begin'),
onActivate: () => scheduleOnRN(report, 'tap', 'act'),
onFinalize: (e) =>
scheduleOnRN(report, 'tap', e.canceled ? 'fail' : 'end'),
});
const doubleTap = useTapGesture({
numberOfTaps: 2,
onBegin: () => scheduleOnRN(report, 'doubleTap', 'begin'),
onActivate: () => scheduleOnRN(report, 'doubleTap', 'act'),
onFinalize: (e) =>
scheduleOnRN(report, 'doubleTap', e.canceled ? 'fail' : 'end'),
});
const longPress = useLongPressGesture({
minDurationMs: 400,
onBegin: () => scheduleOnRN(report, 'longPress', 'begin'),
onActivate: () => scheduleOnRN(report, 'longPress', 'act'),
onFinalize: (e) =>
scheduleOnRN(report, 'longPress', e.canceled ? 'fail' : 'end'),
});
const fling = useFlingGesture({
direction: Directions.RIGHT,
onBegin: () => scheduleOnRN(report, 'fling', 'begin'),
onActivate: () => scheduleOnRN(report, 'fling', 'act'),
onFinalize: (e) =>
scheduleOnRN(report, 'fling', e.canceled ? 'fail' : 'end'),
});
const pan = usePanGesture({
activateAfterLongPress: 400,
onBegin: () => scheduleOnRN(report, 'pan', 'begin'),
onActivate: () => scheduleOnRN(report, 'pan', 'act'),
onFinalize: (e) =>
scheduleOnRN(report, 'pan', e.canceled ? 'fail' : 'end'),
});
const hover = useHoverGesture({
onBegin: () => scheduleOnRN(report, 'hover', 'begin'),
onActivate: () => scheduleOnRN(report, 'hover', 'act'),
onFinalize: (e) =>
scheduleOnRN(report, 'hover', e.canceled ? 'fail' : 'end'),
});
const rows: { key: string; label: string; gesture: any }[] = [
{ key: 'tap', label: 'Tap', gesture: tap },
{ key: 'doubleTap', label: 'DoubleTap', gesture: doubleTap },
{ key: 'longPress', label: 'LongPress 400ms', gesture: longPress },
{ key: 'fling', label: 'Fling right', gesture: fling },
{ key: 'pan', label: 'Pan holdActivate 400ms', gesture: pan },
{ key: 'hover', label: 'Hover', gesture: hover },
];
return (
<View style={styles.container}>
{rows.map(({ key, label, gesture }) => {
const c = results[key] ?? EMPTY;
return (
<GestureDetector key={key} gesture={gesture}>
<View style={styles.box}>
<Text style={styles.label}>{label}</Text>
<Text style={styles.status}>
{`${key} b:${c.begin} a:${c.act} e:${c.end} f:${c.fail}`}
</Text>
</View>
</GestureDetector>
);
})}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 12,
gap: 10,
},
box: {
height: 88,
borderRadius: 12,
backgroundColor: '#dbe4ff',
justifyContent: 'center',
alignItems: 'center',
},
label: {
fontSize: 18,
fontWeight: '600',
},
status: {
fontSize: 15,
fontVariant: ['tabular-nums'],
},
});
```
</details>Handler fields eagerly (#4461)1 parent 156aca8 commit 8a4d05d
5 files changed
Lines changed: 28 additions & 52 deletions
File tree
- packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/core
Lines changed: 6 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 32 | + | |
| 33 | + | |
38 | 34 | | |
39 | 35 | | |
40 | 36 | | |
| |||
69 | 65 | | |
70 | 66 | | |
71 | 67 | | |
72 | | - | |
| 68 | + | |
73 | 69 | | |
74 | 70 | | |
75 | 71 | | |
| |||
109 | 105 | | |
110 | 106 | | |
111 | 107 | | |
112 | | - | |
| 108 | + | |
113 | 109 | | |
114 | 110 | | |
115 | 111 | | |
116 | 112 | | |
117 | 113 | | |
118 | | - | |
| 114 | + | |
119 | 115 | | |
120 | 116 | | |
121 | 117 | | |
| |||
Lines changed: 5 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
89 | | - | |
| 88 | + | |
90 | 89 | | |
91 | 90 | | |
92 | 91 | | |
| |||
97 | 96 | | |
98 | 97 | | |
99 | 98 | | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
105 | 102 | | |
106 | 103 | | |
107 | 104 | | |
| |||
Lines changed: 5 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | | - | |
| 115 | + | |
116 | 116 | | |
117 | | - | |
| 117 | + | |
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| |||
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
| 127 | + | |
131 | 128 | | |
132 | 129 | | |
133 | 130 | | |
| |||
164 | 161 | | |
165 | 162 | | |
166 | 163 | | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
| 164 | + | |
171 | 165 | | |
172 | 166 | | |
173 | 167 | | |
| |||
Lines changed: 5 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
143 | | - | |
| 143 | + | |
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
| |||
200 | 200 | | |
201 | 201 | | |
202 | 202 | | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
| 203 | + | |
207 | 204 | | |
208 | 205 | | |
209 | 206 | | |
| |||
252 | 249 | | |
253 | 250 | | |
254 | 251 | | |
255 | | - | |
| 252 | + | |
256 | 253 | | |
257 | 254 | | |
258 | 255 | | |
259 | | - | |
| 256 | + | |
260 | 257 | | |
261 | 258 | | |
262 | 259 | | |
| |||
Lines changed: 7 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 50 | + | |
| 51 | + | |
56 | 52 | | |
57 | 53 | | |
58 | 54 | | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
| 55 | + | |
64 | 56 | | |
65 | 57 | | |
66 | 58 | | |
67 | | - | |
| 59 | + | |
68 | 60 | | |
69 | 61 | | |
70 | 62 | | |
| |||
139 | 131 | | |
140 | 132 | | |
141 | 133 | | |
142 | | - | |
| 134 | + | |
143 | 135 | | |
144 | 136 | | |
145 | 137 | | |
146 | 138 | | |
147 | 139 | | |
148 | | - | |
| 140 | + | |
149 | 141 | | |
150 | 142 | | |
151 | 143 | | |
| |||
0 commit comments