From 43569411b854d724400f1982b26a2e67b450c5bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hampus=20Sj=C3=B6berg?= Date: Tue, 25 Jun 2024 17:55:19 +0400 Subject: [PATCH] Move back to using react-native-vision-camera --- package.json | 1 + src/components/Camera.tsx | 70 ++++++++++++++++----------------- src/components/FooterNav.tsx | 4 +- src/windows/Send/SendCamera.tsx | 14 +++---- yarn.lock | 5 +++ 5 files changed, 47 insertions(+), 47 deletions(-) diff --git a/package.json b/package.json index 3fa4ac354..62a1e34ff 100644 --- a/package.json +++ b/package.json @@ -145,6 +145,7 @@ "react-native-tor": "^0.1.8", "react-native-unimodules": "^0.14.10", "react-native-vector-icons": "9.2.0", + "react-native-vision-camera": "4.3.2", "react-native-web": "0.19.12", "react-native-web-linear-gradient": "^1.1.2", "react-native-web-maps": "^0.3.0", diff --git a/src/components/Camera.tsx b/src/components/Camera.tsx index b56148313..6710ca8ea 100644 --- a/src/components/Camera.tsx +++ b/src/components/Camera.tsx @@ -1,61 +1,57 @@ -import React, { ReactNode, useState, useEffect } from "react"; +import React, { ReactNode, useEffect } from "react"; import { StyleProp, ViewStyle, InteractionManager, StyleSheet } from "react-native"; -import { Camera, CameraType } from "react-native-camera-kit"; +import { + Camera, + useCodeScanner, + useCameraDevice, + useCameraPermission, +} from "react-native-vision-camera"; import Container from "./Container"; -import { PLATFORM } from "../utils/constants"; - -let ReactNativePermissions: any; -if (PLATFORM !== "macos") { - ReactNativePermissions = require("react-native-permissions"); -} export interface ICamera { active?: boolean; children?: ReactNode | JSX.Element; - cameraType?: keyof CameraType; + cameraType?: keyof CameraType; // TODO(hsjoberg) onRead?: (text: string) => void; onNotAuthorized?: () => void; // TODO(hsjoberg): style?: StyleProp; } -export default function CameraComponent({ - cameraType, - children, - onNotAuthorized, - onRead, - style, - active, -}: ICamera) { - const [start, setStart] = useState(false); +export default function CameraComponent({ children, onNotAuthorized, onRead, active }: ICamera) { + const device = useCameraDevice("back"); + const { hasPermission, requestPermission } = useCameraPermission(); + const codeScanner = useCodeScanner({ + codeTypes: ["qr"], + onCodeScanned: (codes) => { + if (codes.length >= 0) { + onRead?.(codes[0].value ?? ""); + } + }, + }); active = active ?? true; useEffect(() => { - InteractionManager.runAfterInteractions(async () => { - const permission = - PLATFORM === "ios" - ? ReactNativePermissions.PERMISSIONS.IOS.CAMERA - : ReactNativePermissions.PERMISSIONS.ANDROID.CAMERA; - const result = await ReactNativePermissions.request(permission); - - if (result === "granted" || result === "limited") { - console.log("Camera permission not granted"); - setStart(true); - } else { - console.log("Camera permission granted"); + (async () => { + if (hasPermission === false) { + console.log("Does not have camera permission"); + if (await !requestPermission()) { + // TODO fix await + onNotAuthorized?.(); + } } - }); - }, []); + })(); + }, [requestPermission, hasPermission]); - if (!start || !active) { + if (!active || !hasPermission || !device) { return {children ?? <>}; } return ( <> onRead?.(event.nativeEvent.codeStringValue)} + style={StyleSheet.absoluteFill} + codeScanner={codeScanner} + device={device} + isActive={active} /> {children} diff --git a/src/components/FooterNav.tsx b/src/components/FooterNav.tsx index d559659fc..fde1cd7fb 100644 --- a/src/components/FooterNav.tsx +++ b/src/components/FooterNav.tsx @@ -20,9 +20,7 @@ export default function FooterNav() {