@@ -9,7 +9,13 @@ import { useSafeAreaInsets } from "react-native-safe-area-context";
99import { type AppSymbolName , SymbolView } from "../../components/AppSymbol" ;
1010import { AppText as Text } from "../../components/AppText" ;
1111import { cn } from "../../lib/cn" ;
12- import type { MicrophoneKind , RememberedMicrophone } from "../../lib/microphonePriority" ;
12+ import {
13+ isMicrophoneDevice ,
14+ type MicrophoneEntry ,
15+ type MicrophoneKind ,
16+ microphoneEntryKey ,
17+ microphoneOrder ,
18+ } from "../../lib/microphonePriority" ;
1319import { mobilePreferencesAtom , updateMobilePreferencesAtom } from "../../state/preferences" ;
1420import { SettingsActionRow } from "./components/SettingsActionRow" ;
1521import { SettingsDragHandle } from "./components/SettingsDragHandle" ;
@@ -45,62 +51,95 @@ function useMicrophonePermission() {
4551 return { permission, request } ;
4652}
4753
48- const KINDS : Record < MicrophoneKind , { readonly label : string ; readonly icon : AppSymbolName } > = {
49- wired : { label : "Wired" , icon : "cable.connector" } ,
50- bluetooth : { label : "Bluetooth" , icon : "headphones" } ,
51- builtIn : { label : "Built-in" , icon : "iphone" } ,
52- carPlay : { label : "CarPlay" , icon : "car" } ,
54+ const KINDS : Record <
55+ MicrophoneKind ,
56+ {
57+ /** Shown under a remembered device's name. */
58+ readonly label : string ;
59+ readonly row : string ;
60+ readonly rowDescription : string ;
61+ readonly icon : AppSymbolName ;
62+ }
63+ > = {
64+ wired : {
65+ label : "Wired" ,
66+ row : "Other wired" ,
67+ rowDescription : "Headsets and USB microphones not listed" ,
68+ icon : "cable.connector" ,
69+ } ,
70+ bluetooth : {
71+ label : "Bluetooth" ,
72+ row : "Other Bluetooth" ,
73+ rowDescription : "AirPods, headsets, and cars not listed" ,
74+ icon : "headphones" ,
75+ } ,
76+ builtIn : {
77+ label : "Built-in" ,
78+ row : "Built-in microphone" ,
79+ rowDescription : "The microphone on this device" ,
80+ icon : "iphone" ,
81+ } ,
82+ carPlay : {
83+ label : "CarPlay" ,
84+ row : "CarPlay" ,
85+ rowDescription : "The car’s microphone" ,
86+ icon : "car" ,
87+ } ,
5388} ;
5489
5590/**
56- * Microphones voice input has seen, in the order it prefers them. iOS only.
57- * A microphone joins the list the first time it is connected during dictation.
91+ * Microphone kinds and remembered devices, in the order voice input prefers
92+ * them. iOS only. A device joins the list the first time it is connected
93+ * during dictation; until then its kind's row ranks it.
5894 */
5995export function SettingsMicrophoneRouteScreen ( ) {
6096 const insets = useSafeAreaInsets ( ) ;
6197 const preferencesResult = useAtomValue ( mobilePreferencesAtom ) ;
6298 const savePreferences = useAtomSet ( updateMobilePreferencesAtom ) ;
6399 const ready = AsyncResult . isSuccess ( preferencesResult ) && ! preferencesResult . waiting ;
64- const microphones = AsyncResult . isSuccess ( preferencesResult )
65- ? ( preferencesResult . value . microphones ?? [ ] )
66- : [ ] ;
100+ const entries = microphoneOrder (
101+ AsyncResult . isSuccess ( preferencesResult ) ? preferencesResult . value . microphones : undefined ,
102+ ) ;
103+ const hasDevices = entries . some ( isMicrophoneDevice ) ;
67104 const { permission, request : requestPermission } = useMicrophonePermission ( ) ;
68105 const [ editing , setEditing ] = useState ( false ) ;
69- const [ drag , setDrag ] = useState < { readonly uid : string ; readonly translation : number } | null > (
106+ const [ drag , setDrag ] = useState < { readonly key : string ; readonly translation : number } | null > (
70107 null ,
71108 ) ;
72109 const [ rowHeight , setRowHeight ] = useState ( 0 ) ;
73110 // Each drop remounts the rows so the new order and the cleared offsets land in one frame.
74111 const [ drops , setDrops ] = useState ( 0 ) ;
75112
76- // Transforms apply to the stored list , so a microphone recorded meanwhile is kept.
77- const move = ( uid : string , steps : number ) =>
113+ // Transforms apply to the stored order , so a device recorded meanwhile is kept.
114+ const move = ( key : string , steps : number ) =>
78115 savePreferences ( {
79116 transform : ( current ) => {
80- const next = [ ...( current . microphones ?? [ ] ) ] ;
81- const from = next . findIndex ( ( microphone ) => microphone . uid === uid ) ;
117+ const next = [ ...microphoneOrder ( current . microphones ) ] ;
118+ const from = next . findIndex ( ( entry ) => microphoneEntryKey ( entry ) === key ) ;
82119 const to = Math . min ( next . length - 1 , Math . max ( 0 , from + steps ) ) ;
83120 if ( from === - 1 || from === to ) return { } ;
84121 const [ moved ] = next . splice ( from , 1 ) ;
85122 next . splice ( to , 0 , moved ! ) ;
86123 return { microphones : next } ;
87124 } ,
88125 } ) ;
89- const forget = ( uid : string ) =>
126+ const forget = ( key : string ) =>
90127 savePreferences ( {
91128 transform : ( current ) => ( {
92- microphones : ( current . microphones ?? [ ] ) . filter ( ( microphone ) => microphone . uid !== uid ) ,
129+ microphones : microphoneOrder ( current . microphones ) . filter (
130+ ( entry ) => microphoneEntryKey ( entry ) !== key ,
131+ ) ,
93132 } ) ,
94133 } ) ;
95134 // A lifted row takes a neighbour's slot once it has moved past half of that row.
96135 const dropSteps = ( from : number , translation : number ) =>
97136 rowHeight === 0
98137 ? 0
99- : Math . min ( microphones . length - 1 , Math . max ( 0 , from + Math . round ( translation / rowHeight ) ) ) -
138+ : Math . min ( entries . length - 1 , Math . max ( 0 , from + Math . round ( translation / rowHeight ) ) ) -
100139 from ;
101140
102141 const dragFrom =
103- drag === null ? - 1 : microphones . findIndex ( ( microphone ) => microphone . uid === drag . uid ) ;
142+ drag === null ? - 1 : entries . findIndex ( ( entry ) => microphoneEntryKey ( entry ) === drag . key ) ;
104143 const dragTo = drag === null ? - 1 : dragFrom + dropSteps ( dragFrom , drag . translation ) ;
105144
106145 return (
@@ -130,74 +169,68 @@ export function SettingsMicrophoneRouteScreen() {
130169 </ Text >
131170 </ >
132171 ) : null }
133- { microphones . length === 0 ? (
134- permission === "granted" && ready ? (
135- < Text className = "px-2 text-sm text-foreground-muted" >
136- Microphones appear here after you use voice input with them connected.
137- </ Text >
138- ) : null
139- ) : (
140- < >
141- < SettingsSection
142- title = "Preferred order"
143- trailing = {
144- < Pressable
145- accessibilityRole = "button"
146- onPress = { ( ) => setEditing ( ( value ) => ! value ) }
147- className = "px-2 py-1 active:opacity-70"
148- >
149- < Text className = "text-sm font-t3-medium text-foreground" >
150- { editing ? "Done" : "Edit" }
151- </ Text >
152- </ Pressable >
153- }
154- >
155- { microphones . map ( ( microphone , index ) => {
156- const shift =
157- dragFrom === - 1 || index === dragFrom
158- ? 0
159- : dragFrom < dragTo && index > dragFrom && index <= dragTo
160- ? - rowHeight
161- : dragFrom > dragTo && index < dragFrom && index >= dragTo
162- ? rowHeight
163- : 0 ;
164- return (
165- < MicrophoneRow
166- key = { `${ microphone . uid } :${ drops } ` }
167- microphone = { microphone }
168- position = { index + 1 }
169- count = { microphones . length }
170- reorderable = { ready && microphones . length > 1 }
171- editing = { editing }
172- offset = { index === dragFrom ? ( drag ?. translation ?? 0 ) : shift }
173- lifted = { index === dragFrom }
174- onHeight = { setRowHeight }
175- onDragStart = { ( ) => setDrag ( { uid : microphone . uid , translation : 0 } ) }
176- onDragMove = { ( translation ) => setDrag ( { uid : microphone . uid , translation } ) }
177- onDragEnd = { ( translation , cancelled ) => {
178- setDrag ( null ) ;
179- setDrops ( ( count ) => count + 1 ) ;
180- if ( ! cancelled ) move ( microphone . uid , dropSteps ( index , translation ) ) ;
181- } }
182- onStep = { ( direction ) => move ( microphone . uid , direction === "up" ? - 1 : 1 ) }
183- onForget = { ( ) => forget ( microphone . uid ) }
184- />
185- ) ;
186- } ) }
187- </ SettingsSection >
188- < Text className = "px-2 text-sm text-foreground-muted" >
189- Voice input records from the first connected microphone in this list. New microphones
190- appear after you use voice input with them connected.
191- </ Text >
192- </ >
193- ) }
172+ < SettingsSection
173+ title = "Preferred order"
174+ trailing = {
175+ hasDevices ? (
176+ < Pressable
177+ accessibilityRole = "button"
178+ onPress = { ( ) => setEditing ( ( value ) => ! value ) }
179+ className = "px-2 py-1 active:opacity-70"
180+ >
181+ < Text className = "text-sm font-t3-medium text-foreground" >
182+ { editing ? "Done" : "Edit" }
183+ </ Text >
184+ </ Pressable >
185+ ) : undefined
186+ }
187+ >
188+ { entries . map ( ( entry , index ) => {
189+ const key = microphoneEntryKey ( entry ) ;
190+ const shift =
191+ dragFrom === - 1 || index === dragFrom
192+ ? 0
193+ : dragFrom < dragTo && index > dragFrom && index <= dragTo
194+ ? - rowHeight
195+ : dragFrom > dragTo && index < dragFrom && index >= dragTo
196+ ? rowHeight
197+ : 0 ;
198+ return (
199+ < MicrophoneRow
200+ key = { `${ key } :${ drops } ` }
201+ entry = { entry }
202+ position = { index + 1 }
203+ count = { entries . length }
204+ reorderable = { ready }
205+ editing = { editing && hasDevices }
206+ offset = { index === dragFrom ? ( drag ?. translation ?? 0 ) : shift }
207+ lifted = { index === dragFrom }
208+ onHeight = { setRowHeight }
209+ onDragStart = { ( ) => setDrag ( { key, translation : 0 } ) }
210+ onDragMove = { ( translation ) => setDrag ( { key, translation } ) }
211+ onDragEnd = { ( translation , cancelled ) => {
212+ setDrag ( null ) ;
213+ setDrops ( ( count ) => count + 1 ) ;
214+ if ( ! cancelled ) move ( key , dropSteps ( index , translation ) ) ;
215+ } }
216+ onStep = { ( direction ) => move ( key , direction === "up" ? - 1 : 1 ) }
217+ onForget = { isMicrophoneDevice ( entry ) ? ( ) => forget ( key ) : undefined }
218+ />
219+ ) ;
220+ } ) }
221+ </ SettingsSection >
222+ < Text className = "px-2 text-sm text-foreground-muted" >
223+ Voice input records from the first connected microphone in this list. Wired and Bluetooth
224+ devices are added by name after you use voice input with them connected; until then, the
225+ Other row ranks them.
226+ </ Text >
194227 </ ScrollView >
195228 </ View >
196229 ) ;
197230}
198231
199232function MicrophoneRow ( props : {
200- readonly microphone : RememberedMicrophone ;
233+ readonly entry : MicrophoneEntry ;
201234 readonly position : number ;
202235 readonly count : number ;
203236 readonly reorderable : boolean ;
@@ -209,10 +242,11 @@ function MicrophoneRow(props: {
209242 readonly onDragMove : ( translation : number ) => void ;
210243 readonly onDragEnd : ( translation : number , cancelled : boolean ) => void ;
211244 readonly onStep : ( direction : "up" | "down" ) => void ;
212- readonly onForget : ( ) => void ;
245+ readonly onForget : ( ( ) => void ) | undefined ;
213246} ) {
214- const { lifted, offset, microphone } = props ;
215- const kind = KINDS [ microphone . kind ] ;
247+ const { lifted, offset, entry } = props ;
248+ const kind = KINDS [ entry . kind ] ;
249+ const title = isMicrophoneDevice ( entry ) ? entry . name : kind . row ;
216250 const style = useAnimatedStyle ( ( ) => ( {
217251 transform : [
218252 {
@@ -234,10 +268,10 @@ function MicrophoneRow(props: {
234268 ! props . reorderable && "pr-4" ,
235269 ) }
236270 >
237- { props . editing ? (
271+ { props . editing && props . onForget ? (
238272 < Pressable
239273 accessibilityRole = "button"
240- accessibilityLabel = { `Forget ${ microphone . name } ` }
274+ accessibilityLabel = { `Forget ${ title } ` }
241275 hitSlop = { 8 }
242276 onPress = { props . onForget }
243277 className = "active:opacity-70"
@@ -249,10 +283,12 @@ function MicrophoneRow(props: {
249283 type = "monochrome"
250284 />
251285 </ Pressable >
286+ ) : props . editing ? (
287+ < View style = { { width : REMOVE_SIZE } } />
252288 ) : null }
253289 < View
254290 accessible
255- accessibilityLabel = { `${ microphone . name } , ${ kind . label } , ${ props . position } of ${ props . count } ` }
291+ accessibilityLabel = { `${ title } , ${ kind . label } , ${ props . position } of ${ props . count } ` }
256292 className = "min-w-0 flex-1 flex-row items-center gap-4 py-4"
257293 >
258294 < SymbolView
@@ -264,16 +300,16 @@ function MicrophoneRow(props: {
264300 />
265301 < View className = "min-w-0 flex-1 gap-0.5" >
266302 < Text numberOfLines = { 1 } className = "text-lg text-foreground" >
267- { microphone . name }
303+ { title }
268304 </ Text >
269305 < Text numberOfLines = { 1 } className = "text-sm text-foreground-muted" >
270- { kind . label }
306+ { isMicrophoneDevice ( entry ) ? kind . label : kind . rowDescription }
271307 </ Text >
272308 </ View >
273309 </ View >
274310 { props . reorderable ? (
275311 < SettingsDragHandle
276- title = { microphone . name }
312+ title = { title }
277313 canMoveUp = { props . position > 1 }
278314 canMoveDown = { props . position < props . count }
279315 onStart = { props . onDragStart }
0 commit comments