@supportworks/multi-imagepicker 8.3.6 → 8.3.7
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/package.json +1 -1
- package/src/components/AddImages.js +2 -1
- package/src/components/CameraModal.tsx +609 -396
- package/src/components/MultiImagePicker.js +147 -52
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useCallback, useMemo, useRef, forwardRef, useImperativeHandle, useState } from 'react'
|
|
1
|
+
import React, { useCallback, useEffect, useMemo, useRef, forwardRef, useImperativeHandle, useState } from 'react'
|
|
2
2
|
import {
|
|
3
3
|
AppState,
|
|
4
4
|
View,
|
|
@@ -14,6 +14,7 @@ import ImageResizer from '@bam.tech/react-native-image-resizer'
|
|
|
14
14
|
import { Api } from '@supportworks/api-core'
|
|
15
15
|
import { check, request, PERMISSIONS, RESULTS, openSettings } from 'react-native-permissions'
|
|
16
16
|
import CameraModal from './CameraModal'
|
|
17
|
+
import loaderHandler from './loaderHandler'
|
|
17
18
|
|
|
18
19
|
const waitForAppToBeActive = async () => {
|
|
19
20
|
if (AppState.currentState === 'active') {
|
|
@@ -39,6 +40,7 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
39
40
|
parentStyle = null, // style applied to the parent View; otherwise style prop will be forwarded to TouchableOpacity
|
|
40
41
|
customButtons = [], // array of titles of custom buttons to display, pre-pended to action sheet buttons (optional)
|
|
41
42
|
maxCount = 25, // max number of images that can be selected
|
|
43
|
+
existingCount = 0, // number of images already attached outside of this picker session
|
|
42
44
|
callbackData = null, // data appended to payload and passed with callback function (optional)
|
|
43
45
|
callback = null, // callback function (optional)
|
|
44
46
|
children,
|
|
@@ -48,12 +50,48 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
48
50
|
const [showCameraModal, setShowCameraModal] = useState(false)
|
|
49
51
|
const [pendingPickerIndex, setPendingPickerIndex] = useState(null)
|
|
50
52
|
const [pendingCancelIndex, setPendingCancelIndex] = useState(null)
|
|
53
|
+
const [actionSheetInstanceKey, setActionSheetInstanceKey] = useState(0)
|
|
54
|
+
const [pendingCameraModalPayload, setPendingCameraModalPayload] = useState(null)
|
|
51
55
|
|
|
52
56
|
const imgArrRef = useRef([]) // temp array for holding selected images to attach, before title & desc updated
|
|
53
57
|
const imgDescArrRef = useRef([]) // temp array for holding descriptions of selected images, before saved to project
|
|
54
58
|
const imgValidArrRef = useRef([]) // temp array for holding boolean value specifying whether each image title is valid or not
|
|
55
59
|
const actionSheetRef = useRef(null)
|
|
56
60
|
|
|
61
|
+
const resolvedMaxCount = useMemo(() => {
|
|
62
|
+
const parsedMaxCount = Number(maxCount)
|
|
63
|
+
if (!Number.isFinite(parsedMaxCount)) {
|
|
64
|
+
return 25
|
|
65
|
+
}
|
|
66
|
+
return Math.max(0, Math.floor(parsedMaxCount))
|
|
67
|
+
}, [maxCount])
|
|
68
|
+
|
|
69
|
+
const resolvedExistingCount = useMemo(() => {
|
|
70
|
+
const parsedExistingCount = Number(existingCount)
|
|
71
|
+
if (!Number.isFinite(parsedExistingCount)) {
|
|
72
|
+
return 0
|
|
73
|
+
}
|
|
74
|
+
return Math.max(0, Math.floor(parsedExistingCount))
|
|
75
|
+
}, [existingCount])
|
|
76
|
+
|
|
77
|
+
const remainingCount = useMemo(
|
|
78
|
+
() => Math.max(resolvedMaxCount - resolvedExistingCount, 0),
|
|
79
|
+
[resolvedExistingCount, resolvedMaxCount]
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
const resetActionSheet = useCallback(() => {
|
|
83
|
+
actionSheetRef.current = null
|
|
84
|
+
setActionSheetInstanceKey((prevKey) => prevKey + 1)
|
|
85
|
+
}, [])
|
|
86
|
+
|
|
87
|
+
const showSelectionLimitAlert = useCallback(() => {
|
|
88
|
+
const message = remainingCount <= 0
|
|
89
|
+
? `You already have the maximum of ${resolvedMaxCount} photo(s)`
|
|
90
|
+
: `You may only add ${remainingCount} more photo(s)`
|
|
91
|
+
|
|
92
|
+
Alert.alert('Selection Limit', message)
|
|
93
|
+
}, [remainingCount, resolvedMaxCount])
|
|
94
|
+
|
|
57
95
|
const showPermissionAlert = useCallback((type) => {
|
|
58
96
|
const platformMessage = Platform.select({
|
|
59
97
|
ios:
|
|
@@ -64,7 +102,7 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
64
102
|
|
|
65
103
|
Alert.alert(
|
|
66
104
|
'Permission Required',
|
|
67
|
-
`${platformMessage || 'Permission is required.'} You need to enable access to continue
|
|
105
|
+
`${platformMessage || 'Permission is required.'} You need to enable access to continue.`,
|
|
68
106
|
[
|
|
69
107
|
{ text: 'Cancel', style: 'cancel' },
|
|
70
108
|
{
|
|
@@ -174,64 +212,100 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
174
212
|
return
|
|
175
213
|
}
|
|
176
214
|
|
|
177
|
-
|
|
178
|
-
const resizedImage = await ImageResizer.createResizedImage(
|
|
179
|
-
imgArrRef.current[i].path,
|
|
180
|
-
2000,
|
|
181
|
-
2000,
|
|
182
|
-
'JPEG',
|
|
183
|
-
80
|
|
184
|
-
)
|
|
185
|
-
await Api.getApi().moveLocalFile(resizedImage.path, imgArrRef.current[i].path, true)
|
|
186
|
-
}
|
|
215
|
+
loaderHandler.showLoader('Processing Photos...')
|
|
187
216
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
217
|
+
try {
|
|
218
|
+
for (let i = 0; i < imgArrRef.current.length; i++) {
|
|
219
|
+
const resizedImage = await ImageResizer.createResizedImage(
|
|
220
|
+
imgArrRef.current[i].path,
|
|
221
|
+
2000,
|
|
222
|
+
2000,
|
|
223
|
+
'JPEG',
|
|
224
|
+
80
|
|
225
|
+
)
|
|
226
|
+
await Api.getApi().moveLocalFile(resizedImage.path, imgArrRef.current[i].path, true)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
await Promise.resolve(callback({
|
|
230
|
+
index,
|
|
231
|
+
cancelIndex,
|
|
232
|
+
imageArray: imgArrRef.current,
|
|
233
|
+
imageDescArray: imgDescArrRef.current,
|
|
234
|
+
imageValidArray: imgValidArrRef.current,
|
|
235
|
+
imageSource,
|
|
236
|
+
callbackData
|
|
237
|
+
}))
|
|
238
|
+
} finally {
|
|
239
|
+
loaderHandler.hideLoader()
|
|
240
|
+
}
|
|
197
241
|
}, [callback, callbackData])
|
|
198
242
|
|
|
199
243
|
const handleCameraModalDone = useCallback(async (images = []) => {
|
|
200
244
|
// Handle images returned from CameraModal
|
|
201
245
|
console.log('SWSI Camera modal done, images:', images)
|
|
202
246
|
|
|
247
|
+
const nextPayload =
|
|
248
|
+
images && images.length > 0 && pendingPickerIndex !== null && pendingCancelIndex !== null
|
|
249
|
+
? {
|
|
250
|
+
images,
|
|
251
|
+
pickerIndex: pendingPickerIndex,
|
|
252
|
+
cancelIndex: pendingCancelIndex
|
|
253
|
+
}
|
|
254
|
+
: null
|
|
255
|
+
|
|
203
256
|
setShowCameraModal(false)
|
|
257
|
+
resetActionSheet()
|
|
258
|
+
setPendingCameraModalPayload(nextPayload)
|
|
204
259
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
path: img.imageURI,
|
|
210
|
-
mime: 'image/jpeg',
|
|
211
|
-
width: 2000,
|
|
212
|
-
height: 2000
|
|
213
|
-
}
|
|
260
|
+
// Reset pending indices
|
|
261
|
+
setPendingPickerIndex(null)
|
|
262
|
+
setPendingCancelIndex(null)
|
|
263
|
+
}, [pendingCancelIndex, pendingPickerIndex, pickerDone, resetActionSheet])
|
|
214
264
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
265
|
+
const processPendingCameraModalPayload = useCallback(() => {
|
|
266
|
+
if (!pendingCameraModalPayload) {
|
|
267
|
+
return
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
imgArrRef.current = []
|
|
271
|
+
imgDescArrRef.current = []
|
|
272
|
+
imgValidArrRef.current = []
|
|
219
273
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
274
|
+
pendingCameraModalPayload.images.forEach((img) => {
|
|
275
|
+
const imageData = {
|
|
276
|
+
path: img.imageURI,
|
|
277
|
+
mime: 'image/jpeg',
|
|
278
|
+
width: 2000,
|
|
279
|
+
height: 2000
|
|
280
|
+
}
|
|
224
281
|
|
|
225
|
-
if (
|
|
226
|
-
|
|
227
|
-
pickerDone(pendingPickerIndex, pendingCancelIndex, imageSource)
|
|
282
|
+
if (img.depthMapURI) {
|
|
283
|
+
imageData.depthMapUri = img.depthMapURI
|
|
228
284
|
}
|
|
285
|
+
|
|
286
|
+
imgArrRef.current.push(imageData)
|
|
287
|
+
imgDescArrRef.current.push('')
|
|
288
|
+
imgValidArrRef.current.push(true)
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
const { pickerIndex, cancelIndex } = pendingCameraModalPayload
|
|
292
|
+
setPendingCameraModalPayload(null)
|
|
293
|
+
pickerDone(pickerIndex, cancelIndex, 'taken')
|
|
294
|
+
}, [pendingCameraModalPayload, pickerDone])
|
|
295
|
+
|
|
296
|
+
useEffect(() => {
|
|
297
|
+
if (showCameraModal || !pendingCameraModalPayload) {
|
|
298
|
+
return
|
|
229
299
|
}
|
|
230
300
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
301
|
+
const timeoutId = setTimeout(() => {
|
|
302
|
+
processPendingCameraModalPayload()
|
|
303
|
+
}, 350)
|
|
304
|
+
|
|
305
|
+
return () => {
|
|
306
|
+
clearTimeout(timeoutId)
|
|
307
|
+
}
|
|
308
|
+
}, [pendingCameraModalPayload, processPendingCameraModalPayload, showCameraModal])
|
|
235
309
|
|
|
236
310
|
const takeOnePicture = useCallback(async (
|
|
237
311
|
index,
|
|
@@ -240,7 +314,7 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
240
314
|
depth,
|
|
241
315
|
done
|
|
242
316
|
) => {
|
|
243
|
-
if (depth >
|
|
317
|
+
if (depth > remainingCount) {
|
|
244
318
|
done()
|
|
245
319
|
return
|
|
246
320
|
}
|
|
@@ -262,7 +336,7 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
262
336
|
} catch (e) {
|
|
263
337
|
done()
|
|
264
338
|
}
|
|
265
|
-
}, [
|
|
339
|
+
}, [remainingCount, requestCameraPermission, showPermissionAlert])
|
|
266
340
|
|
|
267
341
|
const usePicker = useCallback(async (index, cancelIndex) => {
|
|
268
342
|
// 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.
|
|
@@ -273,7 +347,7 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
273
347
|
|
|
274
348
|
const options = {
|
|
275
349
|
multiple: true,
|
|
276
|
-
maxFiles:
|
|
350
|
+
maxFiles: remainingCount, // ios only
|
|
277
351
|
cropping: false,
|
|
278
352
|
mediaType: 'photo'
|
|
279
353
|
}
|
|
@@ -290,6 +364,11 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
290
364
|
case takePhotoIndex:
|
|
291
365
|
console.log('User pressed take photo')
|
|
292
366
|
|
|
367
|
+
if (remainingCount <= 0) {
|
|
368
|
+
showSelectionLimitAlert()
|
|
369
|
+
break
|
|
370
|
+
}
|
|
371
|
+
|
|
293
372
|
// Use CameraModal on iOS 26+, native camera on Android
|
|
294
373
|
if (Platform.OS === 'ios' && parseInt(Platform.Version,10) >= 26) {
|
|
295
374
|
const hasPermission = await requestCameraPermission()
|
|
@@ -300,6 +379,7 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
300
379
|
await waitForAppToBeActive()
|
|
301
380
|
setPendingPickerIndex(index)
|
|
302
381
|
setPendingCancelIndex(cancelIndex)
|
|
382
|
+
resetActionSheet()
|
|
303
383
|
setShowCameraModal(true)
|
|
304
384
|
} else {
|
|
305
385
|
// Fall back to native camera (Android)
|
|
@@ -314,12 +394,17 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
314
394
|
}
|
|
315
395
|
break
|
|
316
396
|
case chooseLibIndex:
|
|
397
|
+
if (remainingCount <= 0) {
|
|
398
|
+
showSelectionLimitAlert()
|
|
399
|
+
break
|
|
400
|
+
}
|
|
401
|
+
|
|
317
402
|
checkAndOpenPicker(options, () =>
|
|
318
403
|
ImagePicker.openPicker(options)
|
|
319
404
|
.then((imagesOrImage) => {
|
|
320
405
|
const images = Array.isArray(imagesOrImage) ? imagesOrImage : [imagesOrImage]
|
|
321
|
-
if (images.length >
|
|
322
|
-
|
|
406
|
+
if (images.length > remainingCount) {
|
|
407
|
+
showSelectionLimitAlert()
|
|
323
408
|
} else {
|
|
324
409
|
imgArrRef.current = images
|
|
325
410
|
imgDescArrRef.current = Array(images.length).fill('')
|
|
@@ -340,7 +425,16 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
340
425
|
console.log('----usePicker User pressed custom button')
|
|
341
426
|
pickerDone(index, cancelIndex)
|
|
342
427
|
}
|
|
343
|
-
}, [
|
|
428
|
+
}, [
|
|
429
|
+
checkAndOpenPicker,
|
|
430
|
+
pickerDone,
|
|
431
|
+
remainingCount,
|
|
432
|
+
requestCameraPermission,
|
|
433
|
+
resetActionSheet,
|
|
434
|
+
showPermissionAlert,
|
|
435
|
+
showSelectionLimitAlert,
|
|
436
|
+
takeOnePicture
|
|
437
|
+
])
|
|
344
438
|
|
|
345
439
|
const showActionSheet = useCallback(() => {
|
|
346
440
|
if (actionSheetRef.current && typeof actionSheetRef.current.show === 'function') {
|
|
@@ -361,6 +455,7 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
361
455
|
return (
|
|
362
456
|
<View style={parentStyle}>
|
|
363
457
|
<ActionSheet
|
|
458
|
+
key={`multi-image-picker-action-sheet-${actionSheetInstanceKey}`}
|
|
364
459
|
ref={(node) => {
|
|
365
460
|
actionSheetRef.current = node
|
|
366
461
|
}}
|
|
@@ -382,7 +477,7 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
382
477
|
>
|
|
383
478
|
<CameraModal
|
|
384
479
|
onDone={handleCameraModalDone}
|
|
385
|
-
limit={
|
|
480
|
+
limit={remainingCount}
|
|
386
481
|
onCancel={() => handleCameraModalDone([])}
|
|
387
482
|
/>
|
|
388
483
|
</Modal>
|