-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.native.js
340 lines (306 loc) · 10.6 KB
/
index.native.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* The react native image/document pickers work for iOS/Android, but we want to wrap them both within AttachmentPicker
*/
import _ from 'underscore';
import React, {Component} from 'react';
import {Alert, Linking, View} from 'react-native';
import {launchImageLibrary} from 'react-native-image-picker';
import RNDocumentPicker from 'react-native-document-picker';
import RNFetchBlob from 'react-native-blob-util';
import {propTypes as basePropTypes, defaultProps} from './attachmentPickerPropTypes';
import styles from '../../styles/styles';
import Popover from '../Popover';
import MenuItem from '../MenuItem';
import * as Expensicons from '../Icon/Expensicons';
import withWindowDimensions, {windowDimensionsPropTypes} from '../withWindowDimensions';
import withLocalize, {withLocalizePropTypes} from '../withLocalize';
import compose from '../../libs/compose';
import launchCamera from './launchCamera';
import CONST from '../../CONST';
import * as FileUtils from '../../libs/fileDownload/FileUtils';
const propTypes = {
...basePropTypes,
...windowDimensionsPropTypes,
...withLocalizePropTypes,
};
/**
* See https://github.com/react-native-image-picker/react-native-image-picker/#options
* for ImagePicker configuration options
*/
const imagePickerOptions = {
includeBase64: false,
saveToPhotos: false,
selectionLimit: 1,
includeExtra: false,
};
/**
* Return imagePickerOptions based on the type
* @param {String} type
* @returns {Object}
*/
function getImagePickerOptions(type) {
// mediaType property is one of the ImagePicker configuration to restrict types'
const mediaType = type === CONST.ATTACHMENT_PICKER_TYPE.IMAGE ? 'photo' : 'mixed';
return {
mediaType,
...imagePickerOptions,
};
}
/**
* See https://github.com/rnmods/react-native-document-picker#options for DocumentPicker configuration options
*/
const documentPickerOptions = {
type: [RNDocumentPicker.types.allFiles],
copyTo: 'cachesDirectory',
};
/**
* The data returned from `show` is different on web and mobile, so use this function to ensure the data we
* send to the xhr will be handled properly.
*
* @param {Object} fileData
* @return {Promise}
*/
function getDataForUpload(fileData) {
const fileName = fileData.fileName || fileData.name || 'chat_attachment';
const fileResult = {
name: FileUtils.cleanFileName(fileName),
type: fileData.type,
width: fileData.width,
height: fileData.height,
uri: fileData.fileCopyUri || fileData.uri,
size: fileData.fileSize || fileData.size,
};
if (fileResult.size) {
return Promise.resolve(fileResult);
}
return RNFetchBlob.fs.stat(fileData.uri.replace('file://', '')).then((stats) => {
fileResult.size = stats.size;
return fileResult;
});
}
/**
* This component renders a function as a child and
* returns a "show attachment picker" method that takes
* a callback. This is the ios/android implementation
* opening a modal with attachment options
*/
class AttachmentPicker extends Component {
constructor(...args) {
super(...args);
this.state = {
isVisible: false,
};
this.menuItemData = [
{
icon: Expensicons.Camera,
textTranslationKey: 'attachmentPicker.takePhoto',
pickAttachment: () => this.showImagePicker(launchCamera),
},
{
icon: Expensicons.Gallery,
textTranslationKey: 'attachmentPicker.chooseFromGallery',
pickAttachment: () => this.showImagePicker(launchImageLibrary),
},
];
// When selecting an image on a native device, it would be redundant to have a second option for choosing a document,
// so it is excluded in this case.
if (this.props.type !== CONST.ATTACHMENT_PICKER_TYPE.IMAGE) {
this.menuItemData.push(
{
icon: Expensicons.Paperclip,
textTranslationKey: 'attachmentPicker.chooseDocument',
pickAttachment: () => this.showDocumentPicker(),
},
);
}
this.close = this.close.bind(this);
this.pickAttachment = this.pickAttachment.bind(this);
}
/**
* Handles the image/document picker result and
* sends the selected attachment to the caller (parent component)
*
* @param {Array<ImagePickerResponse|DocumentPickerResponse>} attachments
* @returns {Promise}
*/
pickAttachment(attachments = []) {
if (attachments.length === 0) {
return;
}
const fileData = _.first(attachments);
if (fileData.width === -1 || fileData.height === -1) {
this.showImageCorruptionAlert();
return;
}
return getDataForUpload(fileData).then((result) => {
this.completeAttachmentSelection(result);
}).catch((error) => {
this.showGeneralAlert(error.message);
throw error;
});
}
/**
* Inform the users when they need to grant camera access and guide them to settings
*/
showPermissionsAlert() {
Alert.alert(
this.props.translate('attachmentPicker.cameraPermissionRequired'),
this.props.translate('attachmentPicker.expensifyDoesntHaveAccessToCamera'),
[
{
text: this.props.translate('common.cancel'),
style: 'cancel',
},
{
text: this.props.translate('common.settings'),
onPress: () => Linking.openSettings(),
},
],
{cancelable: false},
);
}
/**
* Common image picker handling
*
* @param {function} imagePickerFunc - RNImagePicker.launchCamera or RNImagePicker.launchImageLibrary
* @returns {Promise<ImagePickerResponse>}
*/
showImagePicker(imagePickerFunc) {
return new Promise((resolve, reject) => {
imagePickerFunc(getImagePickerOptions(this.props.type), (response) => {
if (response.didCancel) {
// When the user cancelled resolve with no attachment
return resolve();
}
if (response.errorCode) {
switch (response.errorCode) {
case 'permission':
this.showPermissionsAlert();
break;
default:
this.showGeneralAlert();
break;
}
return reject(new Error(`Error during attachment selection: ${response.errorMessage}`));
}
return resolve(response.assets);
});
});
}
/**
* A generic handling when we don't know the exact reason for an error
*
*/
showGeneralAlert() {
Alert.alert(
this.props.translate('attachmentPicker.attachmentError'),
this.props.translate('attachmentPicker.errorWhileSelectingAttachment'),
);
}
/**
* An attachment error dialog when user selected malformed images
*/
showImageCorruptionAlert() {
Alert.alert(
this.props.translate('attachmentPicker.attachmentError'),
this.props.translate('attachmentPicker.errorWhileSelectingCorruptedImage'),
);
}
/**
* Launch the DocumentPicker. Results are in the same format as ImagePicker
*
* @returns {Promise<DocumentPickerResponse[]>}
*/
showDocumentPicker() {
return RNDocumentPicker.pick(documentPickerOptions).catch((error) => {
if (RNDocumentPicker.isCancel(error)) {
return;
}
this.showGeneralAlert(error.message);
throw error;
});
}
/**
* Triggers the `onPicked` callback with the selected attachment
*/
completeAttachmentSelection() {
if (!this.state.result) {
return;
}
this.state.onPicked(this.state.result);
}
/**
* Opens the attachment modal
*
* @param {function} onPicked A callback that will be called with the selected attachment
*/
open(onPicked) {
this.completeAttachmentSelection = onPicked;
this.setState({isVisible: true});
}
/**
* Closes the attachment modal
*/
close() {
this.setState({isVisible: false});
}
/**
* Setup native attachment selection to start after this popover closes
*
* @param {{pickAttachment: function}} item - an item from this.menuItemData
*/
selectItem(item) {
/* setTimeout delays execution to the frame after the modal closes
* without this on iOS closing the modal closes the gallery/camera as well */
this.onModalHide = () => setTimeout(
() => item.pickAttachment()
.then(this.pickAttachment)
.catch(console.error)
.finally(() => delete this.onModalHide),
200,
);
this.close();
}
/**
* Call the `children` renderProp with the interface defined in propTypes
*
* @returns {React.ReactNode}
*/
renderChildren() {
return this.props.children({
openPicker: ({onPicked}) => this.open(onPicked),
});
}
render() {
return (
<>
<Popover
onClose={this.close}
isVisible={this.state.isVisible}
anchorPosition={styles.createMenuPosition}
onModalHide={this.onModalHide}
>
<View style={this.props.isSmallScreenWidth ? {} : styles.createMenuContainer}>
{
_.map(this.menuItemData, item => (
<MenuItem
key={item.textTranslationKey}
icon={item.icon}
title={this.props.translate(item.textTranslationKey)}
onPress={() => this.selectItem(item)}
/>
))
}
</View>
</Popover>
{this.renderChildren()}
</>
);
}
}
AttachmentPicker.propTypes = propTypes;
AttachmentPicker.defaultProps = defaultProps;
export default compose(
withWindowDimensions,
withLocalize,
)(AttachmentPicker);