11import { useAtomSet , useAtomValue } from "@effect/atom-react" ;
22import { AsyncResult } from "effect/reactivity" ;
33import { useState } from "react" ;
4- import { ScrollView , View } from "react-native" ;
4+ import { Pressable , ScrollView , View } from "react-native" ;
55import Reanimated , { ReduceMotion , useAnimatedStyle , withTiming } from "react-native-reanimated" ;
66import { useSafeAreaInsets } from "react-native-safe-area-context" ;
77
88import { type AppSymbolName , SymbolView } from "../../components/AppSymbol" ;
99import { AppText as Text } from "../../components/AppText" ;
1010import { cn } from "../../lib/cn" ;
11- import { type MicrophoneKind , resolveMicrophonePriority } from "../../lib/microphonePriority" ;
11+ import type { MicrophoneKind , RememberedMicrophone } from "../../lib/microphonePriority" ;
1212import { mobilePreferencesAtom , updateMobilePreferencesAtom } from "../../state/preferences" ;
1313import { SettingsDragHandle } from "./components/SettingsDragHandle" ;
1414import { 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+ */
4329export 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
124150function 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}
0 commit comments