-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLocationAccessButton.js
More file actions
67 lines (59 loc) · 1.49 KB
/
LocationAccessButton.js
File metadata and controls
67 lines (59 loc) · 1.49 KB
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
/**
* @format
* @flow
*/
import type { TimingState, TimingDispatch } from '../../types';
import React from 'react';
import { Linking } from 'react-native';
import RNLocation from 'react-native-location';
import Button from './Button';
import { Actions } from '../reducer';
type Props = {
state: TimingState,
dispatch: TimingDispatch,
};
const LocationAccessButton = ({ state, dispatch }: Props) => {
const requestPermission = () => {
RNLocation.requestPermission({
ios: 'always', // or 'whenInUse'
android: {
detail: 'fine', // or 'coarse'
rationale: {
title: 'We need to access your location',
message:
'We use your location while your run to calculate your distance and pace',
buttonPositive: 'OK',
buttonNegative: 'Cancel',
},
},
}).then(granted => {
dispatch({ type: Actions.LocationRequested, granted });
});
};
switch (state.granted) {
case true:
return (
<Button
title="Location Access Granted"
type="Success"
onPress={() => Linking.openSettings()}
/>
);
case false:
return (
<Button
title="Location Access Denied"
type="Error"
onPress={() => Linking.openSettings()}
/>
);
default:
return (
<Button
title="Request Location Permissions"
onPress={requestPermission}
/>
);
}
};
export default LocationAccessButton;