import React, { useEffect, useRef, useState } from "react"; import { StyleSheet, View } from "react-native"; import { RNCamera, RecordResponse, TakePictureResponse, } from "react-native-camera"; import { useSelector } from "react-redux"; import { RootState, useAppDispatch } from "../../redux/store"; import { CameraModuleHandle } from "./cameraCommonInterface"; import { disableCaptureAction, lockNavigationToggle, } from "../../redux/UtilSlice"; import RNFS from "react-native-fs"; import { CameraRoll } from "@react-native-camera-roll/camera-roll"; import { setActionData } from "../../redux/AOPSlice"; import { Platform } from "react-native"; //! NOTE: https://github.com/react-native-camera/react-native-camera/issues/3423 //! use given link to fix propType issue for RNCamera const DefaultCamera: React.ForwardRefRenderFunction = ( _, ref ) => { const [videoData, setVideoData] = useState(); const camera = useRef(null); const dispatch = useAppDispatch(); const orientation = useSelector( (appState: RootState) => appState.Util.orientation ); const captureSettings = useSelector((appState: RootState) => appState.Util.captureSettings.filter( (setting) => (setting.platform === "all" || setting.platform === Platform.OS) && (setting.camera === "all" || setting.camera === "default") ) ); const zoom = useSelector((appState: RootState) => appState.Util.zoom); const steps = useSelector((state: RootState) => state.AOP.steps); const activeSlotIndex = useSelector( (state: RootState) => state.AOP.activeSlotIndex as number ); const activeStepIndex = useSelector( (state: RootState) => state.AOP.activeStepIndex as number ); const activeSlotItemIndex = useSelector( (state: RootState) => state.AOP.activeSlotItemIndex as number ); const stepName = steps.length > 0 ? steps[activeStepIndex].title : ""; const slotName = steps.length > 0 ? steps[activeStepIndex].slots[activeSlotIndex].title : ""; const slotItemName = steps.length > 0 && steps[activeStepIndex].slots[activeSlotIndex].slotItems.length > 0 ? steps[activeStepIndex].slots[activeSlotIndex].slotItems[ activeSlotItemIndex ].title : ""; const productId = useSelector( (appState: RootState) => appState.App.productID ); React.useImperativeHandle(ref, () => ({ async takePhoto() { try { dispatch(lockNavigationToggle(true)); // locks navigation till photo is saved const photo = await camera.current?.takePictureAsync({ imageType: "jpeg", quality: 1, orientation: orientation === "PORTRAIT" ? "portrait" : orientation === "LANDSCAPE-LEFT" ? "landscapeRight" : "landscapeLeft", }); if (photo) { const { uri: path } = photo; let fileSavePath = ""; if (slotItemName.length > 0) { fileSavePath = `${RNFS.DocumentDirectoryPath}/${ productId + "_" + stepName + "_" + slotName + "_" + slotItemName }.jpeg`; } else { fileSavePath = `${RNFS.DocumentDirectoryPath}/${ productId + "_" + stepName + "_" + slotName }.jpeg`; } const flag = await RNFS.exists(fileSavePath); if (flag) { await RNFS.unlink(fileSavePath); } await RNFS.copyFile(path, fileSavePath); const updatedPhoto = { ...photo, path: fileSavePath, }; await CameraRoll.save(fileSavePath, { type: "photo", album: "capture-sdk", }); dispatch(setActionData(updatedPhoto)); dispatch(lockNavigationToggle(false)); // unlocks navigation after photo is saved dispatch(disableCaptureAction(false)); } } catch (error: any) { console.log(error.message); } }, async startVideo() { dispatch(lockNavigationToggle(true)); // locks navigation till video is being recorded const video = await camera.current?.recordAsync({ mute: true, fps: captureSettings.find((setting) => setting.settingsKey === "fps") ?.value as number, orientation: orientation === "PORTRAIT" ? "portrait" : "landscapeRight", }); setVideoData(video); }, async stopVideo() { camera.current?.stopRecording(); }, })); const saveVideoToFile = async (videoData: RecordResponse) => { let fileSavePath = ""; if (slotItemName.length > 0) { fileSavePath = `${RNFS.DocumentDirectoryPath}/${ productId + "_" + stepName + "_" + slotName + "_" + slotItemName }.mp4`; } else { fileSavePath = `${RNFS.DocumentDirectoryPath}/${ productId + "_" + stepName + "_" + slotName }.mp4`; } const flag = await RNFS.exists(fileSavePath); if (flag) { await RNFS.unlink(fileSavePath); } await RNFS.copyFile(videoData.uri, fileSavePath); const updatedVideo = { ...videoData, path: fileSavePath, }; dispatch(setActionData(updatedVideo)); dispatch(lockNavigationToggle(false)); // unlocks navigation after video is saved dispatch(disableCaptureAction(false)); }; useEffect(() => { if (videoData) { saveVideoToFile(videoData); } }, [videoData]); return ( setting.settingsKey === "torch") ?.value as boolean) ? "torch" : "off" } type={"back"} useNativeZoom focusable autoFocus="on" useCamera2Api // exposure={cameraSettings.exposure} whiteBalance={ Platform.OS === "ios" ? { redGainOffset: 0, greenGainOffset: 0, blueGainOffset: 0, tint: captureSettings.find( (setting) => setting.settingsKey === "tint" )?.value as number, temperature: captureSettings.find( (setting) => setting.settingsKey === "temperature" )?.value as number, } : undefined } captureAudio={false} androidCameraPermissionOptions={{ title: "Permission to use camera", message: "We need your permission to use your camera", buttonPositive: "Ok", buttonNegative: "Cancel", }} /> ); }; const styles = StyleSheet.create({ root: { width: "100%", height: "100%", position: "absolute", zIndex: 0, justifyContent: "center", }, camera: { width: "100%", height: "100%", }, }); export default React.memo(React.forwardRef(DefaultCamera));