aboutsummaryrefslogtreecommitdiff
path: root/src/utils/camera.ts
diff options
context:
space:
mode:
authorIvan Chen <ivan@tagg.id>2021-06-30 14:27:25 -0400
committerIvan Chen <ivan@tagg.id>2021-06-30 14:27:25 -0400
commit3b7297189633cda8b886fa06f4b9d4787b6aa7c7 (patch)
tree14e88a1559480f4546a26216e2e35b94567bd674 /src/utils/camera.ts
parent2f3244dfa11cc23b804930ad448222bbff4f022a (diff)
Clean up code, Remove tab bar logic to use modal style instead
Diffstat (limited to 'src/utils/camera.ts')
-rw-r--r--src/utils/camera.ts54
1 files changed, 33 insertions, 21 deletions
diff --git a/src/utils/camera.ts b/src/utils/camera.ts
index 73461ad7..1ee5dbf4 100644
--- a/src/utils/camera.ts
+++ b/src/utils/camera.ts
@@ -1,9 +1,8 @@
import CameraRoll from '@react-native-community/cameraroll';
-import {Dispatch, RefObject, SetStateAction} from 'react';
+import {RefObject} from 'react';
import {Alert} from 'react-native';
-import {RNCamera} from 'react-native-camera';
-import ImagePicker from 'react-native-image-crop-picker';
-import {ScreenType} from 'src/types';
+import {RNCamera, TakePictureResponse} from 'react-native-camera';
+import ImagePicker, {Image, Video} from 'react-native-image-crop-picker';
import {ERROR_UPLOAD} from '../constants/strings';
/*
@@ -11,8 +10,7 @@ import {ERROR_UPLOAD} from '../constants/strings';
*/
export const takePicture = (
cameraRef: RefObject<RNCamera>,
- setShowSaveButton: Dispatch<SetStateAction<boolean>>,
- setCapturedImage: Dispatch<SetStateAction<string>>,
+ callback: (pic: TakePictureResponse) => void,
) => {
if (cameraRef !== null) {
cameraRef.current?.pausePreview();
@@ -20,9 +18,8 @@ export const takePicture = (
forceUpOrientation: true,
writeExif: false,
};
- cameraRef.current?.takePictureAsync(options).then((response) => {
- setShowSaveButton(true);
- setCapturedImage(response.uri);
+ cameraRef.current?.takePictureAsync(options).then((pic) => {
+ callback(pic);
});
}
};
@@ -33,11 +30,7 @@ export const downloadImage = (capturedImageURI: string) => {
.catch((_err) => Alert.alert('Failed to save to device!'));
};
-export const navigateToImagePicker = (
- navigation: any,
- screenType: ScreenType,
- title: string,
-) => {
+export const navigateToImagePicker = (callback: (pic: Image) => void) => {
ImagePicker.openPicker({
smartAlbums: [
'Favorites',
@@ -48,13 +41,32 @@ export const navigateToImagePicker = (
],
mediaType: 'photo',
})
- .then((picture) => {
- if ('path' in picture) {
- navigation.navigate('ZoomInCropper', {
- screenType,
- title,
- image: picture,
- });
+ .then((pic) => {
+ if (pic.path && pic.filename) {
+ callback(pic);
+ }
+ })
+ .catch((err) => {
+ if (err.code && err.code !== 'E_PICKER_CANCELLED') {
+ Alert.alert(ERROR_UPLOAD);
+ }
+ });
+};
+
+/**
+ * This function opens the ImagePicker, only lets you select video files,
+ * formats the file extension, then makes a call to the server to get the presigned URL,
+ * after which it makes a POST request to the returned URL to upload the file directly to S3.
+ * params: none
+ * @returns: none
+ */
+export const navigateToVideoPicker = (callback: (vid: Video) => void) => {
+ ImagePicker.openPicker({
+ mediaType: 'video',
+ })
+ .then(async (vid) => {
+ if (vid.path) {
+ callback(vid);
}
})
.catch((err) => {