11import { useAtomSet , useAtomValue } from "@effect/atom-react" ;
22import { AsyncResult } from "effect/reactivity" ;
3- import { useState } from "react" ;
4- import { Pressable , ScrollView , View } from "react-native" ;
3+ import { getRecordingPermissionsAsync , requestRecordingPermissionsAsync } from "expo-audio" ;
4+ import { useCallback , useEffect , useState } from "react" ;
5+ import { AppState , Linking , Pressable , ScrollView , View } from "react-native" ;
56import Reanimated , { ReduceMotion , useAnimatedStyle , withTiming } from "react-native-reanimated" ;
67import { useSafeAreaInsets } from "react-native-safe-area-context" ;
78
@@ -10,11 +11,40 @@ import { AppText as Text } from "../../components/AppText";
1011import { cn } from "../../lib/cn" ;
1112import type { MicrophoneKind , RememberedMicrophone } from "../../lib/microphonePriority" ;
1213import { mobilePreferencesAtom , updateMobilePreferencesAtom } from "../../state/preferences" ;
14+ import { SettingsActionRow } from "./components/SettingsActionRow" ;
1315import { SettingsDragHandle } from "./components/SettingsDragHandle" ;
1416import { SettingsSection } from "./components/SettingsSection" ;
1517
1618const REMOVE_SIZE = 20 ;
1719
20+ type MicrophonePermission = "checking" | "granted" | "ask" | "denied" ;
21+
22+ /** Re-reads microphone access whenever the app returns from system Settings. */
23+ function useMicrophonePermission ( ) {
24+ const [ permission , setPermission ] = useState < MicrophonePermission > ( "checking" ) ;
25+ const apply = useCallback (
26+ ( response : { readonly granted : boolean ; readonly canAskAgain : boolean } ) =>
27+ setPermission ( response . granted ? "granted" : response . canAskAgain ? "ask" : "denied" ) ,
28+ [ ] ,
29+ ) ;
30+ const refresh = useCallback (
31+ ( ) => void getRecordingPermissionsAsync ( ) . then ( apply , ( ) => setPermission ( "ask" ) ) ,
32+ [ apply ] ,
33+ ) ;
34+ useEffect ( ( ) => {
35+ refresh ( ) ;
36+ const subscription = AppState . addEventListener ( "change" , ( state ) => {
37+ if ( state === "active" ) refresh ( ) ;
38+ } ) ;
39+ return ( ) => subscription . remove ( ) ;
40+ } , [ refresh ] ) ;
41+ const request = useCallback (
42+ ( ) => void requestRecordingPermissionsAsync ( ) . then ( apply , refresh ) ,
43+ [ apply , refresh ] ,
44+ ) ;
45+ return { permission, request } ;
46+ }
47+
1848const KINDS : Record < MicrophoneKind , { readonly label : string ; readonly icon : AppSymbolName } > = {
1949 wired : { label : "Wired" , icon : "cable.connector" } ,
2050 bluetooth : { label : "Bluetooth" , icon : "headphones" } ,
@@ -34,6 +64,7 @@ export function SettingsMicrophoneRouteScreen() {
3464 const microphones = AsyncResult . isSuccess ( preferencesResult )
3565 ? ( preferencesResult . value . microphones ?? [ ] )
3666 : [ ] ;
67+ const { permission, request : requestPermission } = useMicrophonePermission ( ) ;
3768 const [ editing , setEditing ] = useState ( false ) ;
3869 const [ drag , setDrag ] = useState < { readonly uid : string ; readonly translation : number } | null > (
3970 null ,
@@ -81,12 +112,30 @@ export function SettingsMicrophoneRouteScreen() {
81112 contentContainerClassName = "gap-3 px-5 pt-4"
82113 contentContainerStyle = { { paddingBottom : Math . max ( insets . bottom , 18 ) + 18 } }
83114 >
115+ { permission === "ask" || permission === "denied" ? (
116+ < >
117+ < SettingsSection title = "Microphone access" >
118+ < SettingsActionRow
119+ icon = "mic"
120+ label = { permission === "ask" ? "Allow microphone access" : "Open Settings" }
121+ onPress = {
122+ permission === "ask" ? requestPermission : ( ) => void Linking . openSettings ( )
123+ }
124+ />
125+ </ SettingsSection >
126+ < Text className = "px-2 text-sm text-foreground-muted" >
127+ { permission === "ask"
128+ ? "Voice input needs microphone access before it can record or list your microphones."
129+ : "Microphone access is off for T3 Code. Turn it on in Settings to use voice input." }
130+ </ Text >
131+ </ >
132+ ) : null }
84133 { 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 >
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
90139 ) : (
91140 < >
92141 < SettingsSection
0 commit comments