Skip to content

Commit 2b14a86

Browse files
feat(mobile): rank remembered microphones by device
Kinds could not tell AirPods from a car without CarPlay, since both connect as Bluetooth headsets. Voice input now remembers each connected microphone by its iOS port identifier and name the first time it records near it, and Settings → Microphone lists those devices for the user to drag into order or forget. A new device joins after the last one of its kind, so an existing preference carries over; CarPlay still starts below the built-in microphone. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e2cea12 commit 2b14a86

9 files changed

Lines changed: 293 additions & 163 deletions

File tree

‎apps/mobile/src/components/AppSymbol.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ const ANDROID_ICON_BY_SF_SYMBOL = {
143143
"arrow.up.right.circle": IconArrowUpRightCircle,
144144
"arrow.uturn.backward": IconArrowBackUp,
145145
"arrow.uturn.forward": IconArrowForwardUp,
146-
airpods: IconHeadphones,
147146
archivebox: IconArchive,
148147
"archivebox.fill": IconArchive,
149148
"bell.badge": IconBellRinging,
@@ -184,6 +183,7 @@ const ANDROID_ICON_BY_SF_SYMBOL = {
184183
gearshape: IconSettings,
185184
globe: IconWorld,
186185
hammer: IconHammer,
186+
headphones: IconHeadphones,
187187
house: IconHome,
188188
"info.circle": IconInfoCircle,
189189
internaldrive: IconDatabase,
Lines changed: 136 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,76 @@
11
import { useAtomSet, useAtomValue } from "@effect/atom-react";
22
import { AsyncResult } from "effect/reactivity";
33
import { useState } from "react";
4-
import { ScrollView, View } from "react-native";
4+
import { Pressable, ScrollView, View } from "react-native";
55
import Reanimated, { ReduceMotion, useAnimatedStyle, withTiming } from "react-native-reanimated";
66
import { useSafeAreaInsets } from "react-native-safe-area-context";
77

88
import { type AppSymbolName, SymbolView } from "../../components/AppSymbol";
99
import { AppText as Text } from "../../components/AppText";
1010
import { cn } from "../../lib/cn";
11-
import { type MicrophoneKind, resolveMicrophonePriority } from "../../lib/microphonePriority";
11+
import type { MicrophoneKind, RememberedMicrophone } from "../../lib/microphonePriority";
1212
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
1313
import { SettingsDragHandle } from "./components/SettingsDragHandle";
1414
import { SettingsSection } from "./components/SettingsSection";
1515

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-
},
16+
const REMOVE_SIZE = 20;
17+
18+
const KINDS: Record<MicrophoneKind, { readonly label: string; readonly icon: AppSymbolName }> = {
19+
wired: { label: "Wired", icon: "cable.connector" },
20+
bluetooth: { label: "Bluetooth", icon: "headphones" },
21+
builtIn: { label: "Built-in", icon: "iphone" },
22+
carPlay: { label: "CarPlay", icon: "car" },
4023
};
4124

42-
/** Voice input records from the first connected microphone in this order. iOS only. */
25+
/**
26+
* Microphones voice input has seen, in the order it prefers them. iOS only.
27+
* A microphone joins the list the first time it is connected during dictation.
28+
*/
4329
export function SettingsMicrophoneRouteScreen() {
4430
const insets = useSafeAreaInsets();
4531
const preferencesResult = useAtomValue(mobilePreferencesAtom);
4632
const savePreferences = useAtomSet(updateMobilePreferencesAtom);
4733
const ready = AsyncResult.isSuccess(preferencesResult) && !preferencesResult.waiting;
48-
const order = resolveMicrophonePriority(
49-
AsyncResult.isSuccess(preferencesResult) ? preferencesResult.value.microphonePriority : [],
34+
const microphones = AsyncResult.isSuccess(preferencesResult)
35+
? (preferencesResult.value.microphones ?? [])
36+
: [];
37+
const [editing, setEditing] = useState(false);
38+
const [drag, setDrag] = useState<{ readonly uid: string; readonly translation: number } | null>(
39+
null,
5040
);
51-
const [drag, setDrag] = useState<{
52-
readonly kind: MicrophoneKind;
53-
readonly translation: number;
54-
} | null>(null);
5541
const [rowHeight, setRowHeight] = useState(0);
5642
// Each drop remounts the rows so the new order and the cleared offsets land in one frame.
5743
const [drops, setDrops] = useState(0);
5844

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-
};
45+
// Transforms apply to the stored list, so a microphone recorded meanwhile is kept.
46+
const move = (uid: string, steps: number) =>
47+
savePreferences({
48+
transform: (current) => {
49+
const next = [...(current.microphones ?? [])];
50+
const from = next.findIndex((microphone) => microphone.uid === uid);
51+
const to = Math.min(next.length - 1, Math.max(0, from + steps));
52+
if (from === -1 || from === to) return {};
53+
const [moved] = next.splice(from, 1);
54+
next.splice(to, 0, moved!);
55+
return { microphones: next };
56+
},
57+
});
58+
const forget = (uid: string) =>
59+
savePreferences({
60+
transform: (current) => ({
61+
microphones: (current.microphones ?? []).filter((microphone) => microphone.uid !== uid),
62+
}),
63+
});
6664
// A lifted row takes a neighbour's slot once it has moved past half of that row.
67-
const dropIndex = (from: number, translation: number) =>
65+
const dropSteps = (from: number, translation: number) =>
6866
rowHeight === 0
69-
? from
70-
: Math.min(order.length - 1, Math.max(0, from + Math.round(translation / rowHeight)));
67+
? 0
68+
: Math.min(microphones.length - 1, Math.max(0, from + Math.round(translation / rowHeight))) -
69+
from;
7170

72-
const dragFrom = drag === null ? -1 : order.indexOf(drag.kind);
73-
const dragTo = drag === null ? -1 : dropIndex(dragFrom, drag.translation);
71+
const dragFrom =
72+
drag === null ? -1 : microphones.findIndex((microphone) => microphone.uid === drag.uid);
73+
const dragTo = drag === null ? -1 : dragFrom + dropSteps(dragFrom, drag.translation);
7474

7575
return (
7676
<View collapsable={false} className="flex-1 bg-sheet">
@@ -81,61 +81,89 @@ export function SettingsMicrophoneRouteScreen() {
8181
contentContainerClassName="gap-3 px-5 pt-4"
8282
contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
8383
>
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>
84+
{microphones.length === 0 ? (
85+
<Text className="px-2 text-sm text-foreground-muted">
86+
{ready
87+
? "Microphones appear here after you use voice input with them connected."
88+
: null}
89+
</Text>
90+
) : (
91+
<>
92+
<SettingsSection
93+
title="Preferred order"
94+
trailing={
95+
<Pressable
96+
accessibilityRole="button"
97+
onPress={() => setEditing((value) => !value)}
98+
className="px-2 py-1 active:opacity-70"
99+
>
100+
<Text className="text-sm font-t3-medium text-foreground">
101+
{editing ? "Done" : "Edit"}
102+
</Text>
103+
</Pressable>
104+
}
105+
>
106+
{microphones.map((microphone, index) => {
107+
const shift =
108+
dragFrom === -1 || index === dragFrom
109+
? 0
110+
: dragFrom < dragTo && index > dragFrom && index <= dragTo
111+
? -rowHeight
112+
: dragFrom > dragTo && index < dragFrom && index >= dragTo
113+
? rowHeight
114+
: 0;
115+
return (
116+
<MicrophoneRow
117+
key={`${microphone.uid}:${drops}`}
118+
microphone={microphone}
119+
position={index + 1}
120+
count={microphones.length}
121+
reorderable={ready && microphones.length > 1}
122+
editing={editing}
123+
offset={index === dragFrom ? (drag?.translation ?? 0) : shift}
124+
lifted={index === dragFrom}
125+
onHeight={setRowHeight}
126+
onDragStart={() => setDrag({ uid: microphone.uid, translation: 0 })}
127+
onDragMove={(translation) => setDrag({ uid: microphone.uid, translation })}
128+
onDragEnd={(translation, cancelled) => {
129+
setDrag(null);
130+
setDrops((count) => count + 1);
131+
if (!cancelled) move(microphone.uid, dropSteps(index, translation));
132+
}}
133+
onStep={(direction) => move(microphone.uid, direction === "up" ? -1 : 1)}
134+
onForget={() => forget(microphone.uid)}
135+
/>
136+
);
137+
})}
138+
</SettingsSection>
139+
<Text className="px-2 text-sm text-foreground-muted">
140+
Voice input records from the first connected microphone in this list. New microphones
141+
appear after you use voice input with them connected.
142+
</Text>
143+
</>
144+
)}
119145
</ScrollView>
120146
</View>
121147
);
122148
}
123149

124150
function MicrophoneRow(props: {
125-
readonly kind: MicrophoneKind;
151+
readonly microphone: RememberedMicrophone;
126152
readonly position: number;
127153
readonly count: number;
128-
readonly disabled: boolean;
154+
readonly reorderable: boolean;
155+
readonly editing: boolean;
129156
readonly offset: number;
130157
readonly lifted: boolean;
131158
readonly onHeight: (height: number) => void;
132159
readonly onDragStart: () => void;
133160
readonly onDragMove: (translation: number) => void;
134161
readonly onDragEnd: (translation: number, cancelled: boolean) => void;
135162
readonly onStep: (direction: "up" | "down") => void;
163+
readonly onForget: () => void;
136164
}) {
137-
const { lifted, offset } = props;
138-
const microphone = MICROPHONES[props.kind];
165+
const { lifted, offset, microphone } = props;
166+
const kind = KINDS[microphone.kind];
139167
const style = useAnimatedStyle(() => ({
140168
transform: [
141169
{
@@ -154,40 +182,57 @@ function MicrophoneRow(props: {
154182
"flex-row items-center gap-4 pl-4",
155183
props.position > 1 && !lifted && "border-t border-border-subtle",
156184
lifted && "bg-grouped-card shadow-md",
185+
!props.reorderable && "pr-4",
157186
)}
158187
>
188+
{props.editing ? (
189+
<Pressable
190+
accessibilityRole="button"
191+
accessibilityLabel={`Forget ${microphone.name}`}
192+
hitSlop={8}
193+
onPress={props.onForget}
194+
className="active:opacity-70"
195+
>
196+
<SymbolView
197+
name="xmark.circle.fill"
198+
size={REMOVE_SIZE}
199+
tintColorClassName="accent-danger-foreground"
200+
type="monochrome"
201+
/>
202+
</Pressable>
203+
) : null}
159204
<View
160205
accessible
161-
accessibilityLabel={`${microphone.label}, ${microphone.description}, ${props.position} of ${props.count}`}
206+
accessibilityLabel={`${microphone.name}, ${kind.label}, ${props.position} of ${props.count}`}
162207
className="min-w-0 flex-1 flex-row items-center gap-4 py-4"
163208
>
164209
<SymbolView
165-
name={microphone.icon}
210+
name={kind.icon}
166211
size={22}
167212
tintColorClassName="accent-icon"
168213
type="monochrome"
169214
weight="regular"
170215
/>
171216
<View className="min-w-0 flex-1 gap-0.5">
172217
<Text numberOfLines={1} className="text-lg text-foreground">
173-
{microphone.label}
218+
{microphone.name}
174219
</Text>
175220
<Text numberOfLines={1} className="text-sm text-foreground-muted">
176-
{microphone.description}
221+
{kind.label}
177222
</Text>
178223
</View>
179224
</View>
180-
{props.disabled ? null : (
225+
{props.reorderable ? (
181226
<SettingsDragHandle
182-
title={microphone.label}
227+
title={microphone.name}
183228
canMoveUp={props.position > 1}
184229
canMoveDown={props.position < props.count}
185230
onStart={props.onDragStart}
186231
onMove={props.onDragMove}
187232
onEnd={props.onDragEnd}
188233
onStep={props.onStep}
189234
/>
190-
)}
235+
) : null}
191236
</Reanimated.View>
192237
);
193238
}

‎apps/mobile/src/features/voice-input/VoiceInputProvider.tsx‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RegistryContext } from "@effect/atom-react";
1+
import { RegistryContext, useAtomMount } from "@effect/atom-react";
22
import {
33
AudioModule,
44
RecordingPresets,
@@ -24,9 +24,9 @@ import { AppState, Platform } from "react-native";
2424
import { AsyncResult } from "effect/reactivity";
2525
import { useSharedValue } from "react-native-reanimated";
2626

27-
import { preferredMicrophoneInput, resolveMicrophonePriority } from "../../lib/microphonePriority";
27+
import { preferredMicrophoneInput, rememberMicrophones } from "../../lib/microphonePriority";
2828
import { getLocalVoiceTranscriber } from "../../native/voiceTranscription";
29-
import { mobilePreferencesAtom } from "../../state/preferences";
29+
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
3030
import { getNativeShowcaseScene } from "../showcase/nativeShowcaseScene";
3131
import {
3232
VOICE_RECORDING_LIMIT_SECONDS,
@@ -96,6 +96,9 @@ export function useGlobalVoiceInput() {
9696

9797
function useVoiceInputRuntime() {
9898
const registry = use(RegistryContext);
99+
// Dictation reads and records microphones without subscribing the provider to preferences.
100+
useAtomMount(mobilePreferencesAtom);
101+
useAtomMount(updateMobilePreferencesAtom);
99102
const [{ state, ownerKey, label }, setState] = useState({
100103
state: INITIAL_STATE,
101104
ownerKey: null as string | null,
@@ -134,12 +137,17 @@ function useVoiceInputRuntime() {
134137
// Read when recording starts so preference changes do not re-render every composer.
135138
selectInput: (inputs) => {
136139
const preferences = registry.get(mobilePreferencesAtom);
137-
return preferredMicrophoneInput(
138-
inputs,
139-
resolveMicrophonePriority(
140-
AsyncResult.isSuccess(preferences) ? preferences.value.microphonePriority : undefined,
141-
),
142-
);
140+
if (!AsyncResult.isSuccess(preferences)) return null;
141+
const known = preferences.value.microphones ?? [];
142+
const microphones = rememberMicrophones(known, inputs);
143+
if (microphones !== known) {
144+
registry.set(updateMobilePreferencesAtom, {
145+
transform: (current) => ({
146+
microphones: rememberMicrophones(current.microphones ?? [], inputs),
147+
}),
148+
});
149+
}
150+
return preferredMicrophoneInput(inputs, microphones);
143151
},
144152
});
145153
recorderRef.current = recorder;

‎apps/mobile/src/features/voice-input/lazyVoiceRecorder.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ function createNativeRecorder(uri: string) {
3131
return { remove: () => listeners.delete(listener) };
3232
}),
3333
getAvailableInputs: vi.fn((): ReadonlyArray<VoiceRecordingInput> => [
34-
{ uid: "car", type: "CarAudio" },
35-
{ uid: "built-in", type: "MicrophoneBuiltIn" },
34+
{ uid: "car", name: "CarPlay", type: "CarAudio" },
35+
{ uid: "built-in", name: "iPhone Microphone", type: "MicrophoneBuiltIn" },
3636
]),
3737
setInput: vi.fn((_uid: string) => undefined),
3838
release: vi.fn(),

0 commit comments

Comments
 (0)