feat(mobile): one Maps button for every map app, actions as one glass group

The day plan offered Google Maps and CoMaps as two tiles. One Maps button now
opens the day in whichever app the traveller picks: Google Maps, Apple Maps
with the stops between as waypoints, CoMaps, Waze and on Android any other
map app, the last two to the first stop, which they say. The preferred map
app from the settings opens straight away.

The day's actions sit together on one glass surface as round icons over
their labels, and the up-next card wears the same surface as the timeline
card under it.
This commit is contained in:
Maurice
2026-11-06 12:41:57 +01:00
parent 6d093d1102
commit c5cb39c27b
36 changed files with 378 additions and 28 deletions
@@ -0,0 +1,49 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { dayRouteTargets, generateAppleMapsUrl, generateGeoUrl, generateWazeUrl } from './dayRouteApps';
const A = { lat: 35.71, lng: 139.79, name: 'Senso-ji' };
const B = { lat: 35.72, lng: 139.77, name: 'Ueno Park' };
const C = { lat: 35.66, lng: 139.7, name: 'Shibuya' };
function userAgent(ua: string) {
vi.spyOn(navigator, 'userAgent', 'get').mockReturnValue(ua);
}
afterEach(() => vi.restoreAllMocks());
describe('dayRouteApps', () => {
it('opens one stop in Apple Maps as a named pin', () => {
expect(generateAppleMapsUrl([A])).toBe('https://maps.apple.com/?ll=35.71,139.79&q=Senso-ji');
});
it('hands Apple Maps the whole day, the stops between as waypoints, in the travel mode', () => {
expect(generateAppleMapsUrl([A, B, C], 'walking')).toBe(
'https://maps.apple.com/directions?source=35.71,139.79&destination=35.66,139.7&waypoint=35.72,139.77&mode=walking'
);
});
it('sends Waze and geo: to the first stop, since they take one place', () => {
expect(generateWazeUrl([A, B])).toBe('https://waze.com/ul?ll=35.71,139.79&navigate=yes');
expect(generateGeoUrl([A, B])).toBe('geo:35.71,139.79?q=35.71,139.79(Senso-ji)');
expect(generateWazeUrl([])).toBeNull();
});
it('offers Apple Maps on an iPhone and marks the one-place apps for a multi-stop day', () => {
userAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X)');
const targets = dayRouteTargets([A, B], 'driving');
expect(targets.map((t) => t.id)).toEqual(['google', 'apple', 'comaps', 'waze']);
expect(targets.find((t) => t.id === 'waze')?.firstStopOnly).toBe(true);
expect(targets.find((t) => t.id === 'google')?.firstStopOnly).toBeUndefined();
});
it('offers the generic map-app link on Android instead of Apple Maps', () => {
userAgent('Mozilla/5.0 (Linux; Android 15)');
expect(dayRouteTargets([A], 'driving').map((t) => t.id)).toEqual(['google', 'comaps', 'waze', 'geo']);
// A single stop is the whole day, so nothing is cut short.
expect(dayRouteTargets([A], 'driving').some((t) => t.firstStopOnly)).toBe(false);
});
it('offers nothing for a day without a located stop', () => {
expect(dayRouteTargets([], 'driving')).toEqual([]);
});
});
@@ -0,0 +1,87 @@
import {
generateCoMapsUrl,
generateGoogleMapsUrl,
type NamedWaypoint,
type RouteProfileKey,
} from '../Map/RouteCalculator';
import { showsAppleMaps, showsGeoUri, type NavigationAppId } from './placeNavigation';
/**
* The map apps a day's route can be handed to, behind the plan's one Maps
* button. Same stops as every other export (dayExportStops), so the app shows
* the day the plan shows.
*
* Not every app takes a whole route. Google Maps, Apple Maps and CoMaps do (each
* in its own way, see the builders); Waze and the generic geo: link navigate to
* one place only, so they lead to the first stop and say so rather than quietly
* dropping the rest of the day.
*/
export interface DayRouteTarget {
id: NavigationAppId;
/** Product name, never translated; the generic entry carries labelKey instead. */
label: string;
labelKey?: string;
url: string;
/** True when the app only gets the first stop, not the whole route. */
firstStopOnly?: boolean;
}
/** Apple Maps' directions modes. Cycling exists there; anything else drives. */
function appleMode(profile: RouteProfileKey): string {
if (profile === 'walking') return 'walking';
if (profile === 'cycling') return 'cycling';
return 'driving';
}
const point = (p: NamedWaypoint) => `${p.lat},${p.lng}`;
const label = (p: NamedWaypoint) => encodeURIComponent(p.name?.trim() || point(p));
/**
* Apple Maps for a day: one stop opens as a pin with its name, more go as
* directions with the stops between as waypoints (Apple's unified Maps URLs,
* which iOS, macOS and the web version all read).
*/
export function generateAppleMapsUrl(places: NamedWaypoint[], profile: RouteProfileKey = 'driving'): string | null {
const valid = places.filter((p) => p.lat != null && p.lng != null);
if (valid.length === 0) return null;
if (valid.length === 1) return `https://maps.apple.com/?ll=${point(valid[0])}&q=${label(valid[0])}`;
const [first, ...rest] = valid;
const last = rest[rest.length - 1];
const between = rest
.slice(0, -1)
.map((p) => `&waypoint=${point(p)}`)
.join('');
return `https://maps.apple.com/directions?source=${point(first)}&destination=${point(last)}${between}&mode=${appleMode(profile)}`;
}
/** Waze navigates to one place; the day's first stop is where the drive starts. */
export function generateWazeUrl(places: NamedWaypoint[]): string | null {
const first = places.find((p) => p.lat != null && p.lng != null);
return first ? `https://waze.com/ul?ll=${point(first)}&navigate=yes` : null;
}
/** The generic Android link, handed to whichever map app is installed; one place too. */
export function generateGeoUrl(places: NamedWaypoint[]): string | null {
const first = places.find((p) => p.lat != null && p.lng != null);
return first ? `geo:${point(first)}?q=${point(first)}(${label(first)})` : null;
}
/** Every app the day can be opened in on this device, in the order they are offered. */
export function dayRouteTargets(stops: NamedWaypoint[], profile: RouteProfileKey): DayRouteTarget[] {
const out: DayRouteTarget[] = [];
const push = (target: Omit<DayRouteTarget, 'url'>, url: string | null) => {
if (url) out.push({ ...target, url });
};
push({ id: 'google', label: 'Google Maps' }, generateGoogleMapsUrl(stops));
if (showsAppleMaps()) push({ id: 'apple', label: 'Apple Maps' }, generateAppleMapsUrl(stops, profile));
push({ id: 'comaps', label: 'CoMaps' }, generateCoMapsUrl(stops, profile));
const located = stops.filter((p) => p.lat != null && p.lng != null).length;
push({ id: 'waze', label: 'Waze', firstStopOnly: located > 1 }, generateWazeUrl(stops));
if (showsGeoUri()) {
push(
{ id: 'geo', label: 'geo:', labelKey: 'inspector.otherMapApp', firstStopOnly: located > 1 },
generateGeoUrl(stops)
);
}
return out;
}