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,35 +1,38 @@
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>
@ -42,16 +45,17 @@ 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) { if (data) {
const { locations } = data; const { location } = data;
Trips.deltaLoc( locations ); Trips.deltaLoc( location );
} }
}); });
@ -59,8 +63,8 @@ TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
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,6 +56,7 @@ function pauseTrip() {
} }
function endTrip() { Trips.end(); } function endTrip() { Trips.end(); }
export default function TripScreen( { navigation }: RootTabScreenProps<'Trip'>) { export default function TripScreen( { navigation }: RootTabScreenProps<'Trip'>) {