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 { useState } from 'react';
import { StyleSheet } from 'react-native';
import { Pressable } from 'react-native';
import { Alert, Pressable } from 'react-native';
import { Text, View } from './components/Themed.tsx';
import * as TaskManager from 'expo-task-manager';
import * as Location from 'expo-location';
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();
if (status === 'granted') {
await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
accuracy: Location.Accuracy.Balanced,
await Location.startLocationUpdatesAsync(BACKGROUND_LOCATION_TRACKER , {
accuracy: Location.Accuracy.Highest
});
setBgOps(true);
}
else setBgOps(false);
}
};
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)
return(
<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.text2}>{' (Only used during a trip)'}</Text>
<Text style={styles.text2}>{' (Only used during a trip)'}</Text>
</Pressable>
</View>);
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) {
// Error occurred - check `error.message` for more details.
console.log(error.message);
return;
}
if (data) {
const { locations } = data;
Trips.deltaLoc( locations );
}
if (data) {
const { location } = data;
Trips.deltaLoc( location );
}
});
const styles = StyleSheet.create({
container: {
marginVertical: 2,
marginTop: -40,
marginBottom: 25,
marginTop: -20,
marginBottom: 15,
alignItems: 'center',
},
button: {
@ -68,10 +72,12 @@ const styles = StyleSheet.create({
backgroundColor: 'lime',
},
text: {
fontSize: 14,
fontWeight: 'bold',
color: "blue",
},
text2: {
fontSize: 8,
fontSize: 10,
color: "navy",
}
});

View File

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