@supportworks/multi-imagepicker 7.3.2 → 8.0.0
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/README.md +43 -219
- package/TestMIP.js +103 -0
- package/changes.md +3 -15
- package/index.js +3 -3
- package/package.json +1 -1
- package/src/components/ActionBar.js +3 -3
- package/src/components/AddImages.js +175 -119
- package/src/components/CameraModal.tsx +790 -0
- package/src/components/ImageDetailsModal.js +11 -20
- package/src/components/ImageIndicators.js +7 -5
- package/src/components/MultiImagePicker.js +76 -10
- package/src/components/MultiImagePicker_JOHNRIPPLE.js +348 -0
- package/src/components/MultiImagePicker_OLD.js +281 -0
- package/src/components/SelectedImages.js +5 -5
- package/src/components/SingleImage.js +6 -6
- package/src/components/UploadWarningModal.js +40 -50
- package/src/lib/MIPickerApi.js +14 -17
- package/src/components/AddImagesProvider.js +0 -269
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React, { useEffect, useState, useRef, useCallback } from 'react'
|
|
2
|
-
import { Modal, View, Image, StyleSheet, TouchableOpacity, Text, KeyboardAvoidingView, Platform } from 'react-native'
|
|
3
|
-
import { AppBarH8
|
|
2
|
+
import { Modal, SafeAreaView, View, Image, StyleSheet, TouchableOpacity, Text, KeyboardAvoidingView, Platform } from 'react-native'
|
|
3
|
+
import { AppBarH8 } from '@supportworks/core-controls'
|
|
4
4
|
import { colors, stdStyles } from '@supportworks/app-styler'
|
|
5
5
|
import { TextInputSWMD } from '@supportworks/swmaterialdesign'
|
|
6
6
|
|
|
@@ -42,7 +42,7 @@ const ImageDetailsModal = (
|
|
|
42
42
|
visible={visible}
|
|
43
43
|
onRequestClose={handleClose}
|
|
44
44
|
>
|
|
45
|
-
<
|
|
45
|
+
<SafeAreaView style={styles.Page}>
|
|
46
46
|
<AppBarH8
|
|
47
47
|
variant='regular'
|
|
48
48
|
title=''
|
|
@@ -50,22 +50,22 @@ const ImageDetailsModal = (
|
|
|
50
50
|
callback={handleClose}
|
|
51
51
|
rightContent={
|
|
52
52
|
<TouchableOpacity
|
|
53
|
-
hitSlop={{ top: 20, bottom: 20, left: 20, right: 20 }}
|
|
54
53
|
onPress={onDone}
|
|
55
54
|
disabled={!titleValidRef.current}
|
|
56
55
|
>
|
|
57
56
|
<Text style={[styles.DoneText, !titleValidRef.current ? styles.DoneTextDisabled : null]}>Done</Text>
|
|
58
57
|
</TouchableOpacity>
|
|
59
58
|
}
|
|
59
|
+
bottomShadow
|
|
60
60
|
/>
|
|
61
|
-
<KeyboardAvoidingView style={styles.KeyboardAvoidingView} behavior={Platform.OS === 'ios' ? 'padding' : 'height'} enabled>
|
|
62
61
|
<View style={styles.ImageContainer}>
|
|
63
62
|
<Image
|
|
64
63
|
source={{ uri: imageUri }}
|
|
65
|
-
|
|
64
|
+
resizeMode='cover'
|
|
66
65
|
style={[styles.Image]}
|
|
67
66
|
/>
|
|
68
67
|
</View>
|
|
68
|
+
<KeyboardAvoidingView style={styles.KeyboardAvoidingView} behavior={Platform.OS === 'ios' ? 'padding' : 'position'} enabled>
|
|
69
69
|
<View style={styles.InputContainer}>
|
|
70
70
|
<TextInputSWMD
|
|
71
71
|
key={`title-${inputResetKey}`}
|
|
@@ -96,7 +96,7 @@ const ImageDetailsModal = (
|
|
|
96
96
|
/>
|
|
97
97
|
</View>
|
|
98
98
|
</KeyboardAvoidingView>
|
|
99
|
-
</
|
|
99
|
+
</SafeAreaView>
|
|
100
100
|
</Modal>
|
|
101
101
|
)
|
|
102
102
|
}
|
|
@@ -106,30 +106,21 @@ const styles = StyleSheet.create({
|
|
|
106
106
|
flex: 1,
|
|
107
107
|
backgroundColor: colors.white
|
|
108
108
|
},
|
|
109
|
-
/*ScrollContent: {
|
|
110
|
-
flexGrow: 1,
|
|
111
|
-
alignItems: 'stretch',
|
|
112
|
-
paddingTop: 8,
|
|
113
|
-
paddingBottom: 24
|
|
114
|
-
},*/
|
|
115
109
|
ImageContainer: {
|
|
116
110
|
flex: 1,
|
|
117
|
-
|
|
118
|
-
marginBottom: 8,
|
|
119
|
-
borderRadius: 16,
|
|
120
|
-
backgroundColor: colors.lightgray,
|
|
111
|
+
margin: 16
|
|
121
112
|
},
|
|
122
113
|
Image: {
|
|
123
114
|
flex: 1,
|
|
124
|
-
borderRadius: 16
|
|
115
|
+
borderRadius: 16
|
|
125
116
|
},
|
|
126
117
|
KeyboardAvoidingView: {
|
|
127
118
|
flex: 1,
|
|
128
|
-
justifyContent: 'flex-
|
|
119
|
+
justifyContent: 'flex-end'
|
|
129
120
|
},
|
|
130
121
|
InputContainer: {
|
|
131
122
|
paddingHorizontal: 16,
|
|
132
|
-
paddingTop: 8
|
|
123
|
+
paddingTop: 8
|
|
133
124
|
},
|
|
134
125
|
DoneText: {
|
|
135
126
|
...stdStyles.S1,
|
|
@@ -11,10 +11,12 @@ import { BlurView } from '@react-native-community/blur'
|
|
|
11
11
|
const ImageIndicators = (
|
|
12
12
|
{
|
|
13
13
|
saveToDevice = false,
|
|
14
|
+
allowSaveToDevice = null,
|
|
15
|
+
allowShowToCustomer = null,
|
|
14
16
|
showToCust = false
|
|
15
17
|
}) => {
|
|
16
18
|
return (
|
|
17
|
-
(showToCust || saveToDevice) && (
|
|
19
|
+
((allowShowToCustomer && showToCust) || (allowSaveToDevice && saveToDevice)) && (
|
|
18
20
|
Platform.OS === 'ios'
|
|
19
21
|
? (
|
|
20
22
|
<BlurView
|
|
@@ -22,12 +24,12 @@ const ImageIndicators = (
|
|
|
22
24
|
style={[styles.ImageOverlayPill, { width: showToCust && saveToDevice ? 50 : 24 }]}
|
|
23
25
|
pointerEvents='none'
|
|
24
26
|
>
|
|
25
|
-
{showToCust && (
|
|
27
|
+
{allowShowToCustomer && showToCust && (
|
|
26
28
|
<View style={[styles.OverlayIconCircle, styles.OverlayIconCircleEye]}>
|
|
27
29
|
<Icons.IconEye_WithPad size={16} fill={colors.white} fillOpacity={1} />
|
|
28
30
|
</View>
|
|
29
31
|
)}
|
|
30
|
-
{saveToDevice && (
|
|
32
|
+
{allowSaveToDevice && saveToDevice && (
|
|
31
33
|
<View style={[styles.OverlayIconCircle]}>
|
|
32
34
|
<Icons.IconDownloadDone2 size={16} fill={colors.white} fillOpacity={1} />
|
|
33
35
|
</View>
|
|
@@ -40,12 +42,12 @@ const ImageIndicators = (
|
|
|
40
42
|
backgroundColor: 'rgba(0,0,0,0.35)'
|
|
41
43
|
}]} pointerEvents='none'
|
|
42
44
|
>
|
|
43
|
-
{showToCust && (
|
|
45
|
+
{allowShowToCustomer && showToCust && (
|
|
44
46
|
<View style={[styles.OverlayIconCircle, styles.OverlayIconCircleEye]}>
|
|
45
47
|
<Icons.IconEye_WithPad size={16} fill={colors.white} fillOpacity={1} />
|
|
46
48
|
</View>
|
|
47
49
|
)}
|
|
48
|
-
{saveToDevice && (
|
|
50
|
+
{allowSaveToDevice && saveToDevice && (
|
|
49
51
|
<View style={[styles.OverlayIconCircle]}>
|
|
50
52
|
<Icons.IconDownloadDone2 size={16} fill={colors.white} fillOpacity={1} />
|
|
51
53
|
</View>
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import React, { useCallback, useMemo, useRef, forwardRef, useImperativeHandle } from 'react'
|
|
1
|
+
import React, { useCallback, useMemo, useRef, forwardRef, useImperativeHandle, useState } from 'react'
|
|
2
2
|
import {
|
|
3
3
|
View,
|
|
4
4
|
TouchableOpacity,
|
|
5
5
|
Alert,
|
|
6
6
|
Platform,
|
|
7
|
-
Linking
|
|
7
|
+
Linking,
|
|
8
|
+
Modal
|
|
8
9
|
} from 'react-native'
|
|
9
10
|
import ActionSheet from '@alessiocancian/react-native-actionsheet'
|
|
10
11
|
import ImagePicker from 'react-native-image-crop-picker'
|
|
11
12
|
import ImageResizer from '@bam.tech/react-native-image-resizer'
|
|
12
13
|
import { Api } from '@supportworks/api-core'
|
|
13
14
|
import { check, request, PERMISSIONS, RESULTS, openSettings } from 'react-native-permissions'
|
|
15
|
+
import CameraModal from './CameraModal'
|
|
14
16
|
|
|
15
17
|
const MultiImagePicker = forwardRef((props, ref) => {
|
|
16
18
|
// Destructure known props and collect any remaining props to forward to the TouchableOpacity.
|
|
@@ -27,6 +29,10 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
27
29
|
...touchableProps // any other props passed into MultiImagePicker will be spread onto the TouchableOpacity below
|
|
28
30
|
} = props
|
|
29
31
|
|
|
32
|
+
const [showCameraModal, setShowCameraModal] = useState(false)
|
|
33
|
+
const [pendingPickerIndex, setPendingPickerIndex] = useState(null)
|
|
34
|
+
const [pendingCancelIndex, setPendingCancelIndex] = useState(null)
|
|
35
|
+
|
|
30
36
|
const imgArrRef = useRef([]) // temp array for holding selected images to attach, before title & desc updated
|
|
31
37
|
const imgDescArrRef = useRef([]) // temp array for holding descriptions of selected images, before saved to project
|
|
32
38
|
const imgValidArrRef = useRef([]) // temp array for holding boolean value specifying whether each image title is valid or not
|
|
@@ -174,6 +180,43 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
174
180
|
})
|
|
175
181
|
}, [callback, callbackData])
|
|
176
182
|
|
|
183
|
+
const handleCameraModalDone = useCallback(async (images = []) => {
|
|
184
|
+
// Handle images returned from CameraModal
|
|
185
|
+
console.log('SWSI Camera modal done, images:', images)
|
|
186
|
+
|
|
187
|
+
setShowCameraModal(false)
|
|
188
|
+
|
|
189
|
+
if (images && images.length > 0) {
|
|
190
|
+
// Convert CameraModal format to ImagePicker format
|
|
191
|
+
images.forEach((img) => {
|
|
192
|
+
const imageData = {
|
|
193
|
+
path: img.imageURI,
|
|
194
|
+
mime: 'image/jpeg',
|
|
195
|
+
width: 2000,
|
|
196
|
+
height: 2000
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Store depth map URI if available (can be used by calling code)
|
|
200
|
+
if (img.depthMapURI) {
|
|
201
|
+
imageData.depthMapUri = img.depthMapURI
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
imgArrRef.current.push(imageData)
|
|
205
|
+
imgDescArrRef.current.push('')
|
|
206
|
+
imgValidArrRef.current.push(true)
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
if (pendingPickerIndex !== null && pendingCancelIndex !== null) {
|
|
210
|
+
const imageSource = 'taken'
|
|
211
|
+
pickerDone(pendingPickerIndex, pendingCancelIndex, imageSource)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Reset pending indices
|
|
216
|
+
setPendingPickerIndex(null)
|
|
217
|
+
setPendingCancelIndex(null)
|
|
218
|
+
}, [pendingCancelIndex, pendingPickerIndex, pickerDone])
|
|
219
|
+
|
|
177
220
|
const takeOnePicture = useCallback(async (
|
|
178
221
|
index,
|
|
179
222
|
cancelIndex,
|
|
@@ -230,14 +273,23 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
230
273
|
switch (index) {
|
|
231
274
|
case takePhotoIndex:
|
|
232
275
|
console.log('User pressed take photo')
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
276
|
+
|
|
277
|
+
// Use CameraModal on iOS, native camera on Android
|
|
278
|
+
if (Platform.OS === 'ios') {
|
|
279
|
+
setPendingPickerIndex(index)
|
|
280
|
+
setPendingCancelIndex(cancelIndex)
|
|
281
|
+
setShowCameraModal(true)
|
|
282
|
+
} else {
|
|
283
|
+
// Fall back to native camera (Android)
|
|
284
|
+
checkAndOpenPicker(options, () =>
|
|
285
|
+
takeOnePicture(index, cancelIndex, options, 1, () => {
|
|
286
|
+
if (imgArrRef.current.length > 0) {
|
|
287
|
+
imageSource = 'taken'
|
|
288
|
+
pickerDone(index, cancelIndex, imageSource)
|
|
289
|
+
}
|
|
290
|
+
})
|
|
291
|
+
)
|
|
292
|
+
}
|
|
241
293
|
break
|
|
242
294
|
case chooseLibIndex:
|
|
243
295
|
checkAndOpenPicker(options, () =>
|
|
@@ -298,6 +350,20 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
298
350
|
<TouchableOpacity {...touchableProps} onPress={showActionSheet}>
|
|
299
351
|
{children}
|
|
300
352
|
</TouchableOpacity>
|
|
353
|
+
{Platform.OS === 'ios' && showCameraModal && (
|
|
354
|
+
<Modal
|
|
355
|
+
visible={showCameraModal}
|
|
356
|
+
animationType='slide'
|
|
357
|
+
presentationStyle='fullScreen'
|
|
358
|
+
onRequestClose={() => handleCameraModalDone([])}
|
|
359
|
+
>
|
|
360
|
+
<CameraModal
|
|
361
|
+
onDone={handleCameraModalDone}
|
|
362
|
+
limit={maxCount}
|
|
363
|
+
onCancel={() => handleCameraModalDone([])}
|
|
364
|
+
/>
|
|
365
|
+
</Modal>
|
|
366
|
+
)}
|
|
301
367
|
</View>
|
|
302
368
|
)
|
|
303
369
|
})
|
|
@@ -0,0 +1,348 @@
|
|
|
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
|