diff --git a/apps/mobile/src/features/settings/SettingsMicrophoneRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsMicrophoneRouteScreen.tsx index adcf71595b177..b7ccdc0e00640 100644 --- a/apps/mobile/src/features/settings/SettingsMicrophoneRouteScreen.tsx +++ b/apps/mobile/src/features/settings/SettingsMicrophoneRouteScreen.tsx @@ -1,14 +1,13 @@ -import { Button } from "../../components/ui/Button"; import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView"; import { useAtomSet, useAtomValue } from "@effect/atom-react"; import { AsyncResult } from "effect/reactivity"; import { getRecordingPermissionsAsync, requestRecordingPermissionsAsync } from "expo-audio"; import { useState } from "react"; -import { Linking, Pressable, View } from "react-native"; +import { Linking, useWindowDimensions, View } from "react-native"; import Reanimated, { ReduceMotion, useAnimatedStyle, withTiming } from "react-native-reanimated"; import { useSafeAreaInsets } from "react-native-safe-area-context"; -import { type AppSymbolName, SymbolView } from "../../components/ui/AppSymbol"; +import { type AppSymbolName } from "../../components/ui/AppSymbol"; import { AppText as Text } from "../../components/ui/AppText"; import { cn } from "../../lib/cn"; import { makePermissionStatusAtom } from "../../lib/permissionStatusAtom"; @@ -21,12 +20,11 @@ import { } from "../../lib/microphonePriority"; import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences"; import { ListRowLayout } from "../../components/ui/ListRowLayout"; +import { ThreadSwipeable } from "../home/thread-swipe-actions"; import { SettingsActionRow } from "./components/SettingsActionRow"; import { SettingsDragHandle } from "./components/SettingsDragHandle"; import { SettingsSection } from "./components/SettingsSection"; -const REMOVE_SIZE = 20; - type MicrophonePermission = "checking" | "granted" | "ask" | "denied"; function toMicrophonePermission(response: { @@ -98,9 +96,7 @@ export function SettingsMicrophoneRouteScreen() { const entries = microphoneOrder( AsyncResult.isSuccess(preferencesResult) ? preferencesResult.value.microphones : undefined, ); - const hasDevices = entries.some(isMicrophoneDevice); const { permission, request: requestPermission } = useMicrophonePermission(); - const [editing, setEditing] = useState(false); const [drag, setDrag] = useState<{ readonly key: string; readonly translation: number } | null>( null, ); @@ -166,18 +162,7 @@ export function SettingsMicrophoneRouteScreen() { ) : null} - setEditing((value) => !value)} - /> - ) : undefined - } - > + {entries.map((entry, index) => { const key = microphoneEntryKey(entry); const shift = @@ -195,7 +180,6 @@ export function SettingsMicrophoneRouteScreen() { position={index + 1} count={entries.length} reorderable={ready} - editing={editing && hasDevices} offset={index === dragFrom ? (drag?.translation ?? 0) : shift} lifted={index === dragFrom} onHeight={setRowHeight} @@ -215,7 +199,7 @@ export function SettingsMicrophoneRouteScreen() { Voice input records from the first connected microphone in this list. Wired and Bluetooth devices are added by name after you use voice input with them connected; until then, the - Other row ranks them. + Other row ranks them. Swipe a device left to forget it. @@ -227,7 +211,6 @@ function MicrophoneRow(props: { readonly position: number; readonly count: number; readonly reorderable: boolean; - readonly editing: boolean; readonly offset: number; readonly lifted: boolean; readonly onHeight: (height: number) => void; @@ -237,9 +220,10 @@ function MicrophoneRow(props: { readonly onStep: (direction: "up" | "down") => void; readonly onForget: (() => void) | undefined; }) { - const { lifted, offset, entry } = props; + const { lifted, offset, entry, onForget } = props; const kind = KINDS[entry.kind]; const title = isMicrophoneDevice(entry) ? entry.name : kind.row; + const { width: windowWidth } = useWindowDimensions(); const style = useAnimatedStyle(() => ({ transform: [ { @@ -250,39 +234,16 @@ function MicrophoneRow(props: { ], zIndex: lifted ? 1 : 0, })); - return ( - props.onHeight(event.nativeEvent.layout.height)} - className={cn( - "flex-row items-center", - props.position > 1 && !lifted && "border-t border-border-subtle", - lifted && "bg-grouped-card shadow-md", - )} - > - {props.editing && props.onForget ? ( - - - - ) : props.editing ? ( - - - - ) : null} + const row = ( + // Opaque so the swipe actions stay hidden until the row slides over them. + { + if (nativeEvent.actionName === "forget") onForget?.(); + }} className="min-w-0 flex-1" > ) : null} + + ); + return ( + props.onHeight(event.nativeEvent.layout.height)} + className={cn( + props.position > 1 && !lifted && "border-t border-border-subtle", + lifted && "bg-grouped-card shadow-md", + )} + > + {onForget ? ( + + {() => row} + + ) : ( + row + )} ); } diff --git a/apps/mobile/src/features/settings/components/SettingsDragHandle.test.tsx b/apps/mobile/src/features/settings/components/SettingsDragHandle.test.tsx new file mode 100644 index 0000000000000..8c1f1384fa4a6 --- /dev/null +++ b/apps/mobile/src/features/settings/components/SettingsDragHandle.test.tsx @@ -0,0 +1,75 @@ +import type { ReactNode } from "react"; +import { act } from "react"; +import { createRoot } from "react-dom/client"; +import { describe, expect, it, vi } from "vite-plus/test"; + +type PanEvent = { readonly translationY: number }; +type PanHandlers = { + onStart?: () => void; + onUpdate?: (event: PanEvent) => void; + onFinalize?: (event: PanEvent, success: boolean) => void; +}; + +const pans = vi.hoisted(() => [] as PanHandlers[]); + +vi.mock("react-native", () => ({ + View: ({ children }: { children?: ReactNode }) =>
{children}
, +})); +vi.mock("react-native-gesture-handler", () => ({ + GestureDetector: ({ children }: { children?: ReactNode }) => children, + Gesture: { + Pan: () => { + const handlers: PanHandlers = {}; + pans.push(handlers); + const pan = { + minDistance: () => pan, + shouldCancelWhenOutside: () => pan, + runOnJS: () => pan, + onStart: (callback: PanHandlers["onStart"]) => ((handlers.onStart = callback), pan), + onUpdate: (callback: PanHandlers["onUpdate"]) => ((handlers.onUpdate = callback), pan), + onFinalize: (callback: PanHandlers["onFinalize"]) => ( + (handlers.onFinalize = callback), + pan + ), + }; + return pan; + }, + }, +})); +vi.mock("../../../components/ui/AppSymbol", () => ({ SymbolView: () => null })); + +import { SettingsDragHandle } from "./SettingsDragHandle"; + +describe("SettingsDragHandle", () => { + // The worklets Babel plugin copies values a gesture callback closes over when the gesture is + // built, so a drop must not read a translation tracked in a local variable. + it("reports the released translation when the pan ends", () => { + pans.length = 0; + const onEnd = vi.fn(); + const root = createRoot(document.createElement("div")); + act(() => + root.render( + {}} + onMove={() => {}} + onEnd={onEnd} + onStep={() => {}} + />, + ), + ); + const pan = pans.at(-1)!; + + act(() => { + pan.onStart?.(); + pan.onUpdate?.({ translationY: 64 }); + pan.onUpdate?.({ translationY: 130 }); + pan.onFinalize?.({ translationY: 130 }, true); + }); + + expect(onEnd).toHaveBeenCalledWith(130, false); + act(() => root.unmount()); + }); +}); diff --git a/apps/mobile/src/features/settings/components/SettingsDragHandle.tsx b/apps/mobile/src/features/settings/components/SettingsDragHandle.tsx index c27db4ce28e45..9a73983f9da0a 100644 --- a/apps/mobile/src/features/settings/components/SettingsDragHandle.tsx +++ b/apps/mobile/src/features/settings/components/SettingsDragHandle.tsx @@ -18,22 +18,17 @@ export function SettingsDragHandle(props: { const onEnd = useEffectEvent((translation: number, cancelled: boolean) => props.onEnd(translation, cancelled), ); - const [gesture] = useState(() => { - let translation = 0; - return Gesture.Pan() + // The worklets plugin copies the values these callbacks close over when the gesture is built, + // so the drop reads its translation from the final event rather than a tracked variable. + const [gesture] = useState(() => + Gesture.Pan() .minDistance(0) .shouldCancelWhenOutside(false) .runOnJS(true) - .onStart(() => { - translation = 0; - onStart(); - }) - .onUpdate((event) => { - translation = event.translationY; - onMove(event.translationY); - }) - .onFinalize((_, success) => onEnd(translation, !success)); - }); + .onStart(() => onStart()) + .onUpdate((event) => onMove(event.translationY)) + .onFinalize((event, success) => onEnd(event.translationY, !success)), + ); return (