@supportworks/multi-imagepicker 8.3.3 → 8.3.5
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
CHANGED
|
@@ -13,6 +13,8 @@ import {
|
|
|
13
13
|
} from '@supportworks/camera'
|
|
14
14
|
import React, { useRef, useState } from 'react'
|
|
15
15
|
import {
|
|
16
|
+
AppState,
|
|
17
|
+
ActivityIndicator,
|
|
16
18
|
View,
|
|
17
19
|
Text,
|
|
18
20
|
StyleSheet,
|
|
@@ -22,10 +24,12 @@ import {
|
|
|
22
24
|
Image,
|
|
23
25
|
ScrollView,
|
|
24
26
|
Animated,
|
|
25
|
-
PanResponder
|
|
27
|
+
PanResponder,
|
|
28
|
+
Platform
|
|
26
29
|
} from 'react-native'
|
|
27
30
|
import Svg, { Circle, Path } from 'react-native-svg'
|
|
28
31
|
import { useSafeAreaInsets } from 'react-native-safe-area-context'
|
|
32
|
+
import { check, openSettings, PERMISSIONS, request, RESULTS } from 'react-native-permissions'
|
|
29
33
|
|
|
30
34
|
// Types
|
|
31
35
|
interface CapturedImage {
|
|
@@ -55,6 +59,22 @@ const ZOOM_STEP = 0.1
|
|
|
55
59
|
const ZOOM_RANGE = MAX_ZOOM - MIN_ZOOM
|
|
56
60
|
const DEFAULT_ZOOM = 1
|
|
57
61
|
const FOCUS_RING_SIZE = 72
|
|
62
|
+
type CameraPermissionState = 'checking' | 'granted' | 'denied'
|
|
63
|
+
|
|
64
|
+
const waitForAppToBeActive = async () => {
|
|
65
|
+
if (AppState.currentState === 'active') {
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
await new Promise<void>((resolve) => {
|
|
70
|
+
const subscription = AppState.addEventListener('change', (nextState) => {
|
|
71
|
+
if (nextState === 'active') {
|
|
72
|
+
subscription.remove()
|
|
73
|
+
resolve()
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
})
|
|
77
|
+
}
|
|
58
78
|
|
|
59
79
|
const clamp = (value: number, min: number, max: number) => {
|
|
60
80
|
return Math.min(Math.max(value, min), max)
|
|
@@ -142,6 +162,7 @@ const CameraModal: React.FC<CameraTestModalProps> = ({ onDone, onCancel, cameraR
|
|
|
142
162
|
const [isGalleryModalVisible, setIsGalleryModalVisible] = useState<boolean>(false)
|
|
143
163
|
const [isDoneButtonDisabled, setIsDoneButtonDisabled] = useState<boolean>(false)
|
|
144
164
|
const [focusPoint, setFocusPoint] = useState<{ x: number; y: number } | null>(null)
|
|
165
|
+
const [cameraPermissionState, setCameraPermissionState] = useState<CameraPermissionState>('checking')
|
|
145
166
|
const knobTop = useRef(new Animated.Value(getTopPositionForZoom(DEFAULT_ZOOM))).current
|
|
146
167
|
const focusAnim = useRef(new Animated.Value(0)).current
|
|
147
168
|
const dragStartOffset = useRef(0)
|
|
@@ -150,6 +171,53 @@ const CameraModal: React.FC<CameraTestModalProps> = ({ onDone, onCancel, cameraR
|
|
|
150
171
|
const isZoomAvailable = isFrontCamera ? canZoomByPosition.front : canZoomByPosition.back
|
|
151
172
|
const sliderDisabled = isFrontCamera || !isZoomAvailable
|
|
152
173
|
|
|
174
|
+
React.useEffect(() => {
|
|
175
|
+
let isCancelled = false
|
|
176
|
+
|
|
177
|
+
const ensureCameraPermission = async () => {
|
|
178
|
+
const permission = Platform.select({
|
|
179
|
+
ios: PERMISSIONS.IOS.CAMERA,
|
|
180
|
+
android: PERMISSIONS.ANDROID.CAMERA
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
if (!permission) {
|
|
184
|
+
if (!isCancelled) {
|
|
185
|
+
setCameraPermissionState('granted')
|
|
186
|
+
}
|
|
187
|
+
return
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
const currentStatus = await check(permission)
|
|
192
|
+
let nextStatus = currentStatus
|
|
193
|
+
|
|
194
|
+
if (currentStatus === RESULTS.DENIED) {
|
|
195
|
+
nextStatus = await request(permission)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (nextStatus === RESULTS.GRANTED) {
|
|
199
|
+
await waitForAppToBeActive()
|
|
200
|
+
if (!isCancelled) {
|
|
201
|
+
setCameraPermissionState('granted')
|
|
202
|
+
}
|
|
203
|
+
return
|
|
204
|
+
}
|
|
205
|
+
} catch (error) {
|
|
206
|
+
console.log('CAMERA: Failed to resolve camera permission', error)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (!isCancelled) {
|
|
210
|
+
setCameraPermissionState('denied')
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
ensureCameraPermission()
|
|
215
|
+
|
|
216
|
+
return () => {
|
|
217
|
+
isCancelled = true
|
|
218
|
+
}
|
|
219
|
+
}, [])
|
|
220
|
+
|
|
153
221
|
const handleStatusChange = (event: { nativeEvent: OnStatusChangeEventPayload }) => {
|
|
154
222
|
const newStatus = event?.nativeEvent?.status || 'Unknown'
|
|
155
223
|
// console.log('CAMERA: Status changed to:', newStatus)
|
|
@@ -390,6 +458,59 @@ const CameraModal: React.FC<CameraTestModalProps> = ({ onDone, onCancel, cameraR
|
|
|
390
458
|
})
|
|
391
459
|
}
|
|
392
460
|
|
|
461
|
+
if (cameraPermissionState !== 'granted') {
|
|
462
|
+
const isCheckingPermission = cameraPermissionState === 'checking'
|
|
463
|
+
|
|
464
|
+
return (
|
|
465
|
+
<View style={styles.container}>
|
|
466
|
+
<View
|
|
467
|
+
style={[
|
|
468
|
+
styles.topControls,
|
|
469
|
+
{ paddingTop: insets.top + 10, paddingLeft: 20 + insets.left, paddingRight: 20 + insets.right }
|
|
470
|
+
]}
|
|
471
|
+
>
|
|
472
|
+
<View style={styles.topLeft} />
|
|
473
|
+
<View style={styles.topRight}>
|
|
474
|
+
<TouchableOpacity
|
|
475
|
+
style={styles.topTextButton}
|
|
476
|
+
hitSlop={{ top: 12, bottom: 12, left: 12, right: 12 }}
|
|
477
|
+
onPress={onCancel}
|
|
478
|
+
>
|
|
479
|
+
<Icons.IconUndo width={18} height={18} fill={colors.white} />
|
|
480
|
+
<Text style={styles.topTextButtonLabel}>Cancel</Text>
|
|
481
|
+
</TouchableOpacity>
|
|
482
|
+
</View>
|
|
483
|
+
</View>
|
|
484
|
+
|
|
485
|
+
<View style={styles.permissionStateContainer}>
|
|
486
|
+
<View style={styles.permissionCard}>
|
|
487
|
+
{isCheckingPermission ? <ActivityIndicator size='large' color={colors.white} /> : null}
|
|
488
|
+
<Text style={styles.permissionTitle}>
|
|
489
|
+
{isCheckingPermission ? 'Requesting Camera Access' : 'Camera Access Required'}
|
|
490
|
+
</Text>
|
|
491
|
+
<Text style={styles.permissionMessage}>
|
|
492
|
+
{isCheckingPermission
|
|
493
|
+
? 'Waiting for camera permission so the preview can start.'
|
|
494
|
+
: 'Camera access is required before the preview can be shown.'}
|
|
495
|
+
</Text>
|
|
496
|
+
{!isCheckingPermission ? (
|
|
497
|
+
<TouchableOpacity
|
|
498
|
+
style={styles.permissionButton}
|
|
499
|
+
onPress={() => {
|
|
500
|
+
openSettings().catch(() => {
|
|
501
|
+
Alert.alert('Error', 'Unable to open Settings')
|
|
502
|
+
})
|
|
503
|
+
}}
|
|
504
|
+
>
|
|
505
|
+
<Text style={styles.permissionButtonText}>Open Settings</Text>
|
|
506
|
+
</TouchableOpacity>
|
|
507
|
+
) : null}
|
|
508
|
+
</View>
|
|
509
|
+
</View>
|
|
510
|
+
</View>
|
|
511
|
+
)
|
|
512
|
+
}
|
|
513
|
+
|
|
393
514
|
return (
|
|
394
515
|
<View style={styles.container}>
|
|
395
516
|
{/* Top Controls */}
|
|
@@ -602,6 +723,50 @@ const styles = StyleSheet.create({
|
|
|
602
723
|
flex: 1,
|
|
603
724
|
padding: 10
|
|
604
725
|
},
|
|
726
|
+
permissionStateContainer: {
|
|
727
|
+
flex: 1,
|
|
728
|
+
alignItems: 'center',
|
|
729
|
+
justifyContent: 'center',
|
|
730
|
+
paddingHorizontal: 24
|
|
731
|
+
},
|
|
732
|
+
permissionCard: {
|
|
733
|
+
width: '100%',
|
|
734
|
+
maxWidth: 360,
|
|
735
|
+
borderRadius: 18,
|
|
736
|
+
backgroundColor: colors.black60,
|
|
737
|
+
paddingHorizontal: 24,
|
|
738
|
+
paddingVertical: 28,
|
|
739
|
+
alignItems: 'center'
|
|
740
|
+
},
|
|
741
|
+
permissionTitle: {
|
|
742
|
+
color: colors.white,
|
|
743
|
+
fontSize: 20,
|
|
744
|
+
fontWeight: 'bold',
|
|
745
|
+
marginTop: 16,
|
|
746
|
+
textAlign: 'center'
|
|
747
|
+
},
|
|
748
|
+
permissionMessage: {
|
|
749
|
+
color: colors.white,
|
|
750
|
+
fontSize: 15,
|
|
751
|
+
lineHeight: 22,
|
|
752
|
+
textAlign: 'center',
|
|
753
|
+
marginTop: 10
|
|
754
|
+
},
|
|
755
|
+
permissionButton: {
|
|
756
|
+
marginTop: 20,
|
|
757
|
+
minWidth: 160,
|
|
758
|
+
height: 44,
|
|
759
|
+
borderRadius: 22,
|
|
760
|
+
backgroundColor: colors.blue,
|
|
761
|
+
alignItems: 'center',
|
|
762
|
+
justifyContent: 'center',
|
|
763
|
+
paddingHorizontal: 18
|
|
764
|
+
},
|
|
765
|
+
permissionButtonText: {
|
|
766
|
+
color: colors.white,
|
|
767
|
+
fontSize: 15,
|
|
768
|
+
fontWeight: 'bold'
|
|
769
|
+
},
|
|
605
770
|
topControls: {
|
|
606
771
|
position: 'absolute',
|
|
607
772
|
top: 0,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import React, { useCallback, useMemo, useRef, forwardRef, useImperativeHandle, useState } from 'react'
|
|
2
2
|
import {
|
|
3
|
+
AppState,
|
|
3
4
|
View,
|
|
4
5
|
TouchableOpacity,
|
|
5
6
|
Alert,
|
|
@@ -14,6 +15,21 @@ import { Api } from '@supportworks/api-core'
|
|
|
14
15
|
import { check, request, PERMISSIONS, RESULTS, openSettings } from 'react-native-permissions'
|
|
15
16
|
import CameraModal from './CameraModal'
|
|
16
17
|
|
|
18
|
+
const waitForAppToBeActive = async () => {
|
|
19
|
+
if (AppState.currentState === 'active') {
|
|
20
|
+
return
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
await new Promise((resolve) => {
|
|
24
|
+
const subscription = AppState.addEventListener('change', (nextState) => {
|
|
25
|
+
if (nextState === 'active') {
|
|
26
|
+
subscription.remove()
|
|
27
|
+
resolve()
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
17
33
|
const MultiImagePicker = forwardRef((props, ref) => {
|
|
18
34
|
// Destructure known props and collect any remaining props to forward to the TouchableOpacity.
|
|
19
35
|
const {
|
|
@@ -248,7 +264,7 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
248
264
|
}
|
|
249
265
|
}, [maxCount, requestCameraPermission, showPermissionAlert])
|
|
250
266
|
|
|
251
|
-
const usePicker = useCallback((index, cancelIndex) => {
|
|
267
|
+
const usePicker = useCallback(async (index, cancelIndex) => {
|
|
252
268
|
// 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.
|
|
253
269
|
// Custom buttons can be sent using customButtons prop.
|
|
254
270
|
// results - sets cRef.imgArr to an array of image info that the picker library provided
|
|
@@ -276,6 +292,12 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
276
292
|
|
|
277
293
|
// Use CameraModal on iOS 26+, native camera on Android
|
|
278
294
|
if (Platform.OS === 'ios' && parseInt(Platform.Version,10) >= 26) {
|
|
295
|
+
const hasPermission = await requestCameraPermission()
|
|
296
|
+
if (!hasPermission) {
|
|
297
|
+
showPermissionAlert('Camera')
|
|
298
|
+
break
|
|
299
|
+
}
|
|
300
|
+
await waitForAppToBeActive()
|
|
279
301
|
setPendingPickerIndex(index)
|
|
280
302
|
setPendingCancelIndex(cancelIndex)
|
|
281
303
|
setShowCameraModal(true)
|
|
@@ -318,7 +340,7 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
318
340
|
console.log('----usePicker User pressed custom button')
|
|
319
341
|
pickerDone(index, cancelIndex)
|
|
320
342
|
}
|
|
321
|
-
}, [checkAndOpenPicker, maxCount, pickerDone, takeOnePicture])
|
|
343
|
+
}, [checkAndOpenPicker, maxCount, pickerDone, requestCameraPermission, showPermissionAlert, takeOnePicture])
|
|
322
344
|
|
|
323
345
|
const showActionSheet = useCallback(() => {
|
|
324
346
|
if (actionSheetRef.current && typeof actionSheetRef.current.show === 'function') {
|