-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttachmentPicker.js
130 lines (121 loc) · 3.34 KB
/
AttachmentPicker.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
import {Platform} from 'react-native';
import DocumentPicker from 'react-native-document-picker';
import {launchCamera, launchImageLibrary} from 'react-native-image-picker';
import ModalPicker from './ModalPicker';
export const PickInModal = ModalPicker;
export class AttachmentPicker {
constructor(
DocumentPickerOptions,
GalleryPickerOptions,
VideoPickerOptions,
PhotoPickerOptions,
) {
this.DocumentPickerOptions = DocumentPickerOptions;
this.GalleryPickerOptions = GalleryPickerOptions;
this.VideoPickerOptions = VideoPickerOptions;
this.PhotoPickerOptions = PhotoPickerOptions;
}
pickGallery = () => {
return new Promise((resolve, reject) => {
if (!this.GalleryPickerOptions) {
this.GalleryPickerOptions = {};
}
launchImageLibrary(this.GalleryPickerOptions, result => {
if (result.didCancel) {
reject(new Error('Action pickGallery cancelled!'));
} else {
let files = result.assets.map(file => {
return {
uri: Platform.select({
android: file.uri,
ios: decodeURI(file.uri),
}),
filename: file.fileName,
type: file.type
};
});
resolve({
files: files,
original: result,
});
}
});
});
};
makePhoto = () => {
if (!this.PhotoPickerOptions) {
this.PhotoPickerOptions = {
mediaType: 'photo',
};
}
return this.pickCamera(this.PhotoPickerOptions);
};
makeVideo = () => {
if (!this.VideoPickerOptions) {
this.VideoPickerOptions = {
mediaType: 'video',
};
}
return this.pickCamera(this.VideoPickerOptions);
};
pickCamera = params => {
return new Promise((resolve, reject) => {
launchCamera(params, result => {
if (result.errorCode) {
reject(new Error(result.errorCode));
return;
}
if (result.didCancel) {
reject(new Error('Action pickCamera cancelled!'));
return;
} else {
let files = result.assets.map(file => {
return {
uri: Platform.select({
android: file.uri,
ios: decodeURI(file.uri),
}),
filename: file.fileName,
type: file.type
};
});
resolve({
files: files,
original: result,
});
}
});
});
};
pickFiles = () => {
return new Promise(async (resolve, reject) => {
try {
if (!this.DocumentPickerOptions) {
this.DocumentPickerOptions = {
presentationStyle: 'fullScreen',
copyTo: 'cachesDirectory',
allowMultiSelection: true,
};
}
const result = await DocumentPicker.pick(this.DocumentPickerOptions);
let files = result.map(file => {
return {
uri: Platform.select({
android: file.fileCopyUri,
ios: decodeURI(file.uri),
}),
filename: file.name,
type: file.type
};
});
resolve({
files: files,
original: result,
});
} catch (e) {
console.log(e);
reject(new Error('Action pickFiles cancelled!'));
}
});
};
}