aboutsummaryrefslogtreecommitdiff
path: root/src/utils/camera.ts
blob: c997e8b84c5069f7056782264d5d2d7a349b1f79 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import CameraRoll from '@react-native-community/cameraroll';
import {RefObject} from 'react';
import {Alert} from 'react-native';
import {
  RecordOptions,
  RecordResponse,
  RNCamera,
  TakePictureOptions,
  TakePictureResponse,
} from 'react-native-camera';
import ImagePicker, {ImageOrVideo, 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) {
    const options: TakePictureOptions = {
      forceUpOrientation: true,
      orientation: 'portrait',
      writeExif: false,
      pauseAfterCapture: true,
    };
    cameraRef.current?.takePictureAsync(options).then((pic) => {
      callback(pic);
    });
  }
};

export const takeVideo = (
  cameraRef: RefObject<RNCamera>,
  callback: (vid: RecordResponse) => void,
) => {
  if (cameraRef !== null) {
    const options: RecordOptions = {
      orientation: 'portrait',
      maxDuration: 10,
      quality: '1080p',
    };
    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!'))
    .catch((_err) => Alert.alert('Failed to save to device!'));
};

export const navigateToImagePicker = (
  callback: (media: ImageOrVideo) => void,
) => {
  ImagePicker.openPicker({
    smartAlbums: [
      'Favorites',
      'RecentlyAdded',
      'SelfPortraits',
      'Screenshots',
      'UserLibrary',
    ],
    mediaType: 'any',
  })
    .then((media) => {
      callback(media);
    })
    .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.',
        ),
    },
  );