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

120 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-02-19 10:33:35 -07:00
import React, { useCallback, useEffect, useState } from 'react';
2021-02-23 03:57:43 -07:00
import type { ImageRequireSource } 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:33:35 -07:00
import { Navigation, NavigationFunctionComponent } from 'react-native-navigation';
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';
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
2021-02-19 10:33:35 -07:00
export const Splash: NavigationFunctionComponent = ({ componentId }) => {
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}`);
setMicrophonePermissionStatus(permission);
}, []);
const requestCameraPermission = useCallback(async () => {
console.log('Requesting camera permission...');
const permission = await Camera.requestCameraPermission();
console.log(`Camera permission status: ${permission}`);
setCameraPermissionStatus(permission);
}, []);
2021-02-19 10:12:07 -07:00
useEffect(() => {
2021-02-23 03:57:43 -07:00
const checkPermissions = async (): Promise<void> => {
2021-02-20 09:07:10 -07:00
console.log('Checking Permission status...');
const [cameraPermission, microphonePermission] = await Promise.all([
2021-02-19 10:12:07 -07:00
Camera.getCameraPermissionStatus(),
Camera.getMicrophonePermissionStatus(),
]);
2021-02-20 09:07:10 -07:00
console.log(`Check: CameraPermission: ${cameraPermission} | MicrophonePermission: ${microphonePermission}`);
2021-02-19 10:12:07 -07:00
setCameraPermissionStatus(cameraPermission);
setMicrophonePermissionStatus(microphonePermission);
};
checkPermissions();
}, []);
2021-02-19 10:33:35 -07:00
useEffect(() => {
if (cameraPermissionStatus === 'authorized' && microphonePermissionStatus === 'authorized') {
2021-02-19 10:53:08 -07:00
Navigation.setRoot({
root: {
stack: {
children: [
{
component: {
2021-02-20 09:07:10 -07:00
name: 'Home',
},
},
],
},
},
});
2021-02-19 10:33:35 -07:00
}
2021-02-19 10:53:08 -07:00
}, [cameraPermissionStatus, microphonePermissionStatus, componentId]);
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>.
<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>.
<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-20 09:07:10 -07:00
};
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
},
});