Skip to content

Commit 8ed0b1d

Browse files
author
Ervin
committed
Merge branch 'master' of github.com:Tourniercy/RunningApp
2 parents 07311df + 9ebd4fd commit 8ed0b1d

File tree

8 files changed

+143
-96
lines changed

8 files changed

+143
-96
lines changed

App/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
API_URL=http://122e27b1.ngrok.io

App/App.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {Component} from "react";
2-
import {Platform, StatusBar, StyleSheet, AppRegistry} from 'react-native';
2+
import {Platform, StatusBar, StyleSheet, AppRegistry, AsyncStorage, AppState} from 'react-native';
33
import { createRootNavigator } from "./navigation/Navigation";
44
import { isSignedIn } from "./auth/Auth";
55
import { SafeAreaProvider } from 'react-native-safe-area-context';
@@ -16,11 +16,11 @@ export default class App extends Component {
1616
};
1717
}
1818

19-
componentDidMount() {
19+
componentDidMount = async() => {
2020
isSignedIn()
2121
.then(res => this.setState({ signedIn: res, checkedSignIn: true }))
2222
.catch(err => alert("An error occurred"));
23-
}
23+
};
2424

2525
render() {
2626
let { checkedSignIn, signedIn } = this.state;

App/auth/Auth.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AsyncStorage } from "react-native";
2+
import config from '../config/config';
23

