@supportworks/multi-imagepicker 7.3.1 → 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.
@@ -0,0 +1,281 @@
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
@@ -1,11 +1,12 @@
1
1
  import React, { useCallback, useMemo, useEffect, useState } from 'react'
2
2
  import {
3
3
  View,
4
- Modal
4
+ Modal,
5
+ SafeAreaView
5
6
  } from 'react-native'
6
7
  import SingleImage from './SingleImage'
7
8
  import { Analytics } from '@supportworks/analytics'
8
- import { MediaPlayer, ScreenSafeAreaView } from '@supportworks/core-controls'
9
+ import { MediaPlayer } from '@supportworks/core-controls'
9
10
  import { colors } from '@supportworks/app-styler'
10
11
  import { Api } from '@supportworks/api-core'
11
12
 
@@ -124,11 +125,10 @@ const SelectedImages = (
124
125
  onRequestClose={closeMedia}
125
126
  visible
126
127
  >
127
- <ScreenSafeAreaView style={{ flex: 1, backgroundColor: colors.black }} includeProvider>
128
+ <SafeAreaView style={{ flex: 1, backgroundColor: colors.black }}>
128
129
  <MediaPlayer
129
130
  mediaId={mediaIndex}
130
131
  backgroundColor={colors.black}
131
- colorAppBar={'white'}
132
132
  resourceList={resourceList}
133
133
  initialResource={mediaIndex}
134
134
  annotationAllowed={annotationAllowed}
@@ -137,7 +137,7 @@ const SelectedImages = (
137
137
  fullPath={Api.getApi().fullPath}
138
138
  fullImagePath={Api.getApi().fullImagePath}
139
139
  />
140
- </ScreenSafeAreaView>
140
+ </SafeAreaView>
141
141
  </Modal>
142
142
  )}
143
143
  </>
