|
| 1 | +import { useAtomSet, useAtomValue } from "@effect/atom-react"; |
| 2 | +import { AsyncResult } from "effect/reactivity"; |
| 3 | +import { useState } from "react"; |
| 4 | +import { ScrollView, View } from "react-native"; |
| 5 | +import Reanimated, { ReduceMotion, useAnimatedStyle, withTiming } from "react-native-reanimated"; |
| 6 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 7 | + |
| 8 | +import { type AppSymbolName, SymbolView } from "../../components/AppSymbol"; |
| 9 | +import { AppText as Text } from "../../components/AppText"; |
| 10 | +import { cn } from "../../lib/cn"; |
| 11 | +import { type MicrophoneKind, resolveMicrophonePriority } from "../../lib/microphonePriority"; |
| 12 | +import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences"; |
| 13 | +import { SettingsDragHandle } from "./components/SettingsDragHandle"; |
| 14 | +import { SettingsSection } from "./components/SettingsSection"; |
| 15 | + |
| 16 | +const MICROPHONES: Record< |
| 17 | + MicrophoneKind, |
| 18 | + { readonly label: string; readonly description: string; readonly icon: AppSymbolName } |
| 19 | +> = { |
| 20 | + wired: { |
| 21 | + label: "Wired", |
| 22 | + description: "Headsets and USB microphones", |
| 23 | + icon: "cable.connector", |
| 24 | + }, |
| 25 | + bluetooth: { |
| 26 | + label: "Bluetooth", |
| 27 | + description: "AirPods, headsets, and cars without CarPlay", |
| 28 | + icon: "airpods", |
| 29 | + }, |
| 30 | + builtIn: { |
| 31 | + label: "iPhone", |
| 32 | + description: "The microphone on this device", |
| 33 | + icon: "iphone", |
| 34 | + }, |
| 35 | + carPlay: { |
| 36 | + label: "CarPlay", |
| 37 | + description: "The car’s microphone", |
| 38 | + icon: "car", |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +/** Voice input records from the first connected microphone in this order. iOS only. */ |
| 43 | +export function SettingsMicrophoneRouteScreen() { |
| 44 | + const insets = useSafeAreaInsets(); |
| 45 | + const preferencesResult = useAtomValue(mobilePreferencesAtom); |
| 46 | + const savePreferences = useAtomSet(updateMobilePreferencesAtom); |
| 47 | + const ready = AsyncResult.isSuccess(preferencesResult) && !preferencesResult.waiting; |
| 48 | + const order = resolveMicrophonePriority( |
| 49 | + AsyncResult.isSuccess(preferencesResult) ? preferencesResult.value.microphonePriority : [], |
| 50 | + ); |
| 51 | + const [drag, setDrag] = useState<{ |
| 52 | + readonly kind: MicrophoneKind; |
| 53 | + readonly translation: number; |
| 54 | + } | null>(null); |
| 55 | + const [rowHeight, setRowHeight] = useState(0); |
| 56 | + // Each drop remounts the rows so the new order and the cleared offsets land in one frame. |
| 57 | + const [drops, setDrops] = useState(0); |
| 58 | + |
| 59 | + const move = (from: number, to: number) => { |
| 60 | + if (to < 0 || to >= order.length || from === to) return; |
| 61 | + const next = [...order]; |
| 62 | + const [moved] = next.splice(from, 1); |
| 63 | + next.splice(to, 0, moved!); |
| 64 | + savePreferences({ microphonePriority: next }); |
| 65 | + }; |
| 66 | + // A lifted row takes a neighbour's slot once it has moved past half of that row. |
| 67 | + const dropIndex = (from: number, translation: number) => |
| 68 | + rowHeight === 0 |
| 69 | + ? from |
| 70 | + : Math.min(order.length - 1, Math.max(0, from + Math.round(translation / rowHeight))); |
| 71 | + |
| 72 | + const dragFrom = drag === null ? -1 : order.indexOf(drag.kind); |
| 73 | + const dragTo = drag === null ? -1 : dropIndex(dragFrom, drag.translation); |
| 74 | + |
| 75 | + return ( |
| 76 | + <View collapsable={false} className="flex-1 bg-sheet"> |
| 77 | + <ScrollView |
| 78 | + contentInsetAdjustmentBehavior="automatic" |
| 79 | + showsVerticalScrollIndicator={false} |
| 80 | + className="flex-1" |
| 81 | + contentContainerClassName="gap-3 px-5 pt-4" |
| 82 | + contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }} |
| 83 | + > |
| 84 | + <SettingsSection title="Preferred order"> |
| 85 | + {order.map((kind, index) => { |
| 86 | + const shift = |
| 87 | + dragFrom === -1 || index === dragFrom |
| 88 | + ? 0 |
| 89 | + : dragFrom < dragTo && index > dragFrom && index <= dragTo |
| 90 | + ? -rowHeight |
| 91 | + : dragFrom > dragTo && index < dragFrom && index >= dragTo |
| 92 | + ? rowHeight |
| 93 | + : 0; |
| 94 | + return ( |
| 95 | + <MicrophoneRow |
| 96 | + key={`${kind}:${drops}`} |
| 97 | + kind={kind} |
| 98 | + position={index + 1} |
| 99 | + count={order.length} |
| 100 | + disabled={!ready} |
| 101 | + offset={index === dragFrom ? (drag?.translation ?? 0) : shift} |
| 102 | + lifted={index === dragFrom} |
| 103 | + onHeight={setRowHeight} |
| 104 | + onDragStart={() => setDrag({ kind, translation: 0 })} |
| 105 | + onDragMove={(translation) => setDrag({ kind, translation })} |
| 106 | + onDragEnd={(translation, cancelled) => { |
| 107 | + setDrag(null); |
| 108 | + setDrops((count) => count + 1); |
| 109 | + if (!cancelled) move(index, dropIndex(index, translation)); |
| 110 | + }} |
| 111 | + onStep={(direction) => move(index, direction === "up" ? index - 1 : index + 1)} |
| 112 | + /> |
| 113 | + ); |
| 114 | + })} |
| 115 | + </SettingsSection> |
| 116 | + <Text className="px-2 text-sm text-foreground-muted"> |
| 117 | + Voice input records from the first connected microphone in this list. Drag to reorder. |
| 118 | + </Text> |
| 119 | + </ScrollView> |
| 120 | + </View> |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +function MicrophoneRow(props: { |
| 125 | + readonly kind: MicrophoneKind; |
| 126 | + readonly position: number; |
| 127 | + readonly count: number; |
| 128 | + readonly disabled: boolean; |
| 129 | + readonly offset: number; |
| 130 | + readonly lifted: boolean; |
| 131 | + readonly onHeight: (height: number) => void; |
| 132 | + readonly onDragStart: () => void; |
| 133 | + readonly onDragMove: (translation: number) => void; |
| 134 | + readonly onDragEnd: (translation: number, cancelled: boolean) => void; |
| 135 | + readonly onStep: (direction: "up" | "down") => void; |
| 136 | +}) { |
| 137 | + const { lifted, offset } = props; |
| 138 | + const microphone = MICROPHONES[props.kind]; |
| 139 | + const style = useAnimatedStyle(() => ({ |
| 140 | + transform: [ |
| 141 | + { |
| 142 | + translateY: lifted |
| 143 | + ? offset |
| 144 | + : withTiming(offset, { duration: 160, reduceMotion: ReduceMotion.System }), |
| 145 | + }, |
| 146 | + ], |
| 147 | + zIndex: lifted ? 1 : 0, |
| 148 | + })); |
| 149 | + return ( |
| 150 | + <Reanimated.View |
| 151 | + style={style} |
| 152 | + onLayout={(event) => props.onHeight(event.nativeEvent.layout.height)} |
| 153 | + className={cn( |
| 154 | + "flex-row items-center gap-4 pl-4", |
| 155 | + props.position > 1 && !lifted && "border-t border-border-subtle", |
| 156 | + lifted && "bg-grouped-card shadow-md", |
| 157 | + )} |
| 158 | + > |
| 159 | + <View |
| 160 | + accessible |
| 161 | + accessibilityLabel={`${microphone.label}, ${microphone.description}, ${props.position} of ${props.count}`} |
| 162 | + className="min-w-0 flex-1 flex-row items-center gap-4 py-4" |
| 163 | + > |
| 164 | + <SymbolView |
| 165 | + name={microphone.icon} |
| 166 | + size={22} |
| 167 | + tintColorClassName="accent-icon" |
| 168 | + type="monochrome" |
| 169 | + weight="regular" |
| 170 | + /> |
| 171 | + <View className="min-w-0 flex-1 gap-0.5"> |
| 172 | + <Text numberOfLines={1} className="text-lg text-foreground"> |
| 173 | + {microphone.label} |
| 174 | + </Text> |
| 175 | + <Text numberOfLines={1} className="text-sm text-foreground-muted"> |
| 176 | + {microphone.description} |
| 177 | + </Text> |
| 178 | + </View> |
| 179 | + </View> |
| 180 | + {props.disabled ? null : ( |
| 181 | + <SettingsDragHandle |
| 182 | + title={microphone.label} |
| 183 | + canMoveUp={props.position > 1} |
| 184 | + canMoveDown={props.position < props.count} |
| 185 | + onStart={props.onDragStart} |
| 186 | + onMove={props.onDragMove} |
| 187 | + onEnd={props.onDragEnd} |
| 188 | + onStep={props.onStep} |
| 189 | + /> |
| 190 | + )} |
| 191 | + </Reanimated.View> |
| 192 | + ); |
| 193 | +} |
0 commit comments