-
Notifications
You must be signed in to change notification settings - Fork 0
/
PermissionAudio.tsx
165 lines (155 loc) · 4.92 KB
/
PermissionAudio.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import React, {FC, useCallback, useEffect, useState} from 'react';
import {View, Text, StyleSheet, Linking, AppState} from 'react-native';
import {defineMessages, useIntl} from 'react-intl';
import {BottomSheetModalMethods} from '@gorhom/bottom-sheet/lib/typescript/types';
import AudioPermission from '../images/observationEdit/AudioPermission.svg';
import {BLACK, COMAPEO_BLUE, WHITE} from '../lib/styles';
import {BottomSheetModal} from './BottomSheetModal';
import {Button} from './Button';
import {Audio} from 'expo-av';
import {useNavigationFromRoot} from '../hooks/useNavigationWithTypes';
import {PermissionResponse} from 'expo-av/build/Audio';
const handleOpenSettings = () => {
Linking.openSettings();
};
interface PermissionAudio {
sheetRef: React.RefObject<BottomSheetModalMethods>;
closeSheet: () => void;
isOpen: boolean;
}
export const PermissionAudio: FC<PermissionAudio> = props => {
const {sheetRef, closeSheet, isOpen} = props;
const {formatMessage: t} = useIntl();
const navigation = useNavigationFromRoot();
const [permissionData, setPermissionData] = useState<PermissionResponse>();
const handlePermissionGranted = useCallback(() => {
closeSheet();
navigation.navigate('Home', {screen: 'Map'});
}, [closeSheet, navigation]);
const handlePermissionNotGranted = useCallback(async () => {
const response = await Audio.requestPermissionsAsync();
setPermissionData(response);
if (response.granted) {
handlePermissionGranted();
}
}, [handlePermissionGranted]);
const handlePermission = useCallback(async () => {
if (permissionData?.granted) {
handlePermissionGranted();
} else if (
permissionData?.status === 'denied' &&
!permissionData.canAskAgain
) {
handleOpenSettings();
} else if (permissionData?.status !== 'granted') {
handlePermissionNotGranted();
}
}, [handlePermissionGranted, handlePermissionNotGranted, permissionData]);
useEffect(() => {
const subscription = AppState.addEventListener('focus', () =>
Audio.getPermissionsAsync().then(setPermissionData),
);
return () => subscription.remove();
}, []);
return (
<BottomSheetModal
ref={sheetRef}
fullHeight
onDismiss={closeSheet}
isOpen={isOpen}>
<View style={styles.container}>
<View style={styles.mainWrapper}>
<AudioPermission />
<Text style={styles.title}>{t(m.title)}</Text>
<Text style={styles.description}>{t(m.description)}</Text>
</View>
<View style={styles.buttonsWrapper}>
<Button onPress={closeSheet} variant="outlined" fullWidth>
<Text style={styles.notNowButtonText}>{t(m.notNowButtonText)}</Text>
</Button>
<Button onPress={handlePermission} fullWidth>
<Text style={styles.allowPermissionButtonText}>
{permissionData?.status === 'denied' &&
!permissionData.canAskAgain
? t(m.goToSettingsButtonText)
: t(m.allowButtonText)}
</Text>
</Button>
</View>
</View>
</BottomSheetModal>
);
};
const m = defineMessages({
title: {
id: 'screens.AudioPermission.title',
defaultMessage: 'Recording Audio with CoMapeo',
description: 'Screen title for audio permission screen',
},
description: {
id: 'screens.AudioPermission.description',
defaultMessage:
'To record audio while using the app and in the background CoMapeo needs to access your microphone.',
description: 'Screen description for audio permission screen',
},
notNowButtonText: {
id: 'screens.AudioPermission.Button.notNow',
defaultMessage: 'Not Now',
description: 'Screen button text for not allowed audio permission',
},
allowButtonText: {
id: 'screens.AudioPermission.Button.allow',
defaultMessage: 'Allow',
description: 'Screen button text for allow the audio permission',
},
goToSettingsButtonText: {
id: 'screens.AudioPermission.Button.goToSettings',
defaultMessage: 'Go to Settings',
description:
'Screen button text for navigate user to settings when audio permission was denied',
},
});
const styles = StyleSheet.create({
container: {
height: '100%',
justifyContent: 'space-between',
},
mainWrapper: {
justifyContent: 'center',
alignItems: 'center',
flex: 1,
},
title: {
color: BLACK,
fontSize: 24,
fontWeight: 'bold',
alignSelf: 'center',
width: '65%',
textAlign: 'center',
marginTop: 20,
},
description: {
fontSize: 16,
color: BLACK,
alignSelf: 'center',
marginTop: 40,
textAlign: 'center',
paddingHorizontal: 40,
},
buttonsWrapper: {
gap: 20,
paddingHorizontal: 40,
marginBottom: 20,
justifyContent: 'flex-end',
},
notNowButtonText: {
fontWeight: '600',
fontSize: 16,
color: COMAPEO_BLUE,
},
allowPermissionButtonText: {
fontWeight: '600',
fontSize: 16,
color: WHITE,
},
});