34
let USER_TOKEN = "";
45
let USER_REFRESH_TOKEN = "";
@@ -8,7 +9,7 @@ export async function getToken(values) {
89

910
let token = await
1011

11-
fetch(`http://d2714e36.ngrok.io/api/login_check`, {
12+
fetch(``+config.API_URL+`/api/login_check`, {
1213

1314
method: 'POST',
1415
headers: {
@@ -27,8 +28,7 @@ export async function getToken(values) {
2728
.then(async responseData => {
2829

2930
if (responseData.token) {
30-
31-
let id = await fetch(`http://d2714e36.ngrok.io/users/check/` + values.email, {
31+
let id = await fetch(``+config.API_URL+`/users/check/` + values.email, {
3232

3333
method: 'POST',
3434
headers: {
@@ -48,7 +48,7 @@ export async function getToken(values) {
4848
USER_TOKEN = await responseData.token
4949
USER_REFRESH_TOKEN = await responseData.refresh_token
5050

51-
onSignIn()
51+
await onSignIn()
5252
return 200
5353
}
5454
else if (responseData.code === 401) {
@@ -67,9 +67,9 @@ export async function getToken(values) {
6767
}
6868

6969
export const onSignIn = async () => {
70-
AsyncStorage.setItem("user_id", JSON.stringify(USER_ID))
71-
AsyncStorage.setItem("token", USER_TOKEN)
72-
AsyncStorage.setItem("refresh_token", USER_REFRESH_TOKEN)
70+
await AsyncStorage.setItem("user_id", JSON.stringify(USER_ID))
71+
await AsyncStorage.setItem("token", USER_TOKEN)
72+
await AsyncStorage.setItem("refresh_token", USER_REFRESH_TOKEN)
7373
};
7474

7575
export async function getUserId() {

App/babel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = function(api) {
22
api.cache(true);
33
return {
4-
presets: ['babel-preset-expo'],
4+
presets: ['babel-preset-expo', 'module:react-native-dotenv'],
55
};
66
};

App/config/config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { API_URL } from 'react-native-dotenv';
2+
3+
export default {
4+
API_URL,
5+
};

App/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"react-native": "https://github.com/expo/react-native/archive/sdk-36.0.0.tar.gz",
3434
"react-native-chart-kit": "2.6.0",
3535
"react-native-datepicker": "^1.7.2",
36+
"react-native-dotenv": "^0.2.0",
3637
"react-native-elements": "^1.2.7",
3738
"react-native-firebase": "^5.6.0",
3839
"react-native-gesture-handler": "~1.5.0",
@@ -47,6 +48,7 @@
4748
"react-native-scl-alert": "^1.2.1",
4849
"react-native-screens": "2.0.0-alpha.12",
4950
"react-native-stopwatch-timer": "0.0.21",
51+
"react-native-svg": "9.13.3",
5052
"react-native-svg-charts": "^5.3.0",
5153
"react-native-vector-icons": "^6.6.0",
5254
"react-native-view-shot": "^3.1.2",
@@ -58,8 +60,7 @@
5860
"react-navigation-transitions": "^1.0.12",
5961
"react-redux": "^7.1.3",
6062
"redux": "^4.0.4",
61-
"yup": "^0.28.0",
62-
"react-native-svg": "9.13.3"
63+
"yup": "^0.28.0"
6364
},
6465
"devDependencies": {
6566
"babel-preset-expo": "^7.1.0",

App/view/Home.js

Lines changed: 119 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import * as Permissions from 'expo-permissions';
77
import * as Location from 'expo-location';
88
import * as TaskManager from 'expo-task-manager';
99
import { Stopwatch } from 'react-native-stopwatch-timer';
10-
import ViewShot from "react-native-view-shot";
1110
import SafeAreaView from 'react-native-safe-area-view';
1211
import * as geolib from 'geolib';
12+
import config from "../config/config";
13+
import {onSignIn} from "../auth/Auth";
1314

1415
const STORAGE_KEY_COORDINATES = 'COORDINATES';
1516
const STORAGE_KEY_STATS = 'STATS';
@@ -113,7 +114,7 @@ export default class Home extends Component {
113114
const dataFetch = JSON.parse(await Home.getData(STORAGE_KEY_COORDINATES));
114115
const dataStats = JSON.parse(await Home.getData(STORAGE_KEY_STATS));
115116
if (dataFetch && dataStats) {
116-
console.log(dataStats);
117+
// console.log(dataStats);
117118
this.setState({routeCoordinates: dataFetch,lastLocation : dataFetch[dataFetch.length-1],stats: dataStats,centered:false});
118119
}
119120
}
@@ -134,27 +135,60 @@ export default class Home extends Component {
134135
} else if (this.state.canStart && this.state.toggle){
135136
this.toggleStopwatch();
136137
await Location.stopLocationUpdatesAsync('GetLocation');
137-
const dataFetch = JSON.parse(await Home.getData(STORAGE_KEY_COORDINATES));
138+
const dataCoordinates = JSON.parse(await Home.getData(STORAGE_KEY_COORDINATES));
139+
const dataStats = JSON.parse(await Home.getData(STORAGE_KEY_STATS));
138140
let markers = [{
139141
image:require('../assets/img/button_green.png'),
140142
coordinates: {
141-
latitude: dataFetch[0].latitude,
142-
longitude: dataFetch[0].longitude
143+
latitude: dataCoordinates[0].latitude,
144+
longitude: dataCoordinates[0].longitude
143145
},
144146
},
145147
{
146148
image:require('../assets/img/button_red.png'),
147149
coordinates: {
148-
latitude: dataFetch[dataFetch.length-1].latitude,
149-
longitude: dataFetch[dataFetch.length-1].longitude
150+
latitude: dataCoordinates[dataCoordinates.length-1].latitude,
151+
longitude: dataCoordinates[dataCoordinates.length-1].longitude
150152
},
151153

152154
}]
153-
this.refs.viewShot.capture().then(uri => {
154-
// console.log(uri);
155-
});
156-
console.log(dataFetch);
157-
this.setState({started: false,startTime: 0,markers :markers,routeCoordinates: dataFetch});
155+
// this.refs.viewShot.capture().then(uri => {
156+
// // console.log(uri);
157+
// });
158+
dataStats[0].coordinates = dataCoordinates;
159+
const getUserToken = await Home.getData("token");
160+
console.log(getUserToken);
161+
fetch(``+config.API_URL+`/api/courses`, {
162+
163+
method: 'POST',
164+
headers: {
165+
Accept: 'application/json',
166+
'Content-Type': 'application/json',
167+
'Authorization': 'Bearer '+getUserToken
168+
},
169+
body: JSON.stringify({
170+
"distance": 0,
171+
"avgSpeed": 0,
172+
"maxSpeed": 0,
173+
"time":'15:26:00',
174+
"createdAt": "2020-03-29T15:26:33.386Z",
175+
"coordinates": [
176+
"10"
177+
],
178+
"user": "/api/users/1"
179+
})
180+
})
181+
.then(async resp => {
182+
console.log(resp);
183+
return resp.json()
184+
}).then(async responseData => {
185+
console.log(responseData);
186+
return 200
187+
})
188+
189+
190+
this.setState({started: false,startTime: 0,markers :markers,routeCoordinates: dataCoordinates});
191+
158192
await AsyncStorage.removeItem(STORAGE_KEY_COORDINATES);
159193
await AsyncStorage.removeItem(STORAGE_KEY_STATS);
160194
console.log('Stop!');
@@ -207,47 +241,46 @@ export default class Home extends Component {
207241
}
208242
return (
209243
<SafeAreaView style={{ flex: 1}} forceInset={{ top: 'always' }}>
210-
<View style={{flex:1}}>
211-
<View style={{flex:1,backgroundColor:'white', padding: 10, paddingBottom: 20}}>
212-
<View style={{flex: 1, flexDirection: 'row',alignContent:'stretch',justifyContent:'center',alignItems: 'stretch',paddingTop:10}}>
213-
<View style={{flex: 1, width:50, alignItems:'center'}}>
214-
<Image
215-
style={{alignSelf: 'center', width: 50, height: 50}}
216-
source={require('../assets/img/Logo.png')}
217-
/>
218-
</View>
219-
</View>
220-
<View style={{flex: 1, flexDirection: 'row',alignContent:'stretch',justifyContent:'center',alignItems: 'stretch', marginTop: 20}}>
221-
<View style={{flex: 1, alignItems:'center'}} >
222-
<Text style={{fontSize: 24, fontWeight: 'bold'}}>{speed}</Text>
223-
<Text style={{fontSize: 12}}>Rythme. moy. (km/h)</Text>
224-
</View>
225-
<View style={{flex: 1, alignItems:'center'}}>
226-
<Stopwatch start={this.state.stopwatchStart} startTime={this.state.startTime} getTime={this.getFormattedTime} reset={this.state.stopwatchReset} options={stopwatchoptions}/>
227-
<Text style={{fontSize: 12}}>Durée</Text>
244+
<View style={{flex:1}}>
245+
<View style={{flex:1,backgroundColor:'white', padding: 10, paddingBottom: 20}}>
246+
<View style={{flex: 1, flexDirection: 'row',alignContent:'stretch',justifyContent:'center',alignItems: 'stretch',paddingTop:10}}>
247+
<View style={{flex: 1, width:50, alignItems:'center'}}>
248+
<Image
249+
style={{alignSelf: 'center', width: 50, height: 50}}
250+
source={require('../assets/img/Logo.png')}
251+
/>
252+
</View>
228253
</View>
229-
<View style={{flex: 1, alignItems:'center'}}>
230-
<Text style={{fontSize: 24, fontWeight: 'bold'}}>{distance}</Text>
231-
<Text style={{fontSize: 12}}>Distance (m)</Text>
254+
<View style={{flex: 1, flexDirection: 'row',alignContent:'stretch',justifyContent:'center',alignItems: 'stretch', marginTop: 20}}>
255+
<View style={{flex: 1, alignItems:'center'}} >
256+
<Text style={{fontSize: 24, fontWeight: 'bold'}}>{speed}</Text>
257+
<Text style={{fontSize: 12}}>Rythme. moy. (km/h)</Text>
258+
</View>
259+
<View style={{flex: 1, alignItems:'center'}}>
260+
<Stopwatch start={this.state.stopwatchStart} startTime={this.state.startTime} getTime={this.getFormattedTime} reset={this.state.stopwatchReset} options={stopwatchoptions}/>
261+
<Text style={{fontSize: 12}}>Durée</Text>
262+
</View>
263+
<View style={{flex: 1, alignItems:'center'}}>
264+
<Text style={{fontSize: 24, fontWeight: 'bold'}}>{distance}</Text>
265+
<Text style={{fontSize: 12}}>Distance (m)</Text>
266+
</View>
232267
</View>
233268
</View>
234-
</View>
235-
<ViewShot ref="viewShot" options={{ format: "jpg", quality: 0.9,result:"base64" }} style={{flex:4}}>
236269
<MapView
237270
ref={(map) => { this.map = map; }}
238271
showsMyLocationButton={ false }
239272
showsUserLocation={ true }
240273
style={{
241-
flex: 1
274+
flex: 4
242275
}}
243276
region={latitude == null ?
244277
undefined :
245278
{
246-
latitude: latitude,
247-
longitude: longitude,
248-
latitudeDelta: latitudeDelta,
249-
longitudeDelta: longitudeDelta
250-
}
279+
latitude: latitude,
280+
longitude: longitude,
281+
latitudeDelta: latitudeDelta,
282+
longitudeDelta: longitudeDelta
283+
}
251284
}
252285

253286
onUserLocationChange={this.onUserLocationChange}
@@ -267,44 +300,43 @@ export default class Home extends Component {
267300
strokeWidth={5}
268301
/>
269302
</MapView>
270-
</ViewShot>
271-
<View
272-
style={{
273-
position: 'absolute',//use absolute position to show button on top of the map
274-
top: '27%', //for center align
275-
left : '87%',
276-
flexDirection: 'row',
277-
}}
278-
>
279-
<Button
280-
icon={
281-
<Icon
282-
name="gps-fixed"
283-
size={20}
284-
color="white"
285-
/>
286-
}
287-
onPress={this._onPressCenter}
288-
/>
289-
</View>
290-
<View
291-
style={{
292-
position: 'absolute',//use absolute position to show button on top of the map
293-
top: '90%', //for center align
294-
alignSelf: 'center', //for align to right
295-
flexDirection: 'row',
296-
}}
297-
>
298-
<Button
299-
buttonStyle={{backgroundColor:buttonBg,width:120,height:50}}
300-
title={textValue}
301-
type="solid"
302-
color="#2C5077"
303-
onPress={this._onPressStopStart}
304-
/>
305-
</View>
303+
<View
304+
style={{
305+
position: 'absolute',//use absolute position to show button on top of the map
306+
top: '27%', //for center align
307+
left : '87%',
308+
flexDirection: 'row',
309+
}}
310+
>
311+
<Button
312+
icon={
313+
<Icon
314+
name="gps-fixed"
315+
size={20}
316+
color="white"
317+
/>
318+
}
319+
onPress={this._onPressCenter}
320+
/>
321+
</View>
322+
<View
323+
style={{
324+
position: 'absolute',//use absolute position to show button on top of the map
325+
top: '90%', //for center align
326+
alignSelf: 'center', //for align to right
327+
flexDirection: 'row',
328+
}}
329+
>
330+
<Button
331+
buttonStyle={{backgroundColor:buttonBg,width:120,height:50}}
332+
title={textValue}
333+
type="solid"
334+
color="#2C5077"
335+
onPress={this._onPressStopStart}
336+
/>
337+
</View>
306338

307-
</View>
339+
</View>
308340
</SafeAreaView>
309341
);
310342
}
@@ -346,16 +378,22 @@ if (!TaskManager.isTaskDefined('GetLocation')) {
346378
// console.log('Average speed',((pastStats+distance)/TotalTime)*3.6);
347379
// console.log('Distance tottal',pastStats,distance)
348380
// console.log({distance:pastStats+distance,
349-
// avgspeed:(((pastStats+distance)/TotalTime)*3.6).toFixed(2),
381+
// avgSpeed:(((pastStats+distance)/TotalTime)*3.6).toFixed(2),
350382
// speed:(data.locations[data.locations.length-1].coords.speed*3.6).toFixed(2)})
351383

352384
if (pastStats == null) {
353-
let Stats= [{distance:distance,avgspeed:(data.locations[data.locations.length-1].coords.speed).toFixed(2),speed:(data.locations[data.locations.length-1].coords.speed).toFixed(2),totaltime:TotalTime}]
385+
let Stats= [{distance:distance,avgSpeed:(data.locations[data.locations.length-1].coords.speed).toFixed(2),speed:(data.locations[data.locations.length-1].coords.speed).toFixed(2),maxpseed:(data.locations[data.locations.length-1].coords.speed).toFixed(2),totaltime:TotalTime}]
354386
await Home.setData(STORAGE_KEY_STATS, JSON.stringify(Stats));
355387
} else {
356388
let pastStatsParsed = JSON.parse(pastStats);
357389
// console.log('pastStatsParsed',pastStatsParsed)
358-
let CurrentStats= [{distance:pastStatsParsed[0].distance+distance,avgspeed:(((pastStatsParsed[0].distance+distance)/TotalTime)*3.6).toFixed(2),speed:(data.locations[data.locations.length-1].coords.speed).toFixed(2),totaltime:TotalTime},]
390+
let maxSpeed = 0;
391+
if (data.locations[data.locations.length-1].coords.speed.toFixed(2) > pastStatsParsed[0].maxSpeed) {
392+
maxSpeed = data.locations[data.locations.length-1].coords.speed.toFixed(2);
393+
} else {
394+
maxSpeed = pastStatsParsed[0].speed;
395+
}
396+
let CurrentStats= [{distance:pastStatsParsed[0].distance+distance,avgSpeed:(((pastStatsParsed[0].distance+distance)/TotalTime)*3.6).toFixed(2),maxSpeed:maxSpeed,speed:(data.locations[data.locations.length-1].coords.speed).toFixed(2),totaltime:TotalTime},]
359397
await Home.setData(STORAGE_KEY_STATS, JSON.stringify(CurrentStats));
360398
}
361399
await Home.setData(STORAGE_KEY_COORDINATES,JSON.stringify(JSON.parse(pastCoordinates).concat(currentCoordinates)));

0 commit comments

Comments
 (0)