Skip to content

Commit

Permalink
clean up BackgroundGeolocation configuration and start/stop (#412)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kenpugsley authored Apr 9, 2020
1 parent 77f6765 commit f554600
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
10 changes: 5 additions & 5 deletions app/services/LocationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -345,15 +345,15 @@ export default class LocationServices {
});
}

static stop(nav) {
static stop() {
// unregister all event listeners
PushNotification.localNotification({
title: languages.t('label.location_disabled_title'),
message: languages.t('label.location_disabled_message'),
});
BackgroundGeolocation.removeAllListeners();
BackgroundGeolocation.stop();
instanceCount -= 1;
isBackgroundGeolocationConfigured = false;
SetStoreData('PARTICIPATE', 'false').then(() => {
// nav.navigate('LocationTrackingScreen', {});
});
Expand Down
9 changes: 5 additions & 4 deletions app/views/LocationTracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
})
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit f554600

Please sign in to comment.