From 80f52fa900817d614680a60f0c35592cf48310b0 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Fri, 2 Jul 2021 15:59:07 -0400 Subject: Add long press to take video --- src/screens/moments/CameraScreen.tsx | 39 +++++++++++++++++++++++------------- src/utils/camera.ts | 17 ++++++++++++++++ 2 files changed, 42 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/screens/moments/CameraScreen.tsx b/src/screens/moments/CameraScreen.tsx index 37b37264..0f2e3b5d 100644 --- a/src/screens/moments/CameraScreen.tsx +++ b/src/screens/moments/CameraScreen.tsx @@ -16,7 +16,7 @@ import { } from '../../components'; import {MainStackParams} from '../../routes'; import {HeaderHeight, normalize, SCREEN_WIDTH} from '../../utils'; -import {showGIFFailureAlert, takePicture} from '../../utils/camera'; +import {showGIFFailureAlert, takePicture, takeVideo} from '../../utils/camera'; type CameraScreenRouteProps = RouteProp; export type CameraScreenNavigationProps = StackNavigationProp< @@ -33,7 +33,7 @@ const CameraScreen: React.FC = ({route, navigation}) => { const tabBarHeight = useBottomTabBarHeight(); const [cameraType, setCameraType] = useState('front'); const [flashMode, setFlashMode] = useState('off'); - const [capturedImage, setCapturedImage] = useState(''); + const [mediaFromGallery, setMediaFromGallery] = useState(''); const [mostRecentPhoto, setMostRecentPhoto] = useState(''); const [showSaveButton, setShowSaveButton] = useState(false); @@ -64,7 +64,7 @@ const CameraScreen: React.FC = ({route, navigation}) => { .catch((_err) => console.log('Unable to fetch preview photo for gallery'), ); - }, [capturedImage]); + }, [mediaFromGallery]); const navigateToCropper = (uri: string) => { navigation.navigate('ZoomInCropper', { @@ -77,13 +77,13 @@ const CameraScreen: React.FC = ({route, navigation}) => { }); }; - const navigateToCaptionScreen = () => { + const navigateToCaptionScreen = (isVideo: boolean, uri: string) => { navigation.navigate('CaptionScreen', { screenType, title, media: { - uri: capturedImage, - isVideo: false, + uri, + isVideo, }, }); }; @@ -96,7 +96,7 @@ const CameraScreen: React.FC = ({route, navigation}) => { if (showSaveButton) { cameraRef.current?.resumePreview(); setShowSaveButton(false); - setCapturedImage(''); + setMediaFromGallery(''); } else { navigation.goBack(); } @@ -116,24 +116,35 @@ const CameraScreen: React.FC = ({route, navigation}) => { /> {showSaveButton ? ( - + ) : ( )} + onLongPress={() => { + takeVideo(cameraRef, (vid) => { + navigateToCaptionScreen(true, vid.uri); + }); + }} + onPressOut={async () => { + if (await cameraRef.current?.isRecording()) { + cameraRef.current?.stopRecording(); + } else { + } + }} + onPress={() => { takePicture(cameraRef, (pic) => { setShowSaveButton(true); - setCapturedImage(pic.uri); - }) - } + setMediaFromGallery(pic.uri); + }); + }} style={styles.captureButtonContainer}> - {capturedImage ? ( + {mediaFromGallery ? ( navigateToCaptionScreen(false, mediaFromGallery)} title={'Next'} buttonStyle={'large'} buttonColor={'blue'} diff --git a/src/utils/camera.ts b/src/utils/camera.ts index 3937129a..7fa18b7c 100644 --- a/src/utils/camera.ts +++ b/src/utils/camera.ts @@ -2,6 +2,8 @@ import CameraRoll from '@react-native-community/cameraroll'; import {RefObject} from 'react'; import {Alert} from 'react-native'; import { + RecordOptions, + RecordResponse, RNCamera, TakePictureOptions, TakePictureResponse, @@ -29,6 +31,21 @@ export const takePicture = ( } }; +export const takeVideo = ( + cameraRef: RefObject, + callback: (vid: RecordResponse) => void, +) => { + if (cameraRef !== null) { + const options: RecordOptions = { + orientation: 'portrait', + maxDuration: 10, + }; + cameraRef.current?.recordAsync(options).then((vid) => { + callback(vid); + }); + } +}; + export const saveImageToGallery = (capturedImageURI: string) => { CameraRoll.save(capturedImageURI, {album: 'Recents', type: 'photo'}) .then((_res) => Alert.alert('Saved to device!')) -- cgit v1.2.3-70-g09d2