import CameraRoll from '@react-native-community/cameraroll'; import {useBottomTabBarHeight} from '@react-navigation/bottom-tabs'; import {RouteProp} from '@react-navigation/core'; import {useFocusEffect} from '@react-navigation/native'; import {StackNavigationProp} from '@react-navigation/stack'; import React, {createRef, useCallback, useEffect, useState} from 'react'; import {Modal, StyleSheet, TouchableOpacity, View} from 'react-native'; import {CameraType, FlashMode, RNCamera} from 'react-native-camera'; import {AnimatedCircularProgress} from 'react-native-circular-progress'; import CloseIcon from '../../assets/ionicons/close-outline.svg'; import {FlashButton, FlipButton, GalleryIcon} from '../../components'; import {MAX_VIDEO_RECORDING_DURATION, TAGG_PURPLE} from '../../constants'; import {MainStackParams} from '../../routes'; import {HeaderHeight, SCREEN_HEIGHT, SCREEN_WIDTH} from '../../utils'; import {showGIFFailureAlert, takePicture, takeVideo} from '../../utils/camera'; type CameraScreenRouteProps = RouteProp; export type CameraScreenNavigationProps = StackNavigationProp< MainStackParams, 'CameraScreen' >; interface CameraScreenProps { route: CameraScreenRouteProps; navigation: CameraScreenNavigationProps; } const CameraScreen: React.FC = ({route, navigation}) => { const {screenType, selectedCategory} = route.params; const cameraRef = createRef(); const tabBarHeight = useBottomTabBarHeight(); const [cameraType, setCameraType] = useState('back'); const [flashMode, setFlashMode] = useState('off'); const [mostRecentPhoto, setMostRecentPhoto] = useState(''); const [isRecording, setIsRecording] = useState(false); useFocusEffect( useCallback(() => { navigation.dangerouslyGetParent()?.setOptions({ tabBarVisible: false, }); return () => setIsRecording(false); }, [navigation]), ); /* * Chooses the last picture from gallery to display as the gallery button icon */ useEffect(() => { CameraRoll.getPhotos({first: 1}) .then((lastPhoto) => { if (lastPhoto.edges.length > 0) { const image = lastPhoto.edges[0].node.image; setMostRecentPhoto(image.uri); } }) .catch((_err) => console.log('Unable to fetch preview photo for gallery'), ); }, []); const navigateToEditMedia = (uri: string) => { cameraRef.current?.resumePreview(); navigation.navigate('EditMedia', { screenType, media: { uri, isVideo: !( uri.endsWith('jpg') || uri.endsWith('JPG') || uri.endsWith('PNG') || uri.endsWith('png') || uri.endsWith('GIF') || uri.endsWith('gif') ), }, selectedCategory, }); }; const handleClose = () => { navigation.dangerouslyGetParent()?.setOptions({ tabBarVisible: true, }); navigation.goBack(); }; return ( { setCameraType(cameraType === 'front' ? 'back' : 'front'); }} /> { takeVideo(cameraRef, (vid) => navigateToEditMedia(vid.uri)); setIsRecording(true); }} onPressOut={async () => { const cancelRecording = async () => { if (await cameraRef.current?.isRecording()) { cameraRef.current?.stopRecording(); setIsRecording(false); } }; cancelRecording(); // tmp fix for when the animation glitches during the beginning of // recording causing onPressOut to not be detected. setTimeout(() => { cancelRecording(); }, 500); setTimeout(() => { cancelRecording(); }, 1000); setTimeout(() => { cancelRecording(); }, 1500); }} onPress={() => { takePicture(cameraRef, (pic) => navigateToEditMedia(pic.uri)); }}> {isRecording && ( )} { const filename = media.filename; if ( filename && (filename.endsWith('gif') || filename.endsWith('GIF')) ) { showGIFFailureAlert(() => navigateToEditMedia(media.path)); } else { navigateToEditMedia(media.path); } }} /> ); }; const styles = StyleSheet.create({ camera: { flex: 1, justifyContent: 'space-between', }, container: { flex: 1, flexDirection: 'column', backgroundColor: 'black', }, flashView: { width: SCREEN_WIDTH, height: SCREEN_HEIGHT, backgroundColor: '#fff', opacity: 0.5, }, captureButtonVideoContainer: { alignSelf: 'center', backgroundColor: 'transparent', borderRadius: 100, borderWidth: 15, borderColor: 'rgba(255,255,255,0.3)', width: 93, height: 93, flexDirection: 'row', alignItems: 'center', justifyContent: 'center', }, captureButtonContainer: { alignSelf: 'center', backgroundColor: 'transparent', borderRadius: 100, borderWidth: 4, borderColor: '#fff', width: 93, height: 93, flexDirection: 'row', alignItems: 'center', justifyContent: 'center', }, captureButton: { backgroundColor: '#fff', width: 68, height: 68, borderRadius: 74, }, closeButton: { position: 'absolute', top: 0, paddingTop: HeaderHeight, zIndex: 1, marginLeft: '5%', }, bottomContainer: { position: 'absolute', width: SCREEN_WIDTH, flexDirection: 'row', justifyContent: 'center', }, bottomRightContainer: { flexDirection: 'column', justifyContent: 'center', alignItems: 'center', width: (SCREEN_WIDTH - 100) / 2, }, }); export default CameraScreen;