react-native-vision-camera/example/src/PermissionsPage.tsx

97 lines
3.3 KiB
TypeScript
Raw Normal View History

import type { NativeStackScreenProps } from '@react-navigation/native-stack';
2021-02-19 10:33:35 -07:00
import React, { useCallback, useEffect, useState } from 'react';
import { ImageRequireSource, Linking } from 'react-native';
2021-02-19 10:12:07 -07:00
2021-02-20 09:07:10 -07:00
import { StyleSheet, View, Text, Image } from 'react-native';
2021-02-19 10:12:07 -07:00
import { Camera, CameraPermissionStatus } from 'react-native-vision-camera';
2021-02-19 10:33:35 -07:00
import { CONTENT_SPACING, SAFE_AREA_PADDING } from './Constants';
import type { Routes } from './Routes';
2021-02-19 10:12:07 -07:00
2021-02-23 03:57:43 -07:00
// eslint-disable-next-line @typescript-eslint/no-var-requires
const BANNER_IMAGE = require('../../docs/static/img/11.png') as ImageRequireSource;
2021-02-23 03:57:43 -07:00
type Props = NativeStackScreenProps<Routes, 'PermissionsPage'>;
export function PermissionsPage({ navigation }: Props): React.ReactElement {
2021-02-20 09:07:10 -07:00
const [cameraPermissionStatus, setCameraPermissionStatus] = useState<CameraPermissionStatus>('not-determined');
const [microphonePermissionStatus, setMicrophonePermissionStatus] = useState<CameraPermissionStatus>('not-determined');
2021-02-19 10:12:07 -07:00
2021-02-19 10:33:35 -07:00
const requestMicrophonePermission = useCallback(async () => {
console.log('Requesting microphone permission...');
const permission = await Camera.requestMicrophonePermission();
console.log(`Microphone permission status: ${permission}`);
if (permission === 'denied') await Linking.openSettings();
2021-02-19 10:33:35 -07:00
setMicrophonePermissionStatus(permission);
}, []);
const requestCameraPermission = useCallback(async () => {
console.log('Requesting camera permission...');
const permission = await Camera.requestCameraPermission();
console.log(`Camera permission status: ${permission}`);
if (permission === 'denied') await Linking.openSettings();
2021-02-19 10:33:35 -07:00
setCameraPermissionStatus(permission);
}, []);
2021-02-19 10:12:07 -07:00
useEffect(() => {
if (cameraPermissionStatus === 'authorized' && microphonePermissionStatus === 'authorized') navigation.replace('CameraPage');
}, [cameraPermissionStatus, microphonePermissionStatus, navigation]);
2021-02-19 10:33:35 -07:00
2021-02-19 10:12:07 -07:00
return (
<View style={styles.container}>
2021-02-23 03:57:43 -07:00
<Image source={BANNER_IMAGE} style={styles.banner} />
2021-02-19 10:17:37 -07:00
<Text style={styles.welcome}>Welcome to{'\n'}Vision Camera.</Text>
2021-02-19 10:33:35 -07:00
<View style={styles.permissionsContainer}>
{cameraPermissionStatus !== 'authorized' && (
2021-02-20 09:07:10 -07:00
<Text style={styles.permissionText}>
Vision Camera needs <Text style={styles.bold}>Camera permission</Text>.{' '}
2021-02-20 09:07:10 -07:00
<Text style={styles.hyperlink} onPress={requestCameraPermission}>
Grant
</Text>
</Text>
2021-02-19 10:33:35 -07:00
)}
{microphonePermissionStatus !== 'authorized' && (
2021-02-20 09:07:10 -07:00
<Text style={styles.permissionText}>
Vision Camera needs <Text style={styles.bold}>Microphone permission</Text>.{' '}
2021-02-20 09:07:10 -07:00
<Text style={styles.hyperlink} onPress={requestMicrophonePermission}>
Grant
</Text>
</Text>
2021-02-19 10:33:35 -07:00
)}
</View>
2021-02-19 10:12:07 -07:00
</View>
);
}
2021-02-19 10:12:07 -07:00
const styles = StyleSheet.create({
2021-02-19 10:17:37 -07:00
welcome: {
fontSize: 38,
fontWeight: 'bold',
maxWidth: '80%',
},
banner: {
position: 'absolute',
opacity: 0.4,
bottom: 0,
left: 0,
},
2021-02-19 10:12:07 -07:00
container: {
flex: 1,
backgroundColor: 'white',
2021-02-20 09:07:10 -07:00
...SAFE_AREA_PADDING,
2021-02-19 10:12:07 -07:00
},
2021-02-19 10:33:35 -07:00
permissionsContainer: {
2021-02-20 09:07:10 -07:00
marginTop: CONTENT_SPACING * 2,
2021-02-19 10:33:35 -07:00
},
permissionText: {
fontSize: 17,
},
hyperlink: {
color: '#007aff',
fontWeight: 'bold',
},
bold: {
fontWeight: 'bold',
2021-02-19 10:12:07 -07:00
},
});