GT2/Ejectable/BGEO.js

84 lines
2.0 KiB
JavaScript
Raw Normal View History

2021-08-30 00:56:54 +00:00
import React from 'react';
2021-09-02 06:58:19 +00:00
import { useState } from 'react';
2021-08-30 00:56:54 +00:00
import { StyleSheet } from 'react-native';
2021-09-02 06:58:19 +00:00
import { Alert, Pressable } from 'react-native';
2021-08-30 00:56:54 +00:00
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
2021-09-02 06:58:19 +00:00
const BACKGROUND_LOCATION_TRACKER = 'BACKGROUND_LOCATION_TRACKER'
2021-08-30 00:56:54 +00:00
2021-09-02 06:58:19 +00:00
const getUpdates = async () => {
2021-08-30 00:56:54 +00:00
2021-09-02 06:58:19 +00:00
await Location.startLocationUpdatesAsync(BACKGROUND_LOCATION_TRACKER , {
accuracy: Location.Accuracy.Highest
2021-08-30 00:56:54 +00:00
});
2021-09-02 06:58:19 +00:00
2021-08-30 00:56:54 +00:00
};
export default function PermissionsButton() {
2021-09-02 06:58:19 +00:00
const [fuBgOps, setfuBgOps] = useState(0);
const requestPermissions = async () => {
const { status } = await Location.requestBackgroundPermissionsAsync();
setBgOps((status === 'granted'));
setfuBgOps(fuBgOps => fuBgOps + 1);
};
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">
2021-09-02 06:58:19 +00:00
<Pressable style={styles.button}
onPressIn={requestPermissions }>
2021-08-30 00:56:54 +00:00
<Text style={styles.text}>{'Enable background operations'}</Text>
2021-09-02 06:58:19 +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
}
2021-09-02 06:58:19 +00:00
TaskManager.defineTask(BACKGROUND_LOCATION_TRACKER , async ({ data, error }) => {
2021-08-30 00:56:54 +00:00
if (error) {
// Error occurred - check `error.message` for more details.
console.log(error.message);
return;
}
2021-09-02 06:58:19 +00:00
2021-08-30 00:56:54 +00:00
if (data) {
2021-09-02 06:58:19 +00:00
const { location } = data;
Trips.deltaLoc( location );
2021-08-30 00:56:54 +00:00
}
2021-09-02 06:58:19 +00:00
2021-08-30 00:56:54 +00:00
});
const styles = StyleSheet.create({
container: {
2021-08-30 21:05:06 +00:00
marginVertical: 2,
2021-09-02 06:58:19 +00:00
marginTop: -20,
marginBottom: 15,
2021-08-30 00:56:54 +00:00
alignItems: 'center',
},
button: {
fontSize: 10,
backgroundColor: 'lime',
},
2021-08-30 21:05:06 +00:00
text: {
2021-09-02 06:58:19 +00:00
fontSize: 14,
fontWeight: 'bold',
2021-08-30 21:05:06 +00:00
color: "blue",
},
text2: {
2021-09-02 06:58:19 +00:00
fontSize: 10,
2021-08-30 21:05:06 +00:00
color: "navy",
}
2021-08-30 00:56:54 +00:00
});