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/stops-icon-type-display #105

Merged
merged 9 commits into from
Nov 6, 2024
59 changes: 32 additions & 27 deletions src/components/map/MapView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@
export let selectedRoute = null;
export let showRoute = false;
export let showRouteMap = false;
export let showAllStops = true;
export let stop = null;
export let mapProvider = null;
let selectedStopID = null;

const dispatch = createEventDispatcher();

let selectedStopID = null;
let mapInstance = null;
let mapElement;

let markers = [];
let allStops = [];
let routeReference = [];
let stopsCache = new Map();

const dispatch = createEventDispatcher();

function cacheKey(zoomLevel, boundingBox) {
const decimalPlaces = 2; // 2 decimal places equals between 0.5 and 1.1 km depending on where you are in the world.
const roundedBox = {
Expand Down Expand Up @@ -99,6 +96,10 @@
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) {
return;
}
await loadStopsAndAddMarkers(center.lat, center.lng, false, zoomLevel);
}, 300);

Expand All @@ -115,19 +116,24 @@
async function loadStopsAndAddMarkers(lat, lng, firstCall = false, zoomLevel = 15) {
const stopsData = await loadStopsForLocation(lat, lng, zoomLevel, firstCall);
const newStops = stopsData.data.list;
routeReference = stopsData.data.references.routes || [];
const routeReference = stopsData.data.references.routes || [];

const routeLookup = new Map(routeReference.map((route) => [route.id, route]));

// merge the stops routeIds with the route data
newStops.forEach((stop) => {
stop.routes = stop.routeIds.map((routeId) => routeLookup.get(routeId)).filter(Boolean);
});

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

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

Expand All @@ -151,25 +157,25 @@
}
}

$: if (showAllStops) {
allStops.forEach((s) => addMarker(s));
}

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

// 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));
}

function addMarker(s, routeReference) {
function addMarker(s) {
if (!mapInstance) {
console.error('Map not initialized yet');
return;
}

// check if the marker already exists
// // check if the marker already exists
const existingMarker = markers.find((marker) => marker.stop.id === s.id);

// if it does, don't add it again
Expand All @@ -179,13 +185,12 @@

let icon = faBus;

if (routeReference && routeReference.length > 0) {
const availableRoutes = s.routeIds
.map((id) => routeReference.find((r) => r.id === id))
.filter(Boolean);
const routeTypes = new Set(availableRoutes.map((r) => r.type));
const prioritizedType =
routePriorities.find((type) => routeTypes.has(type)) || RouteType.UNKNOWN;
if (s.routes && s.routes.length > 0) {
const routeTypes = new Set(s.routes.map((r) => r.type));
let prioritizedType = routePriorities.find((type) => routeTypes.has(type));
if (prioritizedType === undefined) {
prioritizedType = RouteType.UNKNOWN;
}
icon = prioritizedRouteTypeForDisplay(prioritizedType);
}

Expand Down
3 changes: 1 addition & 2 deletions src/components/stops/StopModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
import ModalPane from '$components/navigation/ModalPane.svelte';
import StopPane from '$components/stops/StopPane.svelte';

export let showAllStops;
export let stop;
</script>

<ModalPane on:close title={stop.name}>
<StopPane on:tripSelected on:updateRouteMap on:showAllStops {showAllStops} {stop} />
<StopPane on:tripSelected on:updateRouteMap {stop} />
</ModalPane>
6 changes: 0 additions & 6 deletions src/components/stops/StopPane.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

export let stop;
export let arrivalsAndDeparturesResponse = null;
export let showAllStops = true;

let arrivalsAndDepartures;
let loading = false;
Expand Down Expand Up @@ -48,10 +47,6 @@
}, 30000);
}

$: if (showAllStops) {
showTripDetails = false;
selectedTripDetails = null;
}
$: if (stop?.id && initialDataLoaded) {
clearInterval(interval);
resetDataFetchInterval(stop.id);
Expand Down Expand Up @@ -93,7 +88,6 @@
showTripDetails = false;
dispatch('tripSelected', null);
dispatch('updateRouteMap', { show: false });
dispatch('showAllStops');
}
</script>

Expand Down
4 changes: 3 additions & 1 deletion src/config/routeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ const prioritizedRouteTypeForDisplay = (routeType) => {
case RouteType.FERRY:
return faFerry;
case RouteType.LIGHT_RAIL:
return faTrainSubway;
case RouteType.SUBWAY:
case RouteType.CABLE_CAR:
return faTrainSubway;
case RouteType.CABLE_CAR:
return faCableCar;
case RouteType.RAIL:
return faTrain;
case RouteType.GONDOLA:
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Provider/GoogleMapProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class GoogleMapProvider {
target: container,
props: {
stop: options.stop,
icon: faBus,
icon: options.icon || faBus,
onClick: () => {
options.onClick && options.onClick();
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Provider/OpenStreetMapProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class OpenStreetMapProvider {
target: container,
props: {
stop: options.stop,
icon: faBus,
icon: options.icon || faBus,
onClick: options.onClick || (() => {})
}
});
Expand Down
9 changes: 0 additions & 9 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
let showRoute = false;
let selectedRoute = null;
let showRouteMap = false;
let showAllStops = false;
let showAllRoutesModal = false;
let showRouteModal;
let mapProvider = null;
Expand Down Expand Up @@ -95,12 +94,6 @@

function handleUpdateRouteMap(event) {
showRouteMap = event.detail.show;
showAllStops = !event.detail.show;
}

function handleShowAllStops() {
showAllStops = true;
showRouteMap = false;
}

function handleRouteSelected(event) {
Expand Down Expand Up @@ -166,9 +159,7 @@
on:close={closePane}
on:tripSelected={tripSelected}
on:updateRouteMap={handleUpdateRouteMap}
on:showAllStops={handleShowAllStops}
{stop}
{showAllStops}
/>
{/if}

Expand Down
Loading