From d0490586813f7bc6ae17a0f7fa262f818f26c793 Mon Sep 17 00:00:00 2001 From: Paola De Bartolo Date: Mon, 10 Jun 2024 15:18:38 -0300 Subject: [PATCH] refactor!: internally manage trackLocationId Close #133 --- .../vaadin/addons/googlemaps/GoogleMap.java | 52 ++++++++++++------- .../addons/googlemaps/TrackLocationDemo.java | 37 +++++++------ 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMap.java b/src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMap.java index 5560784..eff7e16 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMap.java +++ b/src/main/java/com/flowingcode/vaadin/addons/googlemaps/GoogleMap.java @@ -51,6 +51,8 @@ @JsModule("./googlemaps/geolocation.js") public class GoogleMap extends Component implements HasSize { + private Integer trackLocationId = null; + /** Base map types supported by Google Maps. */ public enum MapType { ROADMAP, @@ -619,28 +621,37 @@ public Registration addGeolocationErrorEventListener( * *

Geolocation requires that the user gives consent to location sharing when prompted by the * browser.

+ * + * @throws IllegalStateException if a tracking location session is already active. + * The current session must be stopped before starting a new one. */ public void trackLocation() { - getElement().executeJs("return geolocation.trackLocation($0)", this).then(Integer.class, - trackLocationId -> { - ComponentUtil.fireEvent(this, - new LocationTrackingActivatedEvent(this, false, trackLocationId)); - }); + if (this.getTrackLocationId() == null) { + getElement().executeJs("return geolocation.trackLocation($0)", this).then(Integer.class, + trackLocationId -> { + this.trackLocationId = trackLocationId; + ComponentUtil.fireEvent(this, new LocationTrackingActivatedEvent(this, false)); + }); + } else { + throw new IllegalStateException( + "A tracking location session is already active. Please stop the current session before starting a new one."); + } + } + + /** + * Returns track location id if a location tracking was activated. + * + * @return the location tracking id + */ + public Integer getTrackLocationId() { + return trackLocationId; } /** Event that is fired when activating location tracking. */ public class LocationTrackingActivatedEvent extends ComponentEvent { - private Integer trackLocationId; - - public LocationTrackingActivatedEvent(GoogleMap source, boolean fromClient, - Integer trackLocationId) { + public LocationTrackingActivatedEvent(GoogleMap source, boolean fromClient) { super(source, fromClient); - this.trackLocationId = trackLocationId; - } - - public Integer getTrackLocationId() { - return trackLocationId; } } @@ -657,14 +668,15 @@ public Registration addLocationTrackingActivatedEventListener( } /** - * Stops location tracking. - * - * @param trackLocationId the id of the current activated location tracking + * Stops the current location tracking session. */ - public void stopTrackLocation(Integer trackLocationId) { - getElement().executeJs("geolocation.clearTracking($0)", trackLocationId); + public void stopTrackLocation() { + if(trackLocationId != null) { + getElement().executeJs("geolocation.clearTracking($0)", trackLocationId); + trackLocationId = null; + } } - + /** * Returns a {@link CompletableFuture} containing the map current {@link LatLonBounds bounds}. * diff --git a/src/test/java/com/flowingcode/vaadin/addons/googlemaps/TrackLocationDemo.java b/src/test/java/com/flowingcode/vaadin/addons/googlemaps/TrackLocationDemo.java index 31d9c0b..b42e3d1 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/googlemaps/TrackLocationDemo.java +++ b/src/test/java/com/flowingcode/vaadin/addons/googlemaps/TrackLocationDemo.java @@ -24,6 +24,7 @@ import com.flowingcode.vaadin.addons.googlemaps.GoogleMap.MapType; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.notification.Notification; +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; @@ -33,8 +34,6 @@ @SuppressWarnings("serial") public class TrackLocationDemo extends AbstractGoogleMapsDemo { - private Integer trackLocationId = null; - @Override protected void createGoogleMapsDemo(String apiKey) { GoogleMap gmaps = new GoogleMap(apiKey, null, null); @@ -43,13 +42,21 @@ protected void createGoogleMapsDemo(String apiKey) { gmaps.setZoom(15); add(gmaps); - // create button to activate location tracking - Button startLocationTrackingButton = - new Button("Start tracking my location", e -> gmaps.trackLocation()); - // create button to stop location tracking - Button stopLocationTrackingButton = - new Button("Stop tracking my location", e -> gmaps.stopTrackLocation(trackLocationId)); - add(startLocationTrackingButton, stopLocationTrackingButton); + // create buttons to activate/stop location tracking + Button startLocationTrackingButton = new Button("Start tracking my location"); + Button stopLocationTrackingButton = new Button("Stop tracking my location"); + startLocationTrackingButton.addClickListener(e -> { + gmaps.trackLocation(); + stopLocationTrackingButton.setEnabled(true); + }); + startLocationTrackingButton.setDisableOnClick(true); + stopLocationTrackingButton.addClickListener(e -> { + gmaps.stopTrackLocation(); + startLocationTrackingButton.setEnabled(true); + }); + stopLocationTrackingButton.setEnabled(false); + stopLocationTrackingButton.setDisableOnClick(true); + add(new HorizontalLayout(startLocationTrackingButton, stopLocationTrackingButton)); // create marker to track location GoogleMapMarker locationMarker = new GoogleMapMarker(); @@ -57,9 +64,10 @@ protected void createGoogleMapsDemo(String apiKey) { locationMarker.setDraggable(false); gmaps.addMarker(locationMarker); - // add listener to obtain id when track location is activated + // add listener to show notification as track location is activated gmaps.addLocationTrackingActivatedEventListener(ev -> { - trackLocationId = ev.getTrackLocationId(); + Notification + .show("Location tracking was activated with track id: " + gmaps.getTrackLocationId()); }); // add listener to know when location was updated and update location marker position @@ -68,9 +76,8 @@ protected void createGoogleMapsDemo(String apiKey) { }); // add listener to capture geolocation error - gmaps.addGeolocationErrorEventListener(e -> { - if (!e.isBrowserHasGeolocationSupport()) - Notification.show("Your browser doesn't support geolocation."); - }); + gmaps.addGeolocationErrorEventListener(e -> Notification.show(e.isBrowserHasGeolocationSupport() + ? "The geolocation service failed on retrieving your location." + : "Your browser doesn't support geolocation.")); } }