diff options
author | Ivan Chen <ivan@tagg.id> | 2021-07-01 16:52:19 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-01 16:52:19 -0400 |
commit | 202c94b6a5f96db228965a64a33e444511eea1cf (patch) | |
tree | 1d1ddbb28dd0276c7e4e0c7e6cb1de13245b2220 /src/utils/camera.ts | |
parent | de390ea6b0f3bfd851029cf038aacd11f269a823 (diff) | |
parent | c08a67f5cc1d622e91828d0557c6716a40ee5bad (diff) |
Merge pull request #482 from IvanIFChen/tma953-camera-screen-merged-with-master
[TMA-953] Camera Screen from Master to PoC Video Branch
Diffstat (limited to 'src/utils/camera.ts')
-rw-r--r-- | src/utils/camera.ts | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/utils/camera.ts b/src/utils/camera.ts new file mode 100644 index 00000000..3937129a --- /dev/null +++ b/src/utils/camera.ts @@ -0,0 +1,98 @@ +import CameraRoll from '@react-native-community/cameraroll'; +import {RefObject} from 'react'; +import {Alert} from 'react-native'; +import { + RNCamera, + TakePictureOptions, + TakePictureResponse, +} from 'react-native-camera'; +import ImagePicker, {Image, Video} from 'react-native-image-crop-picker'; +import {ERROR_UPLOAD} from '../constants/strings'; + +/* + * Captures a photo and pauses to show the preview of the picture taken + */ +export const takePicture = ( + cameraRef: RefObject<RNCamera>, + callback: (pic: TakePictureResponse) => void, +) => { + if (cameraRef !== null) { + cameraRef.current?.pausePreview(); + const options: TakePictureOptions = { + forceUpOrientation: true, + orientation: 'portrait', + writeExif: false, + }; + cameraRef.current?.takePictureAsync(options).then((pic) => { + callback(pic); + }); + } +}; + +export const saveImageToGallery = (capturedImageURI: string) => { + CameraRoll.save(capturedImageURI, {album: 'Recents', type: 'photo'}) + .then((_res) => Alert.alert('Saved to device!')) + .catch((_err) => Alert.alert('Failed to save to device!')); +}; + +export const navigateToImagePicker = (callback: (pic: Image) => void) => { + ImagePicker.openPicker({ + smartAlbums: [ + 'Favorites', + 'RecentlyAdded', + 'SelfPortraits', + 'Screenshots', + 'UserLibrary', + ], + mediaType: 'photo', + }) + .then((pic) => { + callback(pic); + }) + .catch((err) => { + if (err.code && err.code !== 'E_PICKER_CANCELLED') { + Alert.alert(ERROR_UPLOAD); + } + }); +}; + +export const navigateToVideoPicker = (callback: (vid: Video) => void) => { + ImagePicker.openPicker({ + mediaType: 'video', + }) + .then(async (vid) => { + if (vid.path) { + callback(vid); + } + }) + .catch((err) => { + if (err.code && err.code !== 'E_PICKER_CANCELLED') { + Alert.alert(ERROR_UPLOAD); + } + }); +}; + +export const showGIFFailureAlert = (onSuccess: () => void) => + Alert.alert( + 'Warning', + 'The app currently cannot handle GIFs, and will only save a static image.', + [ + { + text: 'Cancel', + onPress: () => {}, + style: 'cancel', + }, + { + text: 'Post', + onPress: onSuccess, + style: 'default', + }, + ], + { + cancelable: true, + onDismiss: () => + Alert.alert( + 'This alert was dismissed by tapping outside of the alert dialog.', + ), + }, + ); |