Skip to content

Commit 30aaa9d

Browse files
committed
fix(mobile): only fingers that started on the Home list hold row activation
`touches` counts every finger on screen, so a finger resting on another control kept activation deferred after the list touch ended. Track the list's own touch identifiers, pruned to the fingers still down.
1 parent b2ac57b commit 30aaa9d

3 files changed

Lines changed: 50 additions & 16 deletions

File tree

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
ActivityIndicator,
2323
Platform,
2424
View,
25+
type GestureResponderEvent,
2526
type NativeScrollEvent,
2627
type NativeSyntheticEvent,
2728
} from "react-native";
@@ -300,6 +301,16 @@ export function HomeScreen(props: HomeScreenProps) {
300301
},
301302
[activateVisibleRows, onMaterialFabScroll],
302303
);
304+
const trackListTouches = useCallback(
305+
(event: GestureResponderEvent, started: boolean) => {
306+
const { changedTouches, touches } = event.nativeEvent;
307+
swipeRowActivation.trackTouches(
308+
started ? changedTouches.map((touch) => touch.identifier) : [],
309+
touches.map((touch) => touch.identifier),
310+
);
311+
},
312+
[swipeRowActivation],
313+
);
303314
const { swipeEnabled, scrollGateHandlers } = useSwipeableScrollGate({
304315
onScroll: handleListScroll,
305316
onScrollBeginDrag: handleScrollBeginDrag,
@@ -994,13 +1005,9 @@ export function HomeScreen(props: HomeScreenProps) {
9941005
<LegendList
9951006
ref={listRef}
9961007
onLoad={() => activateVisibleRows(threadListV2Items)}
997-
onTouchStart={() => swipeRowActivation.setTouching(true)}
998-
onTouchEnd={(event) =>
999-
swipeRowActivation.setTouching(event.nativeEvent.touches.length > 0)
1000-
}
1001-
onTouchCancel={(event) =>
1002-
swipeRowActivation.setTouching(event.nativeEvent.touches.length > 0)
1003-
}
1008+
onTouchStart={(event) => trackListTouches(event, true)}
1009+
onTouchEnd={(event) => trackListTouches(event, false)}
1010+
onTouchCancel={(event) => trackListTouches(event, false)}
10041011
data={threadListV2Items}
10051012
renderItem={renderV2Item}
10061013
keyExtractor={v2KeyExtractor}

‎apps/mobile/src/features/home/swipe-row-activation.test.ts‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,42 @@ describe("createSwipeRowActivation", () => {
2121
expect(listener).toHaveBeenCalledTimes(2);
2222
});
2323

24-
it("defers changes while a finger is down so a press is never remounted", () => {
24+
it("defers changes while a finger is on the list so a press is never remounted", () => {
2525
const activation = createSwipeRowActivation();
2626
activation.activate(["a"]);
2727

28-
activation.setTouching(true);
28+
activation.trackTouches(["1"], ["1"]);
2929
activation.activate(["b"]);
3030
activation.activate(["c"]);
3131
expect(activation.isActive("a")).toBe(true);
3232
expect(activation.isActive("c")).toBe(false);
3333

34-
activation.setTouching(false);
34+
activation.trackTouches([], []);
3535
expect(activation.isActive("a")).toBe(false);
3636
expect(activation.isActive("b")).toBe(false);
3737
expect(activation.isActive("c")).toBe(true);
3838
});
3939

40+
it("ignores fingers that did not start on the list", () => {
41+
const activation = createSwipeRowActivation();
42+
activation.trackTouches(["1"], ["1", "2"]);
43+
activation.activate(["a"]);
44+
45+
// The list finger lifts while finger 2 stays on another control.
46+
activation.trackTouches([], ["2"]);
47+
expect(activation.isActive("a")).toBe(true);
48+
});
49+
50+
it("drops a list finger whose end event never arrived", () => {
51+
const activation = createSwipeRowActivation();
52+
activation.trackTouches(["1"], ["1"]);
53+
activation.activate(["a"]);
54+
55+
activation.trackTouches(["2"], ["2"]);
56+
activation.trackTouches([], []);
57+
expect(activation.isActive("a")).toBe(true);
58+
});
59+
4060
it("stops notifying after unsubscribe", () => {
4161
const activation = createSwipeRowActivation();
4262
const listener = vi.fn();

‎apps/mobile/src/features/home/swipe-row-activation.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import { createContext, use, useSyncExternalStore } from "react";
1010
export function createSwipeRowActivation() {
1111
let activeKeys = new Set<string>();
1212
// Swapping a row's frame remounts it, which would cancel a press or long
13-
// press in progress, so changes wait for the finger to lift.
14-
let touching = false;
13+
// press in progress, so changes wait until every finger that started on the
14+
// list has lifted.
15+
const listTouches = new Set<string>();
1516
let pendingKeys: ReadonlyArray<string> | null = null;
1617
const listeners = new Set<() => void>();
1718
const apply = (keys: ReadonlyArray<string>) => {
@@ -26,12 +27,18 @@ export function createSwipeRowActivation() {
2627
},
2728
isActive: (key: string) => activeKeys.has(key),
2829
activate(keys: ReadonlyArray<string>) {
29-
if (touching) pendingKeys = keys;
30+
if (listTouches.size > 0) pendingKeys = keys;
3031
else apply(keys);
3132
},
32-
setTouching(next: boolean) {
33-
touching = next;
34-
if (next || pendingKeys === null) return;
33+
/**
34+
* `started` are touches that just began on the list; `onScreen` is every
35+
* finger still down anywhere. A finger on another control never holds
36+
* changes, and one whose end event went missing is dropped here.
37+
*/
38+
trackTouches(started: ReadonlyArray<string>, onScreen: ReadonlyArray<string>) {
39+
for (const id of started) listTouches.add(id);
40+
for (const id of listTouches) if (!onScreen.includes(id)) listTouches.delete(id);
41+
if (listTouches.size > 0 || pendingKeys === null) return;
3542
const keys = pendingKeys;
3643
pendingKeys = null;
3744
apply(keys);

0 commit comments

Comments
 (0)