-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.native.js
280 lines (257 loc) · 8.83 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
/**
* The react native image/document pickers work for iOS/Android, but we want to wrap them both within AttachmentPicker
*/
import React, {Component} from 'react';
import {Alert, Linking, View} from 'react-native';
import RNImagePicker from 'react-native-image-picker';
import RNDocumentPicker from 'react-native-document-picker';
import basePropTypes from './AttachmentPickerPropTypes';
import styles from '../../styles/styles';
import Popover from '../Popover';
import MenuItem from '../MenuItem';
import {Camera, Gallery, Paperclip} from '../Icon/Expensicons';
import withWindowDimensions, {windowDimensionsPropTypes} from '../withWindowDimensions';
import withLocalize, {withLocalizePropTypes} from '../withLocalize';
import compose from '../../libs/compose';
const propTypes = {
...basePropTypes,
...windowDimensionsPropTypes,
...withLocalizePropTypes,
};
/**
* See https://github.com/react-native-community/react-native-image-picker/blob/master/docs/Reference.md#options
* for ImagePicker configuration options
*/
const imagePickerOptions = {
storageOptions: {
skipBackup: true,
},
};
/**
* See https://github.com/rnmods/react-native-document-picker#options for DocumentPicker configuration options
*/
const documentPickerOptions = {
type: [RNDocumentPicker.types.allFiles],
};
/**
* 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 {Object}
*/
function getDataForUpload(fileData) {
return {
name: fileData.fileName || fileData.name || 'chat_attachment',
type: fileData.type,
uri: fileData.uri,
};
}
/**
* 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: Camera,
text: this.props.translate('attachmentPicker.takePhoto'),
pickAttachment: () => this.showImagePicker(RNImagePicker.launchCamera),
},
{
icon: Gallery,
text: this.props.translate('attachmentPicker.chooseFromGallery'),
pickAttachment: () => this.showImagePicker(RNImagePicker.launchImageLibrary),
},
{
icon: Paperclip,
text: this.props.translate('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 {ImagePickerResponse|DocumentPickerResponse} attachment
*/
pickAttachment(attachment) {
if (attachment && !attachment.didCancel && !attachment.error) {
if (attachment.width === -1 || attachment.height === -1) {
this.showImageCorruptionAlert();
return;
}
const result = getDataForUpload(attachment);
this.completeAttachmentSelection(result);
}
}
/**
* 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(imagePickerOptions, (response) => {
if (response.error) {
switch (response.error) {
case 'Camera permissions not granted':
case 'Permissions weren\'t granted':
this.showPermissionsAlert();
break;
default:
this.showGeneralAlert(response.error);
break;
}
reject(new Error(`Error during attachment selection: ${response.error}`));
}
resolve(response);
});
});
}
/**
* 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)) {
this.showGeneralAlert(error.message);
throw error;
}
});
}
/**
* Triggers the `onPicked` callback with the selected attachment
*/
completeAttachmentSelection() {
if (this.state.result) {
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),
10,
);
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}>
{
this.menuItemData.map(item => (
<MenuItem
key={item.text}
icon={item.icon}
title={item.text}
onPress={() => this.selectItem(item)}
/>
))
}
</View>
</Popover>
{this.renderChildren()}
</>
);
}
}
AttachmentPicker.propTypes = propTypes;
AttachmentPicker.displayName = 'AttachmentPicker';
export default compose(
withWindowDimensions,
withLocalize,
)(AttachmentPicker);