aboutsummaryrefslogtreecommitdiff
path: root/src/utils/camera.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/camera.ts')
-rw-r--r--src/utils/camera.ts81
1 files changed, 57 insertions, 24 deletions
diff --git a/src/utils/camera.ts b/src/utils/camera.ts
index 73461ad7..3937129a 100644
--- a/src/utils/camera.ts
+++ b/src/utils/camera.ts
@@ -1,43 +1,41 @@
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,
+ 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 shoe the preview of the picture taken
+ * Captures a photo and pauses to show the preview of the picture taken
*/
export const takePicture = (
cameraRef: RefObject<RNCamera>,
- setShowSaveButton: Dispatch<SetStateAction<boolean>>,
- setCapturedImage: Dispatch<SetStateAction<string>>,
+ callback: (pic: TakePictureResponse) => void,
) => {
if (cameraRef !== null) {
cameraRef.current?.pausePreview();
- const options = {
+ const options: TakePictureOptions = {
forceUpOrientation: true,
+ orientation: 'portrait',
writeExif: false,
};
- cameraRef.current?.takePictureAsync(options).then((response) => {
- setShowSaveButton(true);
- setCapturedImage(response.uri);
+ cameraRef.current?.takePictureAsync(options).then((pic) => {
+ callback(pic);
});
}
};
-export const downloadImage = (capturedImageURI: string) => {
+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 = (
- navigation: any,
- screenType: ScreenType,
- title: string,
-) => {
+export const navigateToImagePicker = (callback: (pic: Image) => void) => {
ImagePicker.openPicker({
smartAlbums: [
'Favorites',
@@ -48,13 +46,23 @@ export const navigateToImagePicker = (
],
mediaType: 'photo',
})
- .then((picture) => {
- if ('path' in picture) {
- navigation.navigate('ZoomInCropper', {
- screenType,
- title,
- image: picture,
- });
+ .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) => {
@@ -63,3 +71,28 @@ export const navigateToImagePicker = (
}
});
};
+
+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.',
+ ),
+ },
+ );