@@ -7,9 +7,10 @@ import * as Permissions from 'expo-permissions';
7
7
import * as Location from 'expo-location' ;
8
8
import * as TaskManager from 'expo-task-manager' ;
9
9
import { Stopwatch } from 'react-native-stopwatch-timer' ;
10
- import ViewShot from "react-native-view-shot" ;
11
10
import SafeAreaView from 'react-native-safe-area-view' ;
12
11
import * as geolib from 'geolib' ;
12
+ import config from "../config/config" ;
13
+ import { onSignIn } from "../auth/Auth" ;
13
14
14
15
const STORAGE_KEY_COORDINATES = 'COORDINATES' ;
15
16
const STORAGE_KEY_STATS = 'STATS' ;
@@ -113,7 +114,7 @@ export default class Home extends Component {
113
114
const dataFetch = JSON . parse ( await Home . getData ( STORAGE_KEY_COORDINATES ) ) ;
114
115
const dataStats = JSON . parse ( await Home . getData ( STORAGE_KEY_STATS ) ) ;
115
116
if ( dataFetch && dataStats ) {
116
- console . log ( dataStats ) ;
117
+ // console.log(dataStats);
117
118
this . setState ( { routeCoordinates : dataFetch , lastLocation : dataFetch [ dataFetch . length - 1 ] , stats : dataStats , centered :false } ) ;
118
119
}
119
120
}
@@ -134,27 +135,60 @@ export default class Home extends Component {
134
135
} else if ( this . state . canStart && this . state . toggle ) {
135
136
this . toggleStopwatch ( ) ;
136
137
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 ) ) ;
138
140
let markers = [ {
139
141
image :require ( '../assets/img/button_green.png' ) ,
140
142
coordinates : {
141
- latitude : dataFetch [ 0 ] . latitude ,
142
- longitude : dataFetch [ 0 ] . longitude
143
+ latitude : dataCoordinates [ 0 ] . latitude ,
144
+ longitude : dataCoordinates [ 0 ] . longitude
143
145
} ,
144
146
} ,
145
147
{
146
148
image :require ( '../assets/img/button_red.png' ) ,
147
149
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
150
152
} ,
151
153
152
154
} ]
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
+
158
192
await AsyncStorage . removeItem ( STORAGE_KEY_COORDINATES ) ;
159
193
await AsyncStorage . removeItem ( STORAGE_KEY_STATS ) ;
160
194
console . log ( 'Stop!' ) ;
@@ -207,47 +241,46 @@ export default class Home extends Component {
207
241
}
208
242
return (
209
243
< 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 >
228
253
</ 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 >
232
267
</ View >
233
268
</ View >
234
- </ View >
235
- < ViewShot ref = "viewShot" options = { { format : "jpg" , quality : 0.9 , result :"base64" } } style = { { flex :4 } } >
236
269
< MapView
237
270
ref = { ( map ) => { this . map = map ; } }
238
271
showsMyLocationButton = { false }
239
272
showsUserLocation = { true }
240
273
style = { {
241
- flex : 1
274
+ flex : 4
242
275
} }
243
276
region = { latitude == null ?
244
277
undefined :
245
278
{
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
+ }
251
284
}
252
285
253
286
onUserLocationChange = { this . onUserLocationChange }
@@ -267,44 +300,43 @@ export default class Home extends Component {
267
300
strokeWidth = { 5 }
268
301
/>
269
302
</ 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 >
306
338
307
- </ View >
339
+ </ View >
308
340
</ SafeAreaView >
309
341
) ;
310
342
}
@@ -346,16 +378,22 @@ if (!TaskManager.isTaskDefined('GetLocation')) {
346
378
// console.log('Average speed',((pastStats+distance)/TotalTime)*3.6);
347
379
// console.log('Distance tottal',pastStats,distance)
348
380
// console.log({distance:pastStats+distance,
349
- // avgspeed :(((pastStats+distance)/TotalTime)*3.6).toFixed(2),
381
+ // avgSpeed :(((pastStats+distance)/TotalTime)*3.6).toFixed(2),
350
382
// speed:(data.locations[data.locations.length-1].coords.speed*3.6).toFixed(2) })
351
383
352
384
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 } ]
354
386
await Home . setData ( STORAGE_KEY_STATS , JSON . stringify ( Stats ) ) ;
355
387
} else {
356
388
let pastStatsParsed = JSON . parse ( pastStats ) ;
357
389
// 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 } , ]
359
397
await Home . setData ( STORAGE_KEY_STATS , JSON . stringify ( CurrentStats ) ) ;
360
398
}
361
399
await Home . setData ( STORAGE_KEY_COORDINATES , JSON . stringify ( JSON . parse ( pastCoordinates ) . concat ( currentCoordinates ) ) ) ;
0 commit comments