@@ -3,6 +3,7 @@ import {
3
3
  Text,
4
4
  View,
5
5
  Modal,
6
+ SafeAreaView,
6
7
  TouchableOpacity,
7
8
  StyleSheet,
8
9
  Image, Dimensions
@@ -16,7 +17,6 @@ import { Analytics } from '@supportworks/analytics'
16
17
  import ActionBar from './ActionBar'
17
18
  import ImageDetailsModal from './ImageDetailsModal'
18
19
  import ImageIndicators from './ImageIndicators'
19
- import { ScreenSafeAreaView } from '@supportworks/core-controls'
20
20
 
21
21
  const SingleImage = (
22
22
  {
@@ -111,7 +111,7 @@ const SingleImage = (
111
111
  }
112
112
 
113
113
  if (newValue !== initValue) {
114
- //console.log("SingleImage onDoneEditingInput: field='" + field + "' changed from '" + initValue + "' to '" + newValue + "'")
114
+ console.log("*** RC: SingleImage onDoneEditingInput: field='" + field + "' changed from '" + initValue + "' to '" + newValue + "'")
115
115
  Analytics.logEvent('mip_singleImage_' + aType, {
116
116
  source_page: 'MultiImagePicker',
117
117
  initValue,
@@ -194,9 +194,9 @@ const SingleImage = (
194
194
  if (showCanvas) {
195
195
  return (
196
196
  <Modal presentationStyle='fullScreen' animationType='slide'>
197
- <ScreenSafeAreaView style={styles.Page} includeProvider>
197
+ <SafeAreaView style={styles.Page}>
198
198
  <Canvas settings={annotationJson.settings} history={annotationJson.history} base64image={'data:image/jpeg;base64,' + canvasData} onSave={onSaveCanvas} onClose={onCloseCanvas} />
199
- </ScreenSafeAreaView>
199
+ </SafeAreaView>
200
200
  </Modal>
201
201
  )
202
202
  }
@@ -210,7 +210,7 @@ const SingleImage = (
210
210
  <Image
211
211
  resizeMode='cover'
212
212
  style={[styles.Image, { width: '100%', height: '100%' }]}
213
- source={{ uri: Api.getApi().fullImagePath(imagePath) + '?x=' + version + '-' + (cacheVersion || 0) }}
213
+ source={{ uri: 'file://' + imagePath + '?x=' + version + '-' + (cacheVersion || 0) }}
214
214
  />
215
215
  <ImageIndicators
216
216
  allowShowToCustomer={allowShowToCustomer}
@@ -253,7 +253,7 @@ const SingleImage = (
253
253
  <ImageDetailsModal
254
254
  visible={showDetailsModal}
255
255
  onClose={closeDetails}
256
- imageUri={Api.getApi().fullImagePath(imagePath) + '?x=' + version + '-' + (cacheVersion || 0)}
256
+ imageUri={'file://' + imagePath + '?x=' + version + '-' + (cacheVersion || 0)}
257
257
  initialTitle={title}
258
258
  initialDesc={desc}
259
259
  titleEditable={titleEditable}
@@ -1,5 +1,5 @@
1
1
  import React, { useCallback, useState } from 'react'
2
- import { View, StyleSheet, Text, Platform } from 'react-native'
2
+ import { Modal, View, StyleSheet, Text } from 'react-native'
3
3
  import { colors, stdStyles } from '@supportworks/app-styler'
4
4
  import { CheckBox, CommonButton } from '@supportworks/core-controls'
5
5
  import { Icons } from '@supportworks/icon-library'
@@ -9,62 +9,59 @@ const UploadWarningModal = (
9
9
  visible = false,
10
10
  onClose = null
11
11
  }) => {
12
- const [skipInfoDialog, setSkipInfoDialog] = useState(true)
12
+ const [skipInfoDialog, setSkipInfoDialog] = useState(false)
13
13
 
14
14
  const handleClose = useCallback(() => {
15
15
  if (onClose) onClose(skipInfoDialog)
16
16
  }, [onClose, skipInfoDialog])
17
17
 
18
- if (!visible) {
19
- return null
20
- }
21
-
22
18
  return (
23
- <View style={styles.ModalView}>
24
- <View style={styles.Backdrop} />
25
- <View style={styles.InfoDialogContent}>
26
- <View style={styles.InfoDialogIconRow}>
27
- <Icons.IconPencilLock fill={colors.brandPrimary} fillOpacity={1} />
28
- </View>
29
- <View style={styles.InfoDialogTitleRow}>
30
- <Text style={styles.TitleText}>
31
- Captions can't be updated in CRM after upload
19
+ <Modal
20
+ animationType='fade'
21
+ transparent
22
+ visible={visible}
23
+ onRequestClose={handleClose}
24
+ >
25
+ <View style={styles.ModalView}>
26
+ <View style={styles.InfoDialogContent}>
27
+ <View style={styles.InfoDialogIconRow}>
28
+ <Icons.IconPencilLock fill={colors.brandPrimary} fillOpacity={1} />
29
+ </View>
30
+ <View style={styles.InfoDialogTitleRow}>
31
+ <Text style={styles.TitleText}>
32
+ Captions can't be updated in CRM after upload
33
+ </Text>
34
+ </View>
35
+ <Text style={stdStyles.Body2}>
36
+ Photo captions can be edited now, but not after submitting to the CRM. Please review before uploading.
32
37
  </Text>
33
- </View>
34
- <Text style={stdStyles.Body2}>
35
- Photo captions can be edited now, but not after submitting to the CRM. Please review before uploading.
36
- </Text>
37
- <View style={styles.InfoDialogCheckboxRow}>
38
- <CheckBox
39
- label="Don't show this message again"
40
- checked={!!skipInfoDialog}
41
- onChange={(newVal) => setSkipInfoDialog(newVal)}
42
- />
43
- </View>
44
- <View style={styles.InfoDialogButtonRow}>
45
- <CommonButton
46
- variant='filled'
47
- size='medium'
48
- label='Got it'
49
- fullWidth
50
- onPress={handleClose}
51
- />
38
+ <View style={styles.InfoDialogCheckboxRow}>
39
+ <CheckBox
40
+ label="Don't show this message again"
41
+ checked={!!skipInfoDialog}
42
+ onChange={(newVal) => setSkipInfoDialog(newVal)}
43
+ />
44
+ </View>
45
+ <View style={styles.InfoDialogButtonRow}>
46
+ <CommonButton
47
+ variant='filled'
48
+ size='medium'
49
+ label='Got it'
50
+ fullWidth
51
+ onPress={handleClose}
52
+ />
53
+ </View>
52
54
  </View>
53
55
  </View>
54
- </View>
56
+ </Modal>
55
57
  )
56
58
  }
57
59
 
58
60
  const styles = StyleSheet.create({
59
61
  ModalView: {
60
- ...StyleSheet.absoluteFillObject,
62
+ flex: 1,
61
63
  justifyContent: 'center',
62
- alignItems: 'center',
63
- zIndex: 10
64
- },
65
- Backdrop: {
66
- ...StyleSheet.absoluteFillObject,
67
- backgroundColor: colors.black36
64
+ alignItems: 'center'
68
65
  },
69
66
  InfoDialogContent: {
70
67
  backgroundColor: 'white',
@@ -80,14 +77,7 @@ const styles = StyleSheet.create({
80
77
  },
81
78
  elevation: 8,
82
79
  maxWidth: '80%',
83
- width: 334,
84
-
85
- ...Platform.select({
86
- android: {
87
- borderWidth: 2,
88
- borderColor: colors.black05,
89
- }
90
- })
80
+ width: 334
91
81
  },
92
82
  InfoDialogIconRow: {
93
83
  alignItems: 'center',
@@ -68,22 +68,19 @@ export default class MIPickerApi {
68
68
  }
69
69
 
70
70
  async getDefaultShowToCustomer () {
71
- return true
72
-
73
- /*
74
- console.log('in getDefaultShowToCustomer')
75
- if ((this._defaultShowToCustomer !== null) && (this._defaultShowToCustomer !== undefined)) {
76
- return this._defaultShowToCustomer
77
- }
78
- console.log('in getDefaultShowToCustomer, trying to get value from AsyncStorage')
79
- // not a value yet, so look in AsyncStorage
80
- const retVal = await AsyncStorage.getItem('@MIP_ShowToCustomer')
81
- if (retVal === 'N') {
82
- console.log('getDefaultShowToCustomer: return false')
83
- return false
84
- } else {
85
- console.log('getDefaultShowToCustomer: return true')
86
- return true
87
- }*/
71
+ console.log('in getDefaultShowToCustomer')
72
+ if ((this._defaultShowToCustomer !== null) && (this._defaultShowToCustomer !== undefined)) {
73
+ return this._defaultShowToCustomer
74
+ }
75
+ console.log('in getDefaultShowToCustomer, trying to get value from AsyncStorage')
76
+ // not a value yet, so look in AsyncStorage
77
+ const retVal = await AsyncStorage.getItem('@MIP_ShowToCustomer')
78
+ if (retVal === 'N') {
79
+ console.log('getDefaultShowToCustomer: return false')
80
+ return false
81
+ } else {
82
+ console.log('getDefaultShowToCustomer: return true')
83
+ return true
84
+ }
88
85
  }
89
86
  }