@supportworks/multi-imagepicker 5.0.2 → 5.0.4

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 CHANGED
@@ -1,3 +1,8 @@
1
+ # v5.0.4 2024-11-14
2
+ * updated multiimagepicker perms message for android to be more clear
3
+
4
+ # v5.0.3 2024-10-23
5
+ * updated multiimagepicker to catch missing perms
1
6
 
2
7
  # v5.0.2 2024-08-30
3
8
  * swmaterialdesign
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supportworks/multi-imagepicker",
3
- "version": "5.0.2",
3
+ "version": "5.0.4",
4
4
  "description": "Android and iOS customized multiple image picker. For react-native >= 0.60.",
5
5
  "main": "index.js",
6
6
  "author": {
@@ -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('Both Storage and the Photos and Videos');
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
- if (cRef.props.callback) {
38
- for(let i=0;i<cRef.imgArr.length;i++){
39
- let x = await ImageResizer.createResizedImage(cRef.imgArr[i].path, 2000, 2000, "JPEG", 80, 0);
40
- await Api.getApi().moveLocalFile(x.path, cRef.imgArr[i].path, true);
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
- cRef.props.callback({index: index, cancelIndex: cancelIndex, imageArray: cRef.imgArr, imageDescArray: cRef.imgDescArr, imageValidArray: cRef.imgValidArr, imageSource: imageSource, callbackData: cRef.props.callbackData});
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
- let cRef = this;
48
- if(depth>cRef.props.maxCount) {
170
+ if (depth > this.props.maxCount) {
49
171
  callback();
50
172
  return;
51
173
  }
52
- ImagePicker.openCamera(options).then(async image => {
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
- cRef.imgArr.push(image);
55
- cRef.imgDescArr.push('');
56
- cRef.imgValidArr.push(true);
57
- await cRef.takeOnePicture(index, cancelIndex, options, depth + 1, callback);
58
- }).catch((e)=>{
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
- let cRef = this;
71
- let options = {
202
+ const options = {
72
203
  multiple: true,
73
- maxFiles: cRef.props.maxCount, //ios only
204
+ maxFiles: this.props.maxCount, //ios only
74
205
  cropping: false,
75
- mediaType: 'photo'
206
+ mediaType: 'photo',
76
207
  };
77
- cRef.imgArr = []; //clear selected images
78
- cRef.imgDescArr = []; //clear descriptions for selected images
79
- cRef.imgValidArr = []; //clear valid flags for selected images
80
- let takePhotoIndex = cancelIndex - 2;
81
- let chooseLibIndex = cancelIndex - 1;
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
- //Take Photo
87
- cRef.takeOnePicture(index, cancelIndex, options, 1, ()=>{
88
- if(cRef.imgArr.length>0){
89
- imageSource = "taken";
90
- cRef.pickerDone(index, cancelIndex, imageSource);
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
- //Choose from Library
97
- ImagePicker.openPicker(options).then(async images => {
98
- console.log("SWSI choose photo, images: " + JSON.stringify(images));
99
- if (images.length > cRef.props.maxCount) {
100
- alert("You may only select a max of " + cRef.props.maxCount + " photo(s)");
101
- } else {
102
- cRef.imgArr = images;
103
- //create array of blank entries for descriptions they might add. Also, an array for validitity of titles
104
- for (let i = 0; i < cRef.imgArr.length; i++) {
105
- cRef.imgDescArr.push('');
106
- cRef.imgValidArr.push(true);
107
- }
108
- imageSource = "library";
109
- cRef.pickerDone(index, cancelIndex, imageSource);
110
- }
111
- }).catch(err1 => console.log("----usePicker, openPicker: User Cancelled Image Picker (choose from lib), error: "+err1.message));
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
- //user pressed cancel (last button) or a custom button
116
- console.log("----usePicker(): User pressed cancel");
251
+ console.log('----usePicker User pressed cancel');
117
252
  break;
118
253
 
119
254
  default:
120
- //user pressed cancel (last button) or a custom button
121
- console.log("----usePicker(): User pressed custom button");
122
- cRef.pickerDone(index, cancelIndex);
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
- let cRef = this;
135
- let customButtonArr = cRef.props.customButtons;
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={cRef.props.parentStyle}>
269
+ <View style={this.props.parentStyle}>
142
270
  <ActionSheet
143
- ref={o => cRef.ActionSheet = o}
144
- title={cRef.props.title}
271
+ ref={(o) => (this.ActionSheet = o)}
272
+ title={this.props.title}
145
273
  options={buttonArr}
146
274
  cancelButtonIndex={cancelIndex}
147
- onPress={(index) => {
148
- cRef.usePicker(index, cancelIndex)
149
- }}>
150
- </ActionSheet>
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
  }