@supportworks/multi-imagepicker 8.0.2 → 8.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
CHANGED
package/package.json
CHANGED
|
@@ -1,348 +0,0 @@
|
|
|
1
|
-
import * as React from 'react'
|
|
2
|
-
import { View, TouchableOpacity, Alert, Platform, Linking, Modal } from 'react-native'
|
|
3
|
-
import ActionSheet from '@alessiocancian/react-native-actionsheet'
|
|
4
|
-
import ImagePicker from 'react-native-image-crop-picker'
|
|
5
|
-
import ImageResizer from '@bam.tech/react-native-image-resizer'
|
|
6
|
-
import { Api } from '@supportworks/api-core'
|
|
7
|
-
import { check, request, PERMISSIONS, RESULTS, openSettings } from 'react-native-permissions'
|
|
8
|
-
import CameraTestModal from '../../../../../app/screens/menu/CameraTestModal'
|
|
9
|
-
|
|
10
|
-
class MultiImagePicker extends React.Component {
|
|
11
|
-
static defaultProps = {
|
|
12
|
-
useCamera: 'Take Photo...',
|
|
13
|
-
useLibrary: 'Choose from Library...',
|
|
14
|
-
title: 'Add Photo(s)',
|
|
15
|
-
parentStyle: null, // style to be applied to the parent (view) in the render function. Otherwise, a 'style' prop is applied to the TouchableOpacity, which is a child of View.
|
|
16
|
-
customButtons: [], // array of titles of custom buttons to display, pre-pended to action sheet buttons (optional)
|
|
17
|
-
maxCount: 25, // max number of images that can be selected
|
|
18
|
-
callbackData: null, // data appended to payload and passed with callback function (optional)
|
|
19
|
-
callback: null // callback function (optional)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
constructor (props) {
|
|
23
|
-
super(props)
|
|
24
|
-
this.state = {
|
|
25
|
-
showCameraModal: false,
|
|
26
|
-
pendingPickerIndex: null,
|
|
27
|
-
pendingCancelIndex: null
|
|
28
|
-
}
|
|
29
|
-
this.imgArr = [] // temp array for holding selected images to attach, before title & desc updated
|
|
30
|
-
this.imgDescArr = [] // temp array for holding descriptions of selected images, before saved to project
|
|
31
|
-
this.imgValidArr = [] // temp array for holding boolean value specifying whether each image title is valid or not
|
|
32
|
-
this.pickerDone = this.pickerDone.bind(this)
|
|
33
|
-
this.showActionSheet = this.showActionSheet.bind(this)
|
|
34
|
-
this.takeOnePicture = this.takeOnePicture.bind(this)
|
|
35
|
-
this.handleCameraModalDone = this.handleCameraModalDone.bind(this)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async checkAndRequestSinglePermission (permission) {
|
|
39
|
-
const result = await check(permission)
|
|
40
|
-
switch (result) {
|
|
41
|
-
case RESULTS.GRANTED:
|
|
42
|
-
return true
|
|
43
|
-
case RESULTS.DENIED:
|
|
44
|
-
// if request is blocked, show persmission
|
|
45
|
-
const requestResponse = await request(permission)
|
|
46
|
-
if (requestResponse === RESULTS.BLOCKED) {
|
|
47
|
-
this.showPermissionAlert('Storage')
|
|
48
|
-
return false
|
|
49
|
-
}
|
|
50
|
-
return requestResponse === RESULTS.GRANTED
|
|
51
|
-
case RESULTS.BLOCKED:
|
|
52
|
-
this.showPermissionAlert('Storage')
|
|
53
|
-
return false
|
|
54
|
-
default:
|
|
55
|
-
return false
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async requestPermissions (permission) {
|
|
60
|
-
if (Array.isArray(permission)) {
|
|
61
|
-
// Handle multiple permissions
|
|
62
|
-
const results = await Promise.all(permission.map((p) => this.checkAndRequestSinglePermission(p)))
|
|
63
|
-
return results.every((result) => result) // All permissions must be granted
|
|
64
|
-
} else {
|
|
65
|
-
// Handle single permission
|
|
66
|
-
return await this.checkAndRequestSinglePermission(permission)
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async requestCameraPermission () {
|
|
71
|
-
const permission = Platform.select({
|
|
72
|
-
ios: PERMISSIONS.IOS.CAMERA,
|
|
73
|
-
android: PERMISSIONS.ANDROID.CAMERA
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
const result = await check(permission)
|
|
77
|
-
|
|
78
|
-
switch (result) {
|
|
79
|
-
case RESULTS.GRANTED:
|
|
80
|
-
return true
|
|
81
|
-
case RESULTS.DENIED:
|
|
82
|
-
const requestResult = await request(permission)
|
|
83
|
-
return requestResult === RESULTS.GRANTED
|
|
84
|
-
case RESULTS.BLOCKED:
|
|
85
|
-
this.showPermissionAlert('Camera')
|
|
86
|
-
return false
|
|
87
|
-
default:
|
|
88
|
-
return false
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
showPermissionAlert (type) {
|
|
93
|
-
const platformMessage = Platform.select({
|
|
94
|
-
ios:
|
|
95
|
-
'Photo library access is required. Please go to Settings > Privacy > Photos and grant full access for this app.',
|
|
96
|
-
android:
|
|
97
|
-
`${type} access is required to select photos. Please go to Settings > Apps > THIS_APP > Permissions and enable access.`
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
Alert.alert(
|
|
101
|
-
'Permission Required',
|
|
102
|
-
`${platformMessage} You need to enable access to continue.`,
|
|
103
|
-
[
|
|
104
|
-
{ text: 'Cancel', style: 'cancel' },
|
|
105
|
-
{
|
|
106
|
-
text: 'Open Settings',
|
|
107
|
-
onPress: () => openSettings().catch(() => Linking.openURL('app-settings:'))
|
|
108
|
-
}
|
|
109
|
-
]
|
|
110
|
-
)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async checkAndOpenPicker (options, action) {
|
|
114
|
-
let permission
|
|
115
|
-
|
|
116
|
-
if (Platform.OS === 'android') {
|
|
117
|
-
// Check Android version
|
|
118
|
-
const androidVersion = parseInt(Platform.Version, 10)
|
|
119
|
-
if (androidVersion >= 33) {
|
|
120
|
-
// Android 13+ requires specific media permissions -
|
|
121
|
-
// UPDATE 9/24/25: these are no longer needed and in fact Google Play Store prohibits use of them in our case
|
|
122
|
-
/* permission = [
|
|
123
|
-
PERMISSIONS.ANDROID.READ_MEDIA_IMAGES,
|
|
124
|
-
PERMISSIONS.ANDROID.READ_MEDIA_VIDEO,
|
|
125
|
-
]; */
|
|
126
|
-
} else {
|
|
127
|
-
// Use legacy storage permission for Android < 33
|
|
128
|
-
permission = PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE
|
|
129
|
-
}
|
|
130
|
-
} else {
|
|
131
|
-
// iOS permission
|
|
132
|
-
permission = PERMISSIONS.IOS.PHOTO_LIBRARY
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// check if permission is a string or an array of strings, if its an array do a for each and check each permission
|
|
136
|
-
const hasPermission = permission ? await this.requestPermissions(permission) : true
|
|
137
|
-
if (hasPermission) {
|
|
138
|
-
action(options) // Proceed if permission granted
|
|
139
|
-
} else {
|
|
140
|
-
this.showPermissionAlert('Both Storage and the Photos and Videos')
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
async pickerDone (index, cancelIndex, imageSource = null) {
|
|
145
|
-
// user is done using the picker to select images. index: index of button that was clicked.
|
|
146
|
-
// cancelIndex: index of the cancel button, so we know which button was clicked, since we know cancel is the last index available.
|
|
147
|
-
// imageSource: "taken" if image was taken from device now, "library" if image(s) were selected from library
|
|
148
|
-
|
|
149
|
-
const { callback, callbackData } = this.props
|
|
150
|
-
if (callback) {
|
|
151
|
-
for (let i = 0; i < this.imgArr.length; i++) {
|
|
152
|
-
const resizedImage = await ImageResizer.createResizedImage(
|
|
153
|
-
this.imgArr[i].path,
|
|
154
|
-
2000,
|
|
155
|
-
2000,
|
|
156
|
-
'JPEG',
|
|
157
|
-
80
|
|
158
|
-
)
|
|
159
|
-
await Api.getApi().moveLocalFile(resizedImage.path, this.imgArr[i].path, true)
|
|
160
|
-
}
|
|
161
|
-
callback({
|
|
162
|
-
index,
|
|
163
|
-
cancelIndex,
|
|
164
|
-
imageArray: this.imgArr,
|
|
165
|
-
imageDescArray: this.imgDescArr,
|
|
166
|
-
imageValidArray: this.imgValidArr,
|
|
167
|
-
imageSource,
|
|
168
|
-
callbackData
|
|
169
|
-
})
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
async handleCameraModalDone (images) {
|
|
174
|
-
// Handle images returned from CameraTestModal
|
|
175
|
-
console.log('SWSI Camera modal done, images:', images)
|
|
176
|
-
|
|
177
|
-
this.setState({ showCameraModal: false })
|
|
178
|
-
|
|
179
|
-
if (images && images.length > 0) {
|
|
180
|
-
// Convert CameraTestModal format to ImagePicker format
|
|
181
|
-
for (const img of images) {
|
|
182
|
-
const imageData = {
|
|
183
|
-
path: img.imageURI,
|
|
184
|
-
mime: 'image/jpeg',
|
|
185
|
-
width: 2000,
|
|
186
|
-
height: 2000
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Store depth map URI if available (can be used by calling code)
|
|
190
|
-
if (img.depthMapURI) {
|
|
191
|
-
imageData.depthMapUri = img.depthMapURI
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
this.imgArr.push(imageData)
|
|
195
|
-
this.imgDescArr.push('')
|
|
196
|
-
this.imgValidArr.push(true)
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const imageSource = 'taken'
|
|
200
|
-
this.pickerDone(this.state.pendingPickerIndex, this.state.pendingCancelIndex, imageSource)
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// Reset pending indices
|
|
204
|
-
this.setState({
|
|
205
|
-
pendingPickerIndex: null,
|
|
206
|
-
pendingCancelIndex: null
|
|
207
|
-
})
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
async takeOnePicture (index, cancelIndex, options, depth, callback) {
|
|
211
|
-
if (depth > this.props.maxCount) {
|
|
212
|
-
callback()
|
|
213
|
-
return
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const hasPermission = await this.requestCameraPermission()
|
|
217
|
-
if (!hasPermission) {
|
|
218
|
-
this.showPermissionAlert('Camera')
|
|
219
|
-
callback()
|
|
220
|
-
return
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
try {
|
|
224
|
-
const image = await ImagePicker.openCamera(options)
|
|
225
|
-
console.log('SWSI take photo, image: ' + JSON.stringify(image))
|
|
226
|
-
|
|
227
|
-
this.imgArr.push(image)
|
|
228
|
-
this.imgDescArr.push('')
|
|
229
|
-
this.imgValidArr.push(true)
|
|
230
|
-
await this.takeOnePicture(index, cancelIndex, options, depth + 1, callback)
|
|
231
|
-
} catch (e) {
|
|
232
|
-
callback()
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
usePicker (index, cancelIndex) {
|
|
237
|
-
// index: index of ActionSheet. Depends on whether custom buttons added. Last 3 are fixed (Take Photo, Choose from Library, Cancel). Anything before that is custom button.
|
|
238
|
-
// Custom buttons can be sent using customButtons prop.
|
|
239
|
-
// results - sets cRef.imgArr to an array of image info that the picker library provided
|
|
240
|
-
// sets cRef.imgDescArray to an array of the image descriptions
|
|
241
|
-
// initializes cRef.imgValidArr to an array of booleans (all true) for all selected images, to be used/updated by calling code as needed
|
|
242
|
-
|
|
243
|
-
const options = {
|
|
244
|
-
multiple: true,
|
|
245
|
-
maxFiles: this.props.maxCount, // ios only
|
|
246
|
-
cropping: false,
|
|
247
|
-
mediaType: 'photo'
|
|
248
|
-
}
|
|
249
|
-
this.imgArr = [] // clear selected images
|
|
250
|
-
this.imgDescArr = [] // clear descriptions for selected images
|
|
251
|
-
this.imgValidArr = [] // clear valid flags for selected images
|
|
252
|
-
|
|
253
|
-
const takePhotoIndex = cancelIndex - 2
|
|
254
|
-
const chooseLibIndex = cancelIndex - 1
|
|
255
|
-
let imageSource = null
|
|
256
|
-
|
|
257
|
-
switch (index) {
|
|
258
|
-
case takePhotoIndex:
|
|
259
|
-
console.log('User pressed take photo')
|
|
260
|
-
|
|
261
|
-
// Use CameraTestModal on iOS, native camera on Android
|
|
262
|
-
if (Platform.OS === 'ios') {
|
|
263
|
-
this.setState({
|
|
264
|
-
showCameraModal: true,
|
|
265
|
-
pendingPickerIndex: index,
|
|
266
|
-
pendingCancelIndex: cancelIndex
|
|
267
|
-
})
|
|
268
|
-
} else {
|
|
269
|
-
// Fall back to native camera (Android)
|
|
270
|
-
this.checkAndOpenPicker(options, () =>
|
|
271
|
-
this.takeOnePicture(index, cancelIndex, options, 1, () => {
|
|
272
|
-
if (this.imgArr.length > 0) {
|
|
273
|
-
imageSource = 'taken'
|
|
274
|
-
this.pickerDone(index, cancelIndex, imageSource)
|
|
275
|
-
}
|
|
276
|
-
})
|
|
277
|
-
)
|
|
278
|
-
}
|
|
279
|
-
break
|
|
280
|
-
|
|
281
|
-
case chooseLibIndex:
|
|
282
|
-
this.checkAndOpenPicker(options, () =>
|
|
283
|
-
ImagePicker.openPicker(options)
|
|
284
|
-
.then((images) => {
|
|
285
|
-
if (images.length > this.props.maxCount) {
|
|
286
|
-
alert(`You may only select a max of ${this.props.maxCount} photo(s)`)
|
|
287
|
-
} else {
|
|
288
|
-
this.imgArr = images
|
|
289
|
-
this.imgDescArr = Array(images.length).fill('')
|
|
290
|
-
this.imgValidArr = Array(images.length).fill(true)
|
|
291
|
-
imageSource = 'library'
|
|
292
|
-
this.pickerDone(index, cancelIndex, imageSource)
|
|
293
|
-
}
|
|
294
|
-
})
|
|
295
|
-
.catch((err) => {
|
|
296
|
-
console.log(`----usePicker Image Picker Error: ${err.message}`)
|
|
297
|
-
})
|
|
298
|
-
)
|
|
299
|
-
break
|
|
300
|
-
|
|
301
|
-
case cancelIndex:
|
|
302
|
-
console.log('----usePicker User pressed cancel')
|
|
303
|
-
break
|
|
304
|
-
|
|
305
|
-
default:
|
|
306
|
-
console.log('----usePicker User pressed custom button')
|
|
307
|
-
this.pickerDone(index, cancelIndex)
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
showActionSheet = () => {
|
|
312
|
-
this.ActionSheet.show()
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
render () {
|
|
316
|
-
const buttonArr = [...this.props.customButtons, this.props.useCamera, this.props.useLibrary, 'Cancel']
|
|
317
|
-
const cancelIndex = buttonArr.length - 1
|
|
318
|
-
|
|
319
|
-
return (
|
|
320
|
-
<View style={this.props.parentStyle}>
|
|
321
|
-
<ActionSheet
|
|
322
|
-
ref={(o) => (this.ActionSheet = o)}
|
|
323
|
-
title={this.props.title}
|
|
324
|
-
options={buttonArr}
|
|
325
|
-
cancelButtonIndex={cancelIndex}
|
|
326
|
-
onPress={(index) => this.usePicker(index, cancelIndex)}
|
|
327
|
-
/>
|
|
328
|
-
<TouchableOpacity {...this.props} onPress={this.showActionSheet}>
|
|
329
|
-
{this.props.children}
|
|
330
|
-
</TouchableOpacity>
|
|
331
|
-
|
|
332
|
-
{/* Camera Modal for iOS */}
|
|
333
|
-
{Platform.OS === 'ios' && this.state.showCameraModal && (
|
|
334
|
-
<Modal
|
|
335
|
-
visible={this.state.showCameraModal}
|
|
336
|
-
animationType='slide'
|
|
337
|
-
presentationStyle='fullScreen'
|
|
338
|
-
onRequestClose={() => this.handleCameraModalDone([])}
|
|
339
|
-
>
|
|
340
|
-
<CameraTestModal onDone={this.handleCameraModalDone} limit={this.props.maxCount} onCancel={() => this.handleCameraModalDone([])} />
|
|
341
|
-
</Modal>
|
|
342
|
-
)}
|
|
343
|
-
</View>
|
|
344
|
-
)
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
export default MultiImagePicker
|
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
import * as React from 'react'
|
|
2
|
-
import { View, TouchableOpacity, Alert, Platform, Linking } from 'react-native'
|
|
3
|
-
import ActionSheet from '@alessiocancian/react-native-actionsheet'
|
|
4
|
-
import ImagePicker from 'react-native-image-crop-picker'
|
|
5
|
-
import ImageResizer from '@bam.tech/react-native-image-resizer'
|
|
6
|
-
import { Api } from '@supportworks/api-core'
|
|
7
|
-
import { check, request, PERMISSIONS, RESULTS, openSettings } from 'react-native-permissions'
|
|
8
|
-
|
|
9
|
-
class MultiImagePicker extends React.Component {
|
|
10
|
-
static defaultProps = {
|
|
11
|
-
useCamera: 'Take Photo...',
|
|
12
|
-
useLibrary: 'Choose from Library...',
|
|
13
|
-
title: 'Add Photo(s)',
|
|
14
|
-
parentStyle: null, // style to be applied to the parent (view) in the render function. Otherwise, a 'style' prop is applied to the TouchableOpacity, which is a child of View.
|
|
15
|
-
customButtons: [], // array of titles of custom buttons to display, pre-pended to action sheet buttons (optional)
|
|
16
|
-
maxCount: 25, // max number of images that can be selected
|
|
17
|
-
callbackData: null, // data appended to payload and passed with callback function (optional)
|
|
18
|
-
callback: null // callback function (optional)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
constructor (props) {
|
|
22
|
-
super(props)
|
|
23
|
-
this.imgArr = [] // temp array for holding selected images to attach, before title & desc updated
|
|
24
|
-
this.imgDescArr = [] // temp array for holding descriptions of selected images, before saved to project
|
|
25
|
-
this.imgValidArr = [] // temp array for holding boolean value specifying whether each image title is valid or not
|
|
26
|
-
this.pickerDone = this.pickerDone.bind(this)
|
|
27
|
-
this.showActionSheet = this.showActionSheet.bind(this)
|
|
28
|
-
this.takeOnePicture = this.takeOnePicture.bind(this)
|
|
29
|
-
}
|
|
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
|
-
async requestCameraPermission () {
|
|
64
|
-
const permission = Platform.select({
|
|
65
|
-
ios: PERMISSIONS.IOS.CAMERA,
|
|
66
|
-
android: PERMISSIONS.ANDROID.CAMERA
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
const result = await check(permission)
|
|
70
|
-
|
|
71
|
-
switch (result) {
|
|
72
|
-
case RESULTS.GRANTED:
|
|
73
|
-
return true
|
|
74
|
-
case RESULTS.DENIED:
|
|
75
|
-
const requestResult = await request(permission)
|
|
76
|
-
return requestResult === RESULTS.GRANTED
|
|
77
|
-
case RESULTS.BLOCKED:
|
|
78
|
-
this.showPermissionAlert('Camera')
|
|
79
|
-
return false
|
|
80
|
-
default:
|
|
81
|
-
return false
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
showPermissionAlert (type) {
|
|
86
|
-
const platformMessage = Platform.select({
|
|
87
|
-
ios:
|
|
88
|
-
'Photo library access is required. Please go to Settings > Privacy > Photos and grant full access for this app.',
|
|
89
|
-
android:
|
|
90
|
-
`${type} access is required to select photos. Please go to Settings > Apps > THIS_APP > Permissions and enable access.`
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
Alert.alert(
|
|
94
|
-
'Permission Required',
|
|
95
|
-
`${platformMessage} You need to enable access to continue.`,
|
|
96
|
-
[
|
|
97
|
-
{ text: 'Cancel', style: 'cancel' },
|
|
98
|
-
{
|
|
99
|
-
text: 'Open Settings',
|
|
100
|
-
onPress: () => openSettings().catch(() => Linking.openURL('app-settings:'))
|
|
101
|
-
}
|
|
102
|
-
]
|
|
103
|
-
)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async checkAndOpenPicker (options, action) {
|
|
107
|
-
let permission
|
|
108
|
-
|
|
109
|
-
if (Platform.OS === 'android') {
|
|
110
|
-
// Check Android version
|
|
111
|
-
const androidVersion = parseInt(Platform.Version, 10)
|
|
112
|
-
if (androidVersion >= 33) {
|
|
113
|
-
// Android 13+ requires specific media permissions -
|
|
114
|
-
// UPDATE 9/24/25: these are no longer needed and in fact Google Play Store prohibits use of them in our case
|
|
115
|
-
/* permission = [
|
|
116
|
-
PERMISSIONS.ANDROID.READ_MEDIA_IMAGES,
|
|
117
|
-
PERMISSIONS.ANDROID.READ_MEDIA_VIDEO,
|
|
118
|
-
]; */
|
|
119
|
-
} else {
|
|
120
|
-
// Use legacy storage permission for Android < 33
|
|
121
|
-
permission = PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE
|
|
122
|
-
}
|
|
123
|
-
} else {
|
|
124
|
-
// iOS permission
|
|
125
|
-
permission = PERMISSIONS.IOS.PHOTO_LIBRARY
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// check if permission is a string or an array of strings, if its an array do a for each and check each permission
|
|
129
|
-
const hasPermission = permission ? await this.requestPermissions(permission) : true
|
|
130
|
-
if (hasPermission) {
|
|
131
|
-
action(options) // Proceed if permission granted
|
|
132
|
-
} else {
|
|
133
|
-
this.showPermissionAlert('Both Storage and the Photos and Videos')
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
async pickerDone (index, cancelIndex, imageSource = null) {
|
|
138
|
-
// user is done using the picker to select images. index: index of button that was clicked.
|
|
139
|
-
// cancelIndex: index of the cancel button, so we know which button was clicked, since we know cancel is the last index available.
|
|
140
|
-
// imageSource: "taken" if image was taken from device now, "library" if image(s) were selected from library
|
|
141
|
-
|
|
142
|
-
const { callback, callbackData } = this.props
|
|
143
|
-
if (callback) {
|
|
144
|
-
for (let i = 0; i < this.imgArr.length; i++) {
|
|
145
|
-
const resizedImage = await ImageResizer.createResizedImage(
|
|
146
|
-
this.imgArr[i].path,
|
|
147
|
-
2000,
|
|
148
|
-
2000,
|
|
149
|
-
'JPEG',
|
|
150
|
-
80
|
|
151
|
-
)
|
|
152
|
-
await Api.getApi().moveLocalFile(resizedImage.path, this.imgArr[i].path, true)
|
|
153
|
-
}
|
|
154
|
-
callback({
|
|
155
|
-
index,
|
|
156
|
-
cancelIndex,
|
|
157
|
-
imageArray: this.imgArr,
|
|
158
|
-
imageDescArray: this.imgDescArr,
|
|
159
|
-
imageValidArray: this.imgValidArr,
|
|
160
|
-
imageSource,
|
|
161
|
-
callbackData
|
|
162
|
-
})
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
async takeOnePicture (index, cancelIndex, options, depth, callback) {
|
|
167
|
-
if (depth > this.props.maxCount) {
|
|
168
|
-
callback()
|
|
169
|
-
return
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const hasPermission = await this.requestCameraPermission()
|
|
173
|
-
if (!hasPermission) {
|
|
174
|
-
this.showPermissionAlert('Camera')
|
|
175
|
-
callback()
|
|
176
|
-
return
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
try {
|
|
180
|
-
const image = await ImagePicker.openCamera(options)
|
|
181
|
-
console.log('SWSI take photo, image: ' + JSON.stringify(image))
|
|
182
|
-
|
|
183
|
-
this.imgArr.push(image)
|
|
184
|
-
this.imgDescArr.push('')
|
|
185
|
-
this.imgValidArr.push(true)
|
|
186
|
-
await this.takeOnePicture(index, cancelIndex, options, depth + 1, callback)
|
|
187
|
-
} catch (e) {
|
|
188
|
-
callback()
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
usePicker (index, cancelIndex) {
|
|
193
|
-
// index: index of ActionSheet. Depends on whether custom buttons added. Last 3 are fixed (Take Photo, Choose from Library, Cancel). Anything before that is custom button.
|
|
194
|
-
// Custom buttons can be sent using customButtons prop.
|
|
195
|
-
// results - sets cRef.imgArr to an array of image info that the picker library provided
|
|
196
|
-
// sets cRef.imgDescArray to an array of the image descriptions
|
|
197
|
-
// initializes cRef.imgValidArr to an array of booleans (all true) for all selected images, to be used/updated by calling code as needed
|
|
198
|
-
|
|
199
|
-
const options = {
|
|
200
|
-
multiple: true,
|
|
201
|
-
maxFiles: this.props.maxCount, // ios only
|
|
202
|
-
cropping: false,
|
|
203
|
-
mediaType: 'photo'
|
|
204
|
-
}
|
|
205
|
-
this.imgArr = [] // clear selected images
|
|
206
|
-
this.imgDescArr = [] // clear descriptions for selected images
|
|
207
|
-
this.imgValidArr = [] // clear valid flags for selected images
|
|
208
|
-
|
|
209
|
-
const takePhotoIndex = cancelIndex - 2
|
|
210
|
-
const chooseLibIndex = cancelIndex - 1
|
|
211
|
-
let imageSource = null
|
|
212
|
-
|
|
213
|
-
switch (index) {
|
|
214
|
-
case takePhotoIndex:
|
|
215
|
-
console.log('User pressed take photo')
|
|
216
|
-
this.checkAndOpenPicker(options, () =>
|
|
217
|
-
this.takeOnePicture(index, cancelIndex, options, 1, () => {
|
|
218
|
-
if (this.imgArr.length > 0) {
|
|
219
|
-
imageSource = 'taken'
|
|
220
|
-
this.pickerDone(index, cancelIndex, imageSource)
|
|
221
|
-
}
|
|
222
|
-
})
|
|
223
|
-
)
|
|
224
|
-
break
|
|
225
|
-
|
|
226
|
-
case chooseLibIndex:
|
|
227
|
-
this.checkAndOpenPicker(options, () =>
|
|
228
|
-
ImagePicker.openPicker(options)
|
|
229
|
-
.then((images) => {
|
|
230
|
-
if (images.length > this.props.maxCount) {
|
|
231
|
-
alert(`You may only select a max of ${this.props.maxCount} photo(s)`)
|
|
232
|
-
} else {
|
|
233
|
-
this.imgArr = images
|
|
234
|
-
this.imgDescArr = Array(images.length).fill('')
|
|
235
|
-
this.imgValidArr = Array(images.length).fill(true)
|
|
236
|
-
imageSource = 'library'
|
|
237
|
-
this.pickerDone(index, cancelIndex, imageSource)
|
|
238
|
-
}
|
|
239
|
-
})
|
|
240
|
-
.catch((err) => {
|
|
241
|
-
console.log(`----usePicker Image Picker Error: ${err.message}`)
|
|
242
|
-
})
|
|
243
|
-
)
|
|
244
|
-
break
|
|
245
|
-
|
|
246
|
-
case cancelIndex:
|
|
247
|
-
console.log('----usePicker User pressed cancel')
|
|
248
|
-
break
|
|
249
|
-
|
|
250
|
-
default:
|
|
251
|
-
console.log('----usePicker User pressed custom button')
|
|
252
|
-
this.pickerDone(index, cancelIndex)
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
showActionSheet = () => {
|
|
257
|
-
this.ActionSheet.show()
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
render () {
|
|
261
|
-
const buttonArr = [...this.props.customButtons, this.props.useCamera, this.props.useLibrary, 'Cancel']
|
|
262
|
-
const cancelIndex = buttonArr.length - 1
|
|
263
|
-
|
|
264
|
-
return (
|
|
265
|
-
<View style={this.props.parentStyle}>
|
|
266
|
-
<ActionSheet
|
|
267
|
-
ref={(o) => (this.ActionSheet = o)}
|
|
268
|
-
title={this.props.title}
|
|
269
|
-
options={buttonArr}
|
|
270
|
-
cancelButtonIndex={cancelIndex}
|
|
271
|
-
onPress={(index) => this.usePicker(index, cancelIndex)}
|
|
272
|
-
/>
|
|
273
|
-
<TouchableOpacity {...this.props} onPress={this.showActionSheet}>
|
|
274
|
-
{this.props.children}
|
|
275
|
-
</TouchableOpacity>
|
|
276
|
-
</View>
|
|
277
|
-
)
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
export default MultiImagePicker
|