Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/clear-markers-on-trip-planner-mode-with-state-enhancement #119

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 19 additions & 45 deletions src/components/map/MapView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
export let selectedRoute = null;
export let showRoute = false;
export let showRouteMap = false;
export let stop = null;
export let mapProvider = null;

let isTripPlanMoodActive = false;
let isTripPlanModeActive = false;

let selectedStopID = null;
let mapInstance = null;
let mapElement;
let markers = [];
Expand Down Expand Up @@ -99,8 +97,8 @@
const center = mapInstance.getCenter();
const zoomLevel = mapInstance.map.getZoom();

// Prevent fetching stops when a route is selected, we only fetch stops when we are see other stops
if (selectedRoute || showRoute) {
// Prevent fetching stops in the background when a route is selected or trip plan mode is active, we only fetch stops when we are see other stops
if (selectedRoute || showRoute || isTripPlanModeActive) {
return;
}
await loadStopsAndAddMarkers(center.lat, center.lng, false, zoomLevel);
Expand Down Expand Up @@ -129,15 +127,6 @@
});

allStops = [...new Map([...allStops, ...newStops].map((stop) => [stop.id, stop])).values()];

if (selectedRoute && !showRoute) {
allStops = [];
} else if (showRoute && selectedRoute) {
const stopsToShow = allStops.filter((s) => s.routeIds.includes(selectedRoute.id));
stopsToShow.forEach((s) => addMarker(s));
} else {
newStops.forEach((s) => addMarker(s));
}
}

function clearAllMarkers() {
Expand All @@ -147,42 +136,28 @@
markers = [];
}

$: if (selectedRoute && !showRoute) {
clearAllMarkers();
allStops = [];
}

$: if (stop && mapInstance) {
// TODO: make sure that these markers are deduped. i.e. we shouldn't
// show the same stop twice on the map
if (stop.id !== selectedStopID) {
addMarker(stop);
function updateMarkers() {
if (showRoute) {
const stopsToShow = allStops.filter((s) => s.routeIds.includes(selectedRoute.id));
stopsToShow.forEach((s) => addMarker(s));
} else if (!selectedRoute && !isTripPlanModeActive) {
allStops.forEach((s) => addMarker(s));
}
}

$: if (selectedRoute && showRoute) {
clearAllMarkers();
const stopsToShow = allStops.filter((s) => s.routeIds.includes(selectedRoute.id));
stopsToShow.forEach((s) => addMarker(s));
}

// If no route is selected and not showing a route, add all markers
// This ensures markers are re-added after a route is deselected
$: if (!selectedRoute && !showRoute) {
allStops.forEach((s) => addMarker(s));
}

// TODO: prevent fetch stops-for-location if the trip planner mode is on - we should do this after merge.
$: {
if (isTripPlanMoodActive) {
if (selectedRoute) {
clearAllMarkers();
} else {
if (!selectedRoute || !showRoute) {
allStops.forEach((s) => addMarker(s));
}
updateMarkers();
} else if (!isTripPlanModeActive) {
allStops.forEach((s) => addMarker(s));
}
}

$: if (isTripPlanModeActive) {
clearAllMarkers();
}

function addMarker(s) {
if (!mapInstance) {
console.error('Map not initialized yet');
Expand Down Expand Up @@ -213,7 +188,6 @@
icon: icon,
stop: s,
onClick: () => {
selectedStopID = s.id;
dispatch('stopSelected', { stop: s });
}
});
Expand All @@ -239,10 +213,10 @@
if (browser) {
const darkMode = document.documentElement.classList.contains('dark');
window.addEventListener('planTripTabClicked', () => {
isTripPlanMoodActive = true;
isTripPlanModeActive = true;
});
window.addEventListener('tabSwitched', () => {
isTripPlanMoodActive = false;
isTripPlanModeActive = false;
});
const event = new CustomEvent('themeChange', { detail: { darkMode } });
window.dispatchEvent(event);
Expand Down
1 change: 0 additions & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@
on:stopSelected={stopSelected}
{showRoute}
{showRouteMap}
{stop}
bind:mapProvider
/>
{/if}
Loading