From 5cf5ee9e6ba3b87e8e2ede347b09b76137f70f61 Mon Sep 17 00:00:00 2001 From: soumyashis Date: Thu, 20 Aug 2020 16:34:17 +0530 Subject: [PATCH 1/3] Maps: location manager start/stop listening fix --- javascript/components/UserLocation.js | 37 ++++++++++--------- .../modules/location/locationManager.js | 7 ++++ 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/javascript/components/UserLocation.js b/javascript/components/UserLocation.js index 440d18823..5c754e7ef 100644 --- a/javascript/components/UserLocation.js +++ b/javascript/components/UserLocation.js @@ -137,15 +137,15 @@ class UserLocation extends React.Component { // after component unmount _isMounted = null; - locationManagerRunning = false; + locationManagerRequired = false; async componentDidMount() { this._isMounted = true; - locationManager.addListener(this._onLocationUpdate); + // locationManager.addListener(this._onLocationUpdate); await this.setLocationManager({ - running: this.needsLocationManagerRunning(), + required: this.needsLocationManagerRunning(), }); if (this.renderMode === UserLocation.RenderMode.Native) { @@ -157,7 +157,7 @@ class UserLocation extends React.Component { async componentDidUpdate(prevProps) { await this.setLocationManager({ - running: this.needsLocationManagerRunning(), + required: this.needsLocationManagerRunning(), }); if (this.props.minDisplacement !== prevProps.minDisplacement) { @@ -167,30 +167,31 @@ class UserLocation extends React.Component { async componentWillUnmount() { this._isMounted = false; - locationManager.removeListener(this._onLocationUpdate); - await this.setLocationManager({running: false}); + // locationManager.removeListener(this._onLocationUpdate); + await this.setLocationManager({required: false}); } /** - * Whether to start or stop the locationManager + * Whether to start or stop listening to the locationManager * - * Notice, that locationManager will start automatically when + * Notice, that listening will start automatically when * either `onUpdate` or `visible` are set * * @async - * @param {Object} running - Object with key `running` and `boolean` value + * @param {Object} required - Object with key `required` and `boolean` value * @return {Promise} */ - async setLocationManager({running}) { - if (this.locationManagerRunning !== running) { - this.locationManagerRunning = running; - if (running) { - locationManager.start(); - - const location = await locationManager.getLastKnownLocation(); - this._onLocationUpdate(location); + async setLocationManager({required}) { + if (this.locationManagerRequired !== required) { + this.locationManagerRequired = required; + if (required) { + // locationManager.start(); + locationManager.addListener(this._onLocationUpdate); + // const location = await locationManager.getLastKnownLocation(); + // this._onLocationUpdate(location); } else { - locationManager.stop(); + // locationManager.stop(); + locationManager.removeListener(this._onLocationUpdate); } } } diff --git a/javascript/modules/location/locationManager.js b/javascript/modules/location/locationManager.js index 52e2b6814..9023d6026 100644 --- a/javascript/modules/location/locationManager.js +++ b/javascript/modules/location/locationManager.js @@ -38,6 +38,9 @@ class LocationManager { } addListener(listener) { + if (!this._isListening) { + this.start(); + } if (!this._listeners.includes(listener)) { this._listeners.push(listener); @@ -49,10 +52,14 @@ class LocationManager { removeListener(listener) { this._listeners = this._listeners.filter((l) => l !== listener); + if (this._listeners.length === 0) { + this.stop(); + } } removeAllListeners() { this._listeners = []; + this.stop(); } start(displacement = 0) { From f2550aeb8e0381246cde1b2b1a6a5c40ddb5c710 Mon Sep 17 00:00:00 2001 From: soumyashis Date: Thu, 20 Aug 2020 16:56:21 +0530 Subject: [PATCH 2/3] Remove commented lines --- javascript/components/UserLocation.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/javascript/components/UserLocation.js b/javascript/components/UserLocation.js index 5c754e7ef..9a9423349 100644 --- a/javascript/components/UserLocation.js +++ b/javascript/components/UserLocation.js @@ -142,8 +142,6 @@ class UserLocation extends React.Component { async componentDidMount() { this._isMounted = true; - // locationManager.addListener(this._onLocationUpdate); - await this.setLocationManager({ required: this.needsLocationManagerRunning(), }); @@ -167,7 +165,6 @@ class UserLocation extends React.Component { async componentWillUnmount() { this._isMounted = false; - // locationManager.removeListener(this._onLocationUpdate); await this.setLocationManager({required: false}); } @@ -185,12 +182,8 @@ class UserLocation extends React.Component { if (this.locationManagerRequired !== required) { this.locationManagerRequired = required; if (required) { - // locationManager.start(); locationManager.addListener(this._onLocationUpdate); - // const location = await locationManager.getLastKnownLocation(); - // this._onLocationUpdate(location); } else { - // locationManager.stop(); locationManager.removeListener(this._onLocationUpdate); } } From ccf3c1c6a0bf598d30a031e7b34fb08d657f8b1d Mon Sep 17 00:00:00 2001 From: soumyashis Date: Sat, 22 Aug 2020 19:29:47 +0530 Subject: [PATCH 3/3] Reaname boolean fields for UserLocation --- javascript/components/UserLocation.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/javascript/components/UserLocation.js b/javascript/components/UserLocation.js index 9a9423349..ab4f338a4 100644 --- a/javascript/components/UserLocation.js +++ b/javascript/components/UserLocation.js @@ -137,13 +137,13 @@ class UserLocation extends React.Component { // after component unmount _isMounted = null; - locationManagerRequired = false; + _isLocationManagerRequired = false; async componentDidMount() { this._isMounted = true; await this.setLocationManager({ - required: this.needsLocationManagerRunning(), + required: this.isLocationManagerRequired(), }); if (this.renderMode === UserLocation.RenderMode.Native) { @@ -155,7 +155,7 @@ class UserLocation extends React.Component { async componentDidUpdate(prevProps) { await this.setLocationManager({ - required: this.needsLocationManagerRunning(), + required: this.isLocationManagerRequired(), }); if (this.props.minDisplacement !== prevProps.minDisplacement) { @@ -179,8 +179,8 @@ class UserLocation extends React.Component { * @return {Promise} */ async setLocationManager({required}) { - if (this.locationManagerRequired !== required) { - this.locationManagerRequired = required; + if (this._isLocationManagerRequired !== required) { + this._isLocationManagerRequired = required; if (required) { locationManager.addListener(this._onLocationUpdate); } else { @@ -191,11 +191,11 @@ class UserLocation extends React.Component { /** * - * If locationManager should be running + * If locationManager is required. * * @return {boolean} */ - needsLocationManagerRunning() { + isLocationManagerRequired() { if (this.props.renderMode === UserLocation.RenderMode.Native) { return false; }