App.tsx 1.31 KB
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * Generated with the TypeScript template
 * https://github.com/react-native-community/react-native-template-typescript
 *
 * @format
 */

import {NativeBaseProvider, Text} from 'native-base';
import React, {useLayoutEffect} from 'react';
import {StyleSheet} from 'react-native';
import {Camera, useCameraDevices} from 'react-native-vision-camera';

const App = () => {
  const devices = useCameraDevices();
  const device = devices.front;

  useLayoutEffect(() => {
    (async () => {
      const [cameraPermission, micPermission] = await Promise.all([
        Camera.getCameraPermissionStatus(),
        Camera.getMicrophonePermissionStatus(),
      ]);

      const requests: (() => Promise<unknown>)[] = [];
      if (cameraPermission !== 'authorized') {
        requests.push(Camera.requestCameraPermission);
      }
      if (micPermission !== 'authorized') {
        requests.push(Camera.requestMicrophonePermission);
      }
      await Promise.all(requests.map(request => request()));
    })();
  }, []);

  return (
    <NativeBaseProvider>
      {device ? (
        <Camera isActive device={device} style={StyleSheet.absoluteFill} />
      ) : (
        <Text>Loading camera..</Text>
      )}
    </NativeBaseProvider>
  );
};

export default App;