Skip to content

Commit ebfd277

Browse files
committed
Refactor MyTripToCanada for improved code clarity and performance, including changing instance variables to static and enhancing marker animation logic
Signed-off-by: makbn <[email protected]>
1 parent 19e5073 commit ebfd277

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

jlmap-vaadin-demo/src/main/java/io/github/makbn/vaadin/demo/views/MyTripToCanada.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,70 +33,72 @@
3333

3434
@Route("my-trip-to-canada")
3535
public class MyTripToCanada extends VerticalLayout {
36-
public static final String MAP_API_KEY = "rNGhTaIpQWWH7C6QGKzF";
36+
private static final String MAP_API_KEY = "rNGhTaIpQWWH7C6QGKzF";
3737
private static final Logger log = LoggerFactory.getLogger(MyTripToCanada.class);
38+
private static final String TRANSPARENT = "#00FFFFFF";
3839

3940
// Journey coordinates
40-
private final JLLatLng SARI = new JLLatLng(36.5633, 53.0601);
41-
private final JLLatLng TEHRAN = new JLLatLng(35.6892, 51.3890);
42-
private final JLLatLng DOHA = new JLLatLng(25.2854, 51.5310);
43-
private final JLLatLng MONTREAL = new JLLatLng(45.5017, -73.5673);
44-
private final JLLatLng CALGARY = new JLLatLng(51.0447, -114.0719);
41+
private static final JLLatLng SARI = new JLLatLng(36.5633, 53.0601);
42+
private static final JLLatLng TEHRAN = new JLLatLng(35.6892, 51.3890);
43+
private static final JLLatLng DOHA = new JLLatLng(25.2854, 51.5310);
44+
private static final JLLatLng MONTREAL = new JLLatLng(45.5017, -73.5673);
45+
private static final JLLatLng CALGARY = new JLLatLng(51.0447, -114.0719);
4546

4647
// Custom icons for different stages of the journey
47-
private final JLIcon CAR_ICON = JLIcon.builder()
48+
private static final JLIcon CAR_ICON = JLIcon.builder()
4849
.iconUrl("https://cdn-icons-png.flaticon.com/512/3097/3097220.png")
4950
.iconSize(new JLPoint(64, 64))
5051
.iconAnchor(new JLPoint(24, 24))
5152
.build();
5253

53-
private final JLIcon AIRPLANE_ICON = JLIcon.builder()
54+
private static final JLIcon AIRPLANE_ICON = JLIcon.builder()
5455
.iconUrl("https://cdn-icons-png.flaticon.com/512/3182/3182857.png")
5556
.iconSize(new JLPoint(64, 64))
5657
.shadowAnchor(new JLPoint(26, 26))
5758
.iconAnchor(new JLPoint(24, 24))
5859
.build();
59-
private final JLIcon EAST_AIRPLANE_ICON = JLIcon.builder()
60+
private static final JLIcon EAST_AIRPLANE_ICON = JLIcon.builder()
6061
.iconUrl("https://cdn-icons-png.flaticon.com/512/1058/1058318.png")
6162
.iconSize(new JLPoint(64, 64))
6263
.shadowAnchor(new JLPoint(26, 26))
6364
.iconAnchor(new JLPoint(24, 24))
6465
.build();
6566

66-
private final JLIcon BALLOON = JLIcon.builder()
67+
private static final JLIcon BALLOON = JLIcon.builder()
6768
.iconUrl("https://cdn-icons-png.flaticon.com/512/1926/1926313.png")
6869
.iconSize(new JLPoint(64, 64))
6970
.iconAnchor(new JLPoint(24, 24))
7071
.build();
7172

72-
private final JLIcon BRIEFCASE_ICON = JLIcon.builder()
73+
private static final JLIcon BRIEFCASE_ICON = JLIcon.builder()
7374
.iconUrl("https://cdn-icons-png.flaticon.com/512/5376/5376980.png")
7475
.iconSize(new JLPoint(64, 64))
7576
.iconAnchor(new JLPoint(24, 24))
7677
.build();
7778

78-
private final JLIcon DOCUMENT_ICON = JLIcon.builder()
79+
private static final JLIcon DOCUMENT_ICON = JLIcon.builder()
7980
.iconUrl("https://cdn-icons-png.flaticon.com/512/3127/3127363.png")
8081
.iconSize(new JLPoint(64, 64))
8182
.iconAnchor(new JLPoint(24, 24))
8283
.build();
83-
private final JLIcon PASSPORT_ICON = JLIcon.builder()
84+
private static final JLIcon PASSPORT_ICON = JLIcon.builder()
8485
.iconUrl("https://cdn-icons-png.flaticon.com/512/18132/18132911.png")
8586
.iconSize(new JLPoint(64, 64))
8687
.iconAnchor(new JLPoint(24, 24))
8788
.build();
8889

89-
private final JLIcon HOUSE_ICON = JLIcon.builder()
90+
private static final JLIcon HOUSE_ICON = JLIcon.builder()
9091
.iconUrl("https://cdn-icons-png.flaticon.com/512/3750/3750400.png")
9192
.iconSize(new JLPoint(64, 64))
9293
.iconAnchor(new JLPoint(24, 48))
9394
.build();
9495

95-
private JLMapView mapView;
96-
private JLMarker currentMarker;
97-
private JLPolyline currentPath;
9896
private final List<MessageListItem> messages = new ArrayList<>();
99-
private MessageList messageList;
97+
private final JLMapView mapView;
98+
private final MessageList messageList;
99+
100+
private transient JLMarker currentMarker;
101+
private transient JLPolyline currentPath;
100102

101103
public MyTripToCanada() {
102104
setSizeFull();
@@ -347,7 +349,7 @@ private void animateSegment(JLLatLng start, JLLatLng end, JLIcon icon, String pa
347349
// Animate marker along path
348350
log.info("Starting marker animation");
349351
UI.getCurrent().push();
350-
animateMarkerAlongPath(icon, pathPoints, duration, () -> {
352+
animateMarkerAlongPath(icon, pathPoints, pathColor, duration, () -> {
351353
// Remove popups after animation
352354
departurePopup.remove();
353355
destinationPopup.remove();
@@ -359,7 +361,7 @@ private void animateSegment(JLLatLng start, JLLatLng end, JLIcon icon, String pa
359361
});
360362
}
361363

362-
private void animateMarkerAlongPath(JLIcon icon, JLLatLng[] path, int duration, Runnable onComplete) {
364+
private void animateMarkerAlongPath(JLIcon icon, JLLatLng[] path, String pathColor, int duration, Runnable onComplete) {
363365
UI ui = UI.getCurrent();
364366

365367
// Increase animation steps to 200 for smoother animation (10x more than before)
@@ -387,8 +389,9 @@ private void animateMarkerAlongPath(JLIcon icon, JLLatLng[] path, int duration,
387389
currentPath = mapView.getVectorLayer().addPolyline(
388390
path,
389391
JLOptions.DEFAULT.toBuilder()
390-
.fillColor(JLColor.fromHex("#00FFFFFF"))
391-
.color(JLColor.fromHex("#00FFFFFF"))
392+
.fillColor(JLColor.fromHex(TRANSPARENT))
393+
.color(JLColor.fromHex(pathColor))
394+
.stroke(true)
392395
.fill(false)
393396
.weight(4)
394397
.opacity(0.7)

0 commit comments

Comments
 (0)