This commit is contained in:
Ren Juan 2021-09-02 06:58:19 +00:00
parent faf3aec937
commit 47341586bd
2 changed files with 33 additions and 26 deletions

View File

@ -1,37 +1,40 @@
import React from 'react'; import React from 'react';
import { useState } from 'react';
import { StyleSheet } from 'react-native'; import { StyleSheet } from 'react-native';
import { Pressable } from 'react-native'; import { Alert, Pressable } from 'react-native';
import { Text, View } from './components/Themed.tsx'; import { Text, View } from './components/Themed.tsx';
import * as TaskManager from 'expo-task-manager'; import * as TaskManager from 'expo-task-manager';
import * as Location from 'expo-location'; import * as Location from 'expo-location';
import { bgOps, Trips, setBgOps } from './GT2'; import { bgOps, Trips, setBgOps } from './GT2';
const LOCATION_TASK_NAME = 'background-location-task'; const BACKGROUND_LOCATION_TRACKER = 'BACKGROUND_LOCATION_TRACKER'
const requestPermissions = async () => {
if (!locEnabled) { const getUpdates = async () => {
const { status } = await Location.requestBackgroundPermissionsAsync(); await Location.startLocationUpdatesAsync(BACKGROUND_LOCATION_TRACKER , {
if (status === 'granted') { accuracy: Location.Accuracy.Highest
await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
accuracy: Location.Accuracy.Balanced,
}); });
setBgOps(true);
}
else setBgOps(false);
}
}; };
export default function PermissionsButton() { export default function PermissionsButton() {
const [fuBgOps, setfuBgOps] = useState(0);
const requestPermissions = async () => {
const { status } = await Location.requestBackgroundPermissionsAsync();
setBgOps((status === 'granted'));
setfuBgOps(fuBgOps => fuBgOps + 1);
};
if (!bgOps) if (!bgOps)
return( return(
<View style={styles.container} lightColor="lime" darkColor="forestgreen"> <View style={styles.container} lightColor="lime" darkColor="forestgreen">
<Pressable style={styles.button} onPress={requestPermissions}> <Pressable style={styles.button}
onPressIn={requestPermissions }>
<Text style={styles.text}>{'Enable background operations'}</Text> <Text style={styles.text}>{'Enable background operations'}</Text>
<Text style={styles.text2}>{' (Only used during a trip)'}</Text> <Text style={styles.text2}>{' (Only used during a trip)'}</Text>
</Pressable> </Pressable>
</View>); </View>);
else else
@ -42,25 +45,26 @@ export default function PermissionsButton() {
} }
TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => { TaskManager.defineTask(BACKGROUND_LOCATION_TRACKER , async ({ data, error }) => {
if (error) { if (error) {
// Error occurred - check `error.message` for more details. // Error occurred - check `error.message` for more details.
console.log(error.message); console.log(error.message);
return; return;
} }
if (data) {
const { locations } = data;
Trips.deltaLoc( locations );
}
if (data) {
const { location } = data;
Trips.deltaLoc( location );
}
}); });
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
marginVertical: 2, marginVertical: 2,
marginTop: -40, marginTop: -20,
marginBottom: 25, marginBottom: 15,
alignItems: 'center', alignItems: 'center',
}, },
button: { button: {
@ -68,10 +72,12 @@ const styles = StyleSheet.create({
backgroundColor: 'lime', backgroundColor: 'lime',
}, },
text: { text: {
fontSize: 14,
fontWeight: 'bold',
color: "blue", color: "blue",
}, },
text2: { text2: {
fontSize: 8, fontSize: 10,
color: "navy", color: "navy",
} }
}); });

View File

@ -56,12 +56,13 @@ function pauseTrip() {
} }
function endTrip() { Trips.end(); } function endTrip() { Trips.end(); }
export default function TripScreen( { navigation }: RootTabScreenProps<'Trip'>) { export default function TripScreen( { navigation }: RootTabScreenProps<'Trip'>) {
const [sButtonText, setSButtonText] = useState("Start"); const [sButtonText, setSButtonText] = useState("Start");
const [pButtonText, setPButtonText] = useState("Pause"); const [pButtonText, setPButtonText] = useState("Pause");
if (Trips.nTrips < 1) if (Trips.nTrips < 1)
return ( return (