GT2/Ejectable/BGEO.js

78 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-08-30 00:56:54 +00:00
import React from 'react';
import { StyleSheet } from 'react-native';
import { Pressable } from 'react-native';
import { Text, View } from './components/Themed.tsx';
import * as TaskManager from 'expo-task-manager';
import * as Location from 'expo-location';
2021-08-30 21:05:06 +00:00
import { bgOps, Trips, setBgOps } from './GT2';
2021-08-30 00:56:54 +00:00
const LOCATION_TASK_NAME = 'background-location-task';
const requestPermissions = async () => {
if (!locEnabled) {
const { status } = await Location.requestBackgroundPermissionsAsync();
if (status === 'granted') {
await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
accuracy: Location.Accuracy.Balanced,
});
setBgOps(true);
}
else setBgOps(false);
}
};
export default function PermissionsButton() {
2021-08-30 21:05:06 +00:00
if (!bgOps)
2021-08-30 00:56:54 +00:00
return(
<View style={styles.container} lightColor="lime" darkColor="forestgreen">
<Pressable style={styles.button} onPress={requestPermissions}>
<Text style={styles.text}>{'Enable background operations'}</Text>
2021-08-30 21:05:06 +00:00
<Text style={styles.text2}>{' (Only used during a trip)'}</Text>
2021-08-30 00:56:54 +00:00
</Pressable>
</View>);
2021-08-30 21:05:06 +00:00
else
return(
<View style={styles.container} lightColor="lime" darkColor="forestgreen">
<Text style={styles.text}>{'Trips can run in background.'}</Text>
</View>);
2021-08-30 00:56:54 +00:00
}
TaskManager.defineTask(LOCATION_TASK_NAME, ({ 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 );
}
});
const styles = StyleSheet.create({
container: {
2021-08-30 21:05:06 +00:00
marginVertical: 2,
marginTop: -40,
marginBottom: 25,
2021-08-30 00:56:54 +00:00
alignItems: 'center',
},
button: {
fontSize: 10,
backgroundColor: 'lime',
},
2021-08-30 21:05:06 +00:00
text: {
color: "blue",
},
text2: {
fontSize: 8,
color: "navy",
}
2021-08-30 00:56:54 +00:00
});