-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
executable file
·96 lines (61 loc) · 2.15 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Alert, InteractionManager, NativeModules, Platform } from 'react-native';
import { ImagePickerOptions } from './types';
const { imagePicker } = NativeModules;
const open = async (options: ImagePickerOptions) => {
return new Promise(async (resolve, reject) => {
try {
if (!imagePicker || !imagePicker?.openImagePicker) throw new Error('Something went wrong')
if (Platform.OS == "android") {
imagePicker.openImagePicker(options, response => {
resolve(response)
})
} else {
let response = await imagePicker.openImagePicker(options)
resolve(response)
}
} catch (error) {
reject(error)
}
})
}
export const openPicker = async (options: ImagePickerOptions) => {
return new Promise((resolve, reject) => {
Alert.alert('Image Picker', 'Please select your desired source', [
{
text: 'Cancel',
style: 'destructive',
onPress: () => InteractionManager.runAfterInteractions(() => {
reject('User cancelled')
})
},
{
text: 'Camera',
onPress: () => InteractionManager.runAfterInteractions(async () => {
try {
const response = await open({ ...options, selection: 'camera' })
resolve(response)
} catch (error) {
reject(error)
}
})
},
{
text: 'Gallery',
onPress: () => InteractionManager.runAfterInteractions(async () => {
try {
const response = await open({ ...options, selection: 'gallery' })
resolve(response)
} catch (error) {
reject(error)
}
})
},
], {
cancelable: false,
})
})
}
export default {
open: open,
openPicker: openPicker
};