GT2/Ejectable/screens/TripScreen.tsx

123 lines
3.4 KiB
TypeScript
Raw Permalink Normal View History

2021-08-16 07:50:50 +00:00
import * as React from 'react';
2021-08-23 06:34:16 +00:00
import { useState } from 'react';
2021-08-30 00:19:58 +00:00
import { Alert, Button, StyleSheet } from 'react-native';
2021-08-16 07:50:50 +00:00
import { Text, View } from '../components/Themed';
2021-08-17 21:05:34 +00:00
import { ScreenInfo2 } from '../components/ScreenInfo';
2021-08-30 00:19:58 +00:00
import { bgOps, TripDisplay, LastTrip, Trips, setEndIsLast } from '../GT2';
2021-08-24 15:43:25 +00:00
import { RootTabScreenProps } from '../types';
2021-08-30 00:19:58 +00:00
import * as Device from 'expo-device';
2021-08-24 15:43:25 +00:00
2021-08-26 03:46:39 +00:00
var debug:number = 0;
2021-08-16 07:50:50 +00:00
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
2021-08-16 22:39:34 +00:00
controls: {
width: "80%",
flexDirection: 'row',
justifyContent: "space-between",
},
2021-08-16 07:50:50 +00:00
title: {
fontSize: 20,
fontWeight: 'bold',
},
separator: {
2021-08-17 21:05:34 +00:00
marginVertical: 10,
2021-08-16 07:50:50 +00:00
height: 1,
width: '80%',
},
});
2021-08-18 18:21:15 +00:00
2021-08-30 00:19:58 +00:00
function startTrip() {
2021-08-18 18:21:15 +00:00
2021-08-30 00:19:58 +00:00
if (!bgOps) {
if (Device.osName === 'iOS')
Alert.alert("Background location not enabled. Must be in foreground and awake during trip!");
else Alert.alert('Stay in foreground, awake during trip!');
2021-08-26 03:46:39 +00:00
}
2021-08-24 00:02:21 +00:00
2021-08-23 06:34:16 +00:00
Trips.start();
2021-08-18 18:21:15 +00:00
}
function pauseTrip() {
2021-08-20 11:23:16 +00:00
2021-08-23 06:34:16 +00:00
if (!Trips.paused) {
Trips.pause();
}
else {
Trips.pause();
}
2021-08-18 18:21:15 +00:00
}
2021-08-24 00:02:21 +00:00
function endTrip() { Trips.end(); }
2021-08-18 18:21:15 +00:00
2021-08-24 15:43:25 +00:00
export default function TripScreen( { navigation }: RootTabScreenProps<'Trip'>) {
2021-08-23 06:34:16 +00:00
const [sButtonText, setSButtonText] = useState("Start");
2021-08-24 00:02:21 +00:00
const [pButtonText, setPButtonText] = useState("Pause");
2021-08-24 15:43:25 +00:00
2021-08-28 16:33:29 +00:00
if (Trips.nTrips < 1)
2021-08-20 11:23:16 +00:00
return (
2021-08-18 18:21:15 +00:00
<View style={styles.container}>
<Text style={styles.title}>Trip Control</Text>
<View style={styles.separator} lightColor="#eee" darkColor="rgba(255,255,255,0.1)" />
<ScreenInfo2 />
<View style={styles.controls} >
<Button
2021-08-23 06:34:16 +00:00
title={sButtonText}
onPress={() => {
2021-08-25 02:25:04 +00:00
if (!Trips.inProgress) {startTrip();
if (Trips.inProgress) { setSButtonText("End"); setPButtonText('Pause');}}
2021-08-28 17:30:12 +00:00
else {setSButtonText('Start'); LastTrip.from(Trips); endTrip(); navigation.push('Modal'); }}
2021-08-23 06:34:16 +00:00
}
2021-08-18 18:21:15 +00:00
/>
<Button
2021-08-23 06:34:16 +00:00
title={pButtonText}
onPress={() => { if (!Trips.inProgress) Alert.alert("No trip in progress!"); else {
if (!Trips.paused) {setPButtonText("Resume");pauseTrip();}
else {setPButtonText('Pause');pauseTrip();}}}
}
/>
2021-08-18 18:21:15 +00:00
</View>
2021-08-23 06:34:16 +00:00
<TripDisplay></TripDisplay>
2021-08-18 18:21:15 +00:00
</View>
2021-08-24 00:02:21 +00:00
);
2021-08-29 00:11:00 +00:00
else
2021-08-28 16:33:29 +00:00
return (
<View style={styles.container}>
<Text style={styles.title}>Trip Control</Text>
<View style={styles.separator} lightColor="#eee" darkColor="rgba(255,255,255,0.1)" />
<ScreenInfo2 />
<View style={styles.controls} >
<Button
title={sButtonText}
onPress={() => {
if (!Trips.inProgress) {startTrip();
if (Trips.inProgress) { setSButtonText("End"); setPButtonText('Pause');}}
else {setSButtonText('Start'); endTrip(); LastTrip.from(Trips); navigation.push('Modal'); }}
}
/>
<Button
title={pButtonText}
onPress={() => { if (!Trips.inProgress) Alert.alert("No trip in progress!"); else {
if (!Trips.paused) {setPButtonText("Resume");pauseTrip();}
else {setPButtonText('Pause');pauseTrip();}}}
}
/>
</View>
<TripDisplay></TripDisplay>
<View>
2021-08-28 17:30:12 +00:00
<Button title={'Show Last Trip'} onPress={() => { setEndIsLast(true); navigation.push('Modal'); } } />
2021-08-28 16:33:29 +00:00
</View>
</View>
);
2021-08-25 02:25:04 +00:00
2021-08-18 18:21:15 +00:00
}