@supportworks/multi-imagepicker 5.0.2 → 5.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changes.md +2 -0
- package/package.json +1 -1
- package/src/components/MultiImagePicker.js +199 -75
package/changes.md
CHANGED
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import {View, TouchableOpacity} from 'react-native';
|
|
2
|
+
import { View, TouchableOpacity, Alert, Platform, Linking } from 'react-native';
|
|
3
3
|
import ActionSheet from '@alessiocancian/react-native-actionsheet';
|
|
4
4
|
import ImagePicker from 'react-native-image-crop-picker';
|
|
5
5
|
import ImageResizer from '@bam.tech/react-native-image-resizer';
|
|
6
|
-
import {Api} from '@supportworks/api-core';
|
|
6
|
+
import { Api } from '@supportworks/api-core';
|
|
7
|
+
import { check, request, PERMISSIONS, RESULTS, openSettings } from 'react-native-permissions';
|
|
7
8
|
|
|
8
9
|
class MultiImagePicker extends React.Component {
|
|
9
|
-
|
|
10
10
|
static defaultProps = {
|
|
11
11
|
useCamera: 'Take Photo...',
|
|
12
12
|
useLibrary: 'Choose from Library...',
|
|
@@ -28,36 +28,168 @@ class MultiImagePicker extends React.Component {
|
|
|
28
28
|
this.takeOnePicture = this.takeOnePicture.bind(this);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
async checkAndRequestSinglePermission(permission) {
|
|
32
|
+
const result = await check(permission);
|
|
33
|
+
switch (result) {
|
|
34
|
+
case RESULTS.GRANTED:
|
|
35
|
+
return true;
|
|
36
|
+
case RESULTS.DENIED:
|
|
37
|
+
//if request is blocked, show persmission
|
|
38
|
+
const requestResponse = await request(permission);
|
|
39
|
+
if (requestResponse === RESULTS.BLOCKED) {
|
|
40
|
+
this.showPermissionAlert('Storage');
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return requestResponse === RESULTS.GRANTED;
|
|
44
|
+
case RESULTS.BLOCKED:
|
|
45
|
+
this.showPermissionAlert('Storage');
|
|
46
|
+
return false;
|
|
47
|
+
default:
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async requestPermissions(permission) {
|
|
53
|
+
if (Array.isArray(permission)) {
|
|
54
|
+
// Handle multiple permissions
|
|
55
|
+
const results = await Promise.all(permission.map((p) => this.checkAndRequestSinglePermission(p)));
|
|
56
|
+
return results.every((result) => result); // All permissions must be granted
|
|
57
|
+
} else {
|
|
58
|
+
// Handle single permission
|
|
59
|
+
return await this.checkAndRequestSinglePermission(permission);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
async requestCameraPermission() {
|
|
65
|
+
const permission = Platform.select({
|
|
66
|
+
ios: PERMISSIONS.IOS.CAMERA,
|
|
67
|
+
android: PERMISSIONS.ANDROID.CAMERA,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const result = await check(permission);
|
|
71
|
+
|
|
72
|
+
switch (result) {
|
|
73
|
+
case RESULTS.GRANTED:
|
|
74
|
+
return true;
|
|
75
|
+
case RESULTS.DENIED:
|
|
76
|
+
const requestResult = await request(permission);
|
|
77
|
+
return requestResult === RESULTS.GRANTED;
|
|
78
|
+
case RESULTS.BLOCKED:
|
|
79
|
+
this.showPermissionAlert('Camera');
|
|
80
|
+
return false;
|
|
81
|
+
default:
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
showPermissionAlert(type) {
|
|
87
|
+
|
|
88
|
+
const platformMessage = Platform.select({
|
|
89
|
+
ios:
|
|
90
|
+
"Photo library access is required. Please go to Settings > Privacy > Photos and grant full access for this app.",
|
|
91
|
+
android:
|
|
92
|
+
`${type} access is required to select photos. Please go to Settings > Apps > SolutionView Tablet > Permissions and enable access.`,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
Alert.alert(
|
|
96
|
+
'Permission Required',
|
|
97
|
+
`${platformMessage} You need to enable access to continue.`,
|
|
98
|
+
[
|
|
99
|
+
{ text: 'Cancel', style: 'cancel' },
|
|
100
|
+
{
|
|
101
|
+
text: 'Open Settings',
|
|
102
|
+
onPress: () => openSettings().catch(() => Linking.openURL('app-settings:'))
|
|
103
|
+
},
|
|
104
|
+
]
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async checkAndOpenPicker(options, action) {
|
|
109
|
+
|
|
110
|
+
//if its android 33+ then we don't need to check read external sorage. we need to check media images and video
|
|
111
|
+
let permission;
|
|
112
|
+
|
|
113
|
+
if (Platform.OS === 'android') {
|
|
114
|
+
// Check Android version
|
|
115
|
+
const androidVersion = parseInt(Platform.Version, 10);
|
|
116
|
+
if (androidVersion >= 33) {
|
|
117
|
+
// Android 13+ requires specific media permissions
|
|
118
|
+
permission = [
|
|
119
|
+
PERMISSIONS.ANDROID.READ_MEDIA_IMAGES,
|
|
120
|
+
PERMISSIONS.ANDROID.READ_MEDIA_VIDEO,
|
|
121
|
+
];
|
|
122
|
+
} else {
|
|
123
|
+
// Use legacy storage permission for Android < 33
|
|
124
|
+
permission = PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE;
|
|
125
|
+
}
|
|
126
|
+
} else {
|
|
127
|
+
// iOS permission
|
|
128
|
+
permission = PERMISSIONS.IOS.PHOTO_LIBRARY;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
//check if permission is a string or an array of strings, if its an array do a for each and check each permission
|
|
132
|
+
const hasPermission = await this.requestPermissions(permission);
|
|
133
|
+
if (hasPermission) {
|
|
134
|
+
action(options); // Proceed if permission granted
|
|
135
|
+
} else {
|
|
136
|
+
this.showPermissionAlert('Storage');
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
31
140
|
async pickerDone(index, cancelIndex, imageSource = null) {
|
|
32
141
|
//user is done using the picker to select images. index: index of button that was clicked.
|
|
33
142
|
// cancelIndex: index of the cancel button, so we know which button was clicked, since we know cancel is the last index available.
|
|
34
143
|
// imageSource: "taken" if image was taken from device now, "library" if image(s) were selected from library
|
|
35
|
-
let cRef = this;
|
|
36
144
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
await
|
|
145
|
+
const { callback, callbackData } = this.props;
|
|
146
|
+
if (callback) {
|
|
147
|
+
for (let i = 0; i < this.imgArr.length; i++) {
|
|
148
|
+
const resizedImage = await ImageResizer.createResizedImage(
|
|
149
|
+
this.imgArr[i].path,
|
|
150
|
+
2000,
|
|
151
|
+
2000,
|
|
152
|
+
'JPEG',
|
|
153
|
+
80
|
|
154
|
+
);
|
|
155
|
+
await Api.getApi().moveLocalFile(resizedImage.path, this.imgArr[i].path, true);
|
|
41
156
|
}
|
|
42
|
-
|
|
157
|
+
callback({
|
|
158
|
+
index,
|
|
159
|
+
cancelIndex,
|
|
160
|
+
imageArray: this.imgArr,
|
|
161
|
+
imageDescArray: this.imgDescArr,
|
|
162
|
+
imageValidArray: this.imgValidArr,
|
|
163
|
+
imageSource,
|
|
164
|
+
callbackData,
|
|
165
|
+
});
|
|
43
166
|
}
|
|
44
167
|
}
|
|
45
168
|
|
|
46
169
|
async takeOnePicture(index, cancelIndex, options, depth, callback) {
|
|
47
|
-
|
|
48
|
-
if(depth>cRef.props.maxCount) {
|
|
170
|
+
if (depth > this.props.maxCount) {
|
|
49
171
|
callback();
|
|
50
172
|
return;
|
|
51
173
|
}
|
|
52
|
-
|
|
174
|
+
|
|
175
|
+
const hasPermission = await this.requestCameraPermission();
|
|
176
|
+
if (!hasPermission) {
|
|
177
|
+
this.showPermissionAlert('Camera');
|
|
178
|
+
callback();
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
const image = await ImagePicker.openCamera(options);
|
|
53
184
|
console.log("SWSI take photo, image: " + JSON.stringify(image));
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
185
|
+
|
|
186
|
+
this.imgArr.push(image);
|
|
187
|
+
this.imgDescArr.push('');
|
|
188
|
+
this.imgValidArr.push(true);
|
|
189
|
+
await this.takeOnePicture(index, cancelIndex, options, depth + 1, callback);
|
|
190
|
+
} catch (e) {
|
|
59
191
|
callback();
|
|
60
|
-
}
|
|
192
|
+
}
|
|
61
193
|
}
|
|
62
194
|
|
|
63
195
|
usePicker(index, cancelIndex) {
|
|
@@ -67,92 +199,84 @@ class MultiImagePicker extends React.Component {
|
|
|
67
199
|
// sets cRef.imgDescArray to an array of the image descriptions
|
|
68
200
|
// initializes cRef.imgValidArr to an array of booleans (all true) for all selected images, to be used/updated by calling code as needed
|
|
69
201
|
|
|
70
|
-
|
|
71
|
-
let options = {
|
|
202
|
+
const options = {
|
|
72
203
|
multiple: true,
|
|
73
|
-
maxFiles:
|
|
204
|
+
maxFiles: this.props.maxCount, //ios only
|
|
74
205
|
cropping: false,
|
|
75
|
-
mediaType: 'photo'
|
|
206
|
+
mediaType: 'photo',
|
|
76
207
|
};
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
208
|
+
this.imgArr = []; //clear selected images
|
|
209
|
+
this.imgDescArr = []; //clear descriptions for selected images
|
|
210
|
+
this.imgValidArr = []; //clear valid flags for selected images
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
const takePhotoIndex = cancelIndex - 2;
|
|
214
|
+
const chooseLibIndex = cancelIndex - 1;
|
|
82
215
|
let imageSource = null;
|
|
83
216
|
|
|
84
217
|
switch (index) {
|
|
85
218
|
case takePhotoIndex:
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
219
|
+
console.log('User pressed take photo');
|
|
220
|
+
this.checkAndOpenPicker(options, () =>
|
|
221
|
+
this.takeOnePicture(index, cancelIndex, options, 1, () => {
|
|
222
|
+
if (this.imgArr.length > 0) {
|
|
223
|
+
imageSource = 'taken';
|
|
224
|
+
this.pickerDone(index, cancelIndex, imageSource);
|
|
225
|
+
}
|
|
226
|
+
})
|
|
227
|
+
);
|
|
93
228
|
break;
|
|
94
229
|
|
|
95
230
|
case chooseLibIndex:
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
231
|
+
this.checkAndOpenPicker(options, () =>
|
|
232
|
+
ImagePicker.openPicker(options)
|
|
233
|
+
.then((images) => {
|
|
234
|
+
if (images.length > this.props.maxCount) {
|
|
235
|
+
alert(`You may only select a max of ${this.props.maxCount} photo(s)`);
|
|
236
|
+
} else {
|
|
237
|
+
this.imgArr = images;
|
|
238
|
+
this.imgDescArr = Array(images.length).fill('');
|
|
239
|
+
this.imgValidArr = Array(images.length).fill(true);
|
|
240
|
+
imageSource = 'library';
|
|
241
|
+
this.pickerDone(index, cancelIndex, imageSource);
|
|
242
|
+
}
|
|
243
|
+
})
|
|
244
|
+
.catch((err) => {
|
|
245
|
+
console.log(`----usePicker Image Picker Error: ${err.message}`);
|
|
246
|
+
})
|
|
247
|
+
);
|
|
112
248
|
break;
|
|
113
249
|
|
|
114
250
|
case cancelIndex:
|
|
115
|
-
|
|
116
|
-
console.log("----usePicker(): User pressed cancel");
|
|
251
|
+
console.log('----usePicker User pressed cancel');
|
|
117
252
|
break;
|
|
118
253
|
|
|
119
254
|
default:
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return;
|
|
124
|
-
} // end switch
|
|
125
|
-
|
|
255
|
+
console.log('----usePicker User pressed custom button');
|
|
256
|
+
this.pickerDone(index, cancelIndex);
|
|
257
|
+
}
|
|
126
258
|
}
|
|
127
259
|
|
|
128
260
|
showActionSheet = () => {
|
|
129
|
-
//open ActionSheet
|
|
130
261
|
this.ActionSheet.show();
|
|
131
262
|
};
|
|
132
263
|
|
|
133
264
|
render() {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
let fixedButtonArr = [cRef.props.useCamera, cRef.props.useLibrary, 'Cancel'];
|
|
137
|
-
let buttonArr = customButtonArr.concat(fixedButtonArr);
|
|
138
|
-
let cancelIndex = buttonArr.length-1;
|
|
265
|
+
const buttonArr = [...this.props.customButtons, this.props.useCamera, this.props.useLibrary, 'Cancel'];
|
|
266
|
+
const cancelIndex = buttonArr.length - 1;
|
|
139
267
|
|
|
140
268
|
return (
|
|
141
|
-
<View style={
|
|
269
|
+
<View style={this.props.parentStyle}>
|
|
142
270
|
<ActionSheet
|
|
143
|
-
ref={o =>
|
|
144
|
-
title={
|
|
271
|
+
ref={(o) => (this.ActionSheet = o)}
|
|
272
|
+
title={this.props.title}
|
|
145
273
|
options={buttonArr}
|
|
146
274
|
cancelButtonIndex={cancelIndex}
|
|
147
|
-
onPress={(index) =>
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
<TouchableOpacity {...cRef.props} onPress={() => {cRef.props.children ? cRef.showActionSheet() : null}}>
|
|
153
|
-
{cRef.props.children}
|
|
275
|
+
onPress={(index) => this.usePicker(index, cancelIndex)}
|
|
276
|
+
/>
|
|
277
|
+
<TouchableOpacity {...this.props} onPress={this.showActionSheet}>
|
|
278
|
+
{this.props.children}
|
|
154
279
|
</TouchableOpacity>
|
|
155
|
-
|
|
156
280
|
</View>
|
|
157
281
|
);
|
|
158
282
|
}
|