aboutsummaryrefslogtreecommitdiff
path: root/src/utils/camera.ts
blob: 1ee5dbf4e6799eae0641fc90dcce0ae56133c41f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import CameraRoll from '@react-native-community/cameraroll';
import {RefObject} from 'react';
import {Alert} from 'react-native';
import {RNCamera, 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
 */
export const takePicture = (
  cameraRef: RefObject<RNCamera>,
  callback: (pic: TakePictureResponse) => void,
) => {
  if (cameraRef !== null) {
    cameraRef.current?.pausePreview();
    const options = {
      forceUpOrientation: true,
      writeExif: false,
    };
    cameraRef.current?.takePictureAsync(options).then((pic) => {
      callback(pic);
    });
  }
};

export const downloadImage = (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) => {
      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) => {
      if (err.code && err.code !== 'E_PICKER_CANCELLED') {
        Alert.alert(ERROR_UPLOAD);
      }
    });
};