From f554600d05be857a404b788c7de9507c60a75c8d Mon Sep 17 00:00:00 2001 From: kpugsley Date: Thu, 9 Apr 2020 09:42:05 -0500 Subject: [PATCH] clean up BackgroundGeolocation configuration and start/stop (#412) LocationService.js - Clean up variable name for internal configuration, and when it's set. - remove unused variable from .stop() method LocationTracking.js - Moved LocationService start() and stop() calls to ensure the location service gets enabled or disabled appropriately if a user changes location permissions --- app/services/LocationService.js | 10 +++++----- app/views/LocationTracking.js | 9 +++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/services/LocationService.js b/app/services/LocationService.js index 5a05f9f467..8f9d1cfaeb 100644 --- a/app/services/LocationService.js +++ b/app/services/LocationService.js @@ -7,7 +7,7 @@ import PushNotification from 'react-native-push-notification'; import { isPlatformAndroid } from '../Util'; import languages from '../locales/languages'; -let hasBeenStarted = false; +let isBackgroundGeolocationConfigured = false; export class LocationData { constructor() { @@ -133,11 +133,10 @@ export default class LocationServices { // handles edge cases around Android where start might get called again even though // the service is already created. Make sure the listeners are still bound and exit - if (hasBeenStarted) { + if (isBackgroundGeolocationConfigured) { BackgroundGeolocation.start(); return; } - hasBeenStarted = true; PushNotification.configure({ // (required) Called when a remote or local notification is opened or received @@ -291,6 +290,7 @@ export default class LocationServices { ); BackgroundGeolocation.start(); //triggers start on start event + isBackgroundGeolocationConfigured = true; if (!status.locationServicesEnabled) { // we need to set delay or otherwise alert may not be shown @@ -345,7 +345,7 @@ export default class LocationServices { }); } - static stop(nav) { + static stop() { // unregister all event listeners PushNotification.localNotification({ title: languages.t('label.location_disabled_title'), @@ -353,7 +353,7 @@ export default class LocationServices { }); BackgroundGeolocation.removeAllListeners(); BackgroundGeolocation.stop(); - instanceCount -= 1; + isBackgroundGeolocationConfigured = false; SetStoreData('PARTICIPATE', 'false').then(() => { // nav.navigate('LocationTrackingScreen', {}); }); diff --git a/app/views/LocationTracking.js b/app/views/LocationTracking.js index 6ef7101ac8..75b9450803 100644 --- a/app/views/LocationTracking.js +++ b/app/views/LocationTracking.js @@ -111,11 +111,13 @@ class LocationTracking extends Component { .then(result => { switch (result) { case RESULTS.GRANTED: + LocationServices.start(); this.checkIfUserAtRisk(); return; case RESULTS.UNAVAILABLE: case RESULTS.BLOCKED: console.log('NO LOCATION'); + LocationServices.stop(); this.setState({ currentState: StateEnum.UNKNOWN }); } }) @@ -293,21 +295,20 @@ class LocationTracking extends Component { willParticipate = () => { SetStoreData('PARTICIPATE', 'true').then(() => { - LocationServices.start(); - // Turn of bluetooth for v1 + // Turn off bluetooth for v1 //BroadcastingServices.start(); }); - // Check and see if they actually authorized in the system dialog. // If not, stop services and set the state to !isLogging // Fixes tripleblindmarket/private-kit#129 BackgroundGeolocation.checkStatus(({ authorization }) => { if (authorization === BackgroundGeolocation.AUTHORIZED) { + LocationServices.start(); this.setState({ isLogging: true, }); } else if (authorization === BackgroundGeolocation.NOT_AUTHORIZED) { - LocationServices.stop(this.props.navigation); + LocationServices.stop(); // Turn of bluetooth for v1 //BroadcastingServices.stop(this.props.navigation); this.setState({