-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
151 lines (142 loc) · 3.83 KB
/
App.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, { Component, useEffect } from 'react';
import { Alert, BackHandler, PermissionsAndroid, Platform, StyleSheet, Text, ToastAndroid, TouchableOpacity, View } from 'react-native';
import Geolocation from 'react-native-geolocation-service';
import SplashScreen from 'react-native-splash-screen';
export default class App extends Component {
constructor() {
super();
this.state = {
location: []
};
}
componentDidMount() {
this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
SplashScreen.hide();
this.getLocationStart();
}
componentWillUnmount() {
this.backHandler.remove();
}
hasLocationPermission = async () => {
if (Platform.OS === 'android' && Platform.Version < 23) {
return true;
}
const hasPermission = await PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
if (hasPermission) {
return true;
}
const status = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
if (status === PermissionsAndroid.RESULTS.GRANTED) {
return true;
}
if (status === PermissionsAndroid.RESULTS.DENIED) {
ToastAndroid.show(
'User rejected the permission request',
ToastAndroid.LONG,
);
} else if (status === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
ToastAndroid.show(
'User canceled the permission request',
ToastAndroid.LONG,
);
}
return false;
};
getLocationStart = async () => {
this.setState({ location: await this.getLocation() });
}
getLocation = async () => {
await this.hasLocationPermission();
const promise = new Promise((resolve, reject) => {
this.setState({ loading: true }, () => {
Geolocation.getCurrentPosition(
(position) => {
resolve(position.coords);
},
(error) => {
ToastAndroid.show(
"Error: " + error.message,
ToastAndroid.LONG,
);
resolve(error);
},
{
enableHighAccuracy: true,
timeout: 15000,
maximumAge: 10000,
}
);
});
});
const result = await promise;
return result;
};
handleBackPress = () => {
if (!this.state.Start_Scanner && !this.state.updatesEnabled) {
Alert.alert(
'Exit the application?',
'Do you want to exit?',
[
{ text: 'No', style: 'cancel' },
{
text: 'Yes', onPress: () => {
BackHandler.exitApp()
}
},
],
{ cancelable: true });
}
return true;
}
render() {
const {
location
} = this.state;
return (
<View style={styles.MainContainer}>
<Text style={{ fontSize: 22, textAlign: 'center' }}>Welcome to Youloc!</Text>
<Text style={{ fontSize: 18, textAlign: 'center' }}>Latitude: {location.latitude}</Text>
<Text style={{ fontSize: 18, textAlign: 'center' }}>Longitude: {location.longitude}</Text>
<TouchableOpacity
onPress={this.getLocationStart}
style={styles.button}>
<Text style={{ color: '#FFF', fontSize: 14 }}>
Refresh
</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
paddingTop: (Platform.OS) === 'ios' ? 20 : 0,
alignItems: 'center',
justifyContent: 'center',
},
QR_text: {
color: '#000',
fontSize: 19,
padding: 8,
marginTop: 12
},
button: {
backgroundColor: '#2979FF',
alignItems: 'center',
padding: 12,
width: 300,
marginTop: 14
},
button_red: {
backgroundColor: 'red',
alignItems: 'center',
padding: 12,
width: 300,
marginTop: 14
},
});