forked from FaridSafi/react-native-gifted-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediaUtils.js
65 lines (57 loc) · 1.73 KB
/
mediaUtils.js
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
import { Linking } from 'expo'
import * as Location from 'expo-location'
import * as Permissions from 'expo-permissions'
import * as ImagePicker from 'expo-image-picker'
import { Alert } from 'react-native'
export default async function getPermissionAsync(permission) {
const { status } = await Permissions.askAsync(permission)
if (status !== 'granted') {
const permissionName = permission.toLowerCase().replace('_', ' ')
Alert.alert(
'Cannot be done 😞',
`If you would like to use this feature, you'll need to enable the ${permissionName} permission in your phone settings.`,
[
{
text: "Let's go!",
onPress: () => Linking.openURL('app-settings:'),
},
{ text: 'Nevermind', onPress: () => {}, style: 'cancel' },
],
{ cancelable: true },
)
return false
}
return true
}
export async function getLocationAsync(onSend) {
if (await getPermissionAsync(Permissions.LOCATION)) {
const location = await Location.getCurrentPositionAsync({})
if (location) {
onSend([{ location: location.coords }])
}
}
}
export async function pickImageAsync(onSend) {
if (await getPermissionAsync(Permissions.CAMERA_ROLL)) {
const result = await ImagePicker.launchImageLibraryAsync({
allowsEditing: true,
aspect: [4, 3],
})
if (!result.cancelled) {
onSend([{ image: result.uri }])
return result.uri
}
}
}
export async function takePictureAsync(onSend) {
if (await getPermissionAsync(Permissions.CAMERA)) {
const result = await ImagePicker.launchCameraAsync({
allowsEditing: true,
aspect: [4, 3],
})
if (!result.cancelled) {
onSend([{ image: result.uri }])
return result.uri
}
}
}