mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
feat(native): a flight widget and a boarding-pass Live Activity
A new Flight widget on iOS (small to large and the lock screen) and Android shows the trip's next flight or the one in the air: both airports, the arc with the aircraft, a countdown the system keeps running, and the delay. It follows the feed's phase, so a late departure is not flown along the arc. Around a flight the travel day's Live Activity becomes a boarding pass with a running bar and countdown, in the Dynamic Island too.
This commit is contained in:
@@ -106,6 +106,18 @@ export interface TrekShellPlugin {
|
||||
nextMinutes?: number;
|
||||
/** Android: each stop's colour (#rrggbb) for the Live Update's segments. */
|
||||
colors?: (string | null)[];
|
||||
/** iOS: the day's flight near or in the air, drawn as a boarding pass; older binaries ignore it. */
|
||||
flight?: {
|
||||
number: string;
|
||||
from: string;
|
||||
to: string;
|
||||
dep: number;
|
||||
arr: number;
|
||||
depTime: string;
|
||||
arrTime: string;
|
||||
delay?: number;
|
||||
airborne?: boolean;
|
||||
};
|
||||
}): Promise<{ running: boolean }>;
|
||||
endTripDay(): Promise<void>;
|
||||
/** Feature storeKit (iOS) or playBilling (Android): the store's price strings for the products it knows. */
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
// FE-NATIVE-WIDGETFLIGHT-001 to FE-NATIVE-WIDGETFLIGHT-008
|
||||
import type { FlightLiveLeg } from '@trek/shared';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { pickWidgetLeg, widgetFlight } from './widgetFlight';
|
||||
import { liveFlightOf } from './widgets';
|
||||
|
||||
const DEP = Date.parse('2026-06-11T11:50:00Z');
|
||||
const ARR = Date.parse('2026-06-11T20:35:00Z');
|
||||
const H = 3_600_000;
|
||||
const CLOCK = { locale: 'en-GB', timeFormat: '24h' };
|
||||
|
||||
function leg(over: Partial<FlightLiveLeg> = {}): FlightLiveLeg {
|
||||
return {
|
||||
reservation_id: 6,
|
||||
leg_index: 0,
|
||||
callsign: 'DLH400',
|
||||
flight_number: 'LH 400',
|
||||
service_date: '2026-06-11',
|
||||
origin: { code: 'FRA', name: 'Frankfurt', lat: 50.03, lng: 8.57, timezone: 'Europe/Berlin' },
|
||||
destination: { code: 'JFK', name: 'New York JFK', lat: 40.64, lng: -73.78, timezone: 'America/New_York' },
|
||||
phase: 'scheduled',
|
||||
scheduled_departure: new Date(DEP).toISOString(),
|
||||
scheduled_arrival: new Date(ARR).toISOString(),
|
||||
departed_at: null,
|
||||
landed_at: null,
|
||||
estimated_arrival: null,
|
||||
departure_delay_min: null,
|
||||
arrival_delay_min: null,
|
||||
progress: null,
|
||||
distance_total_km: null,
|
||||
distance_remaining_km: null,
|
||||
position: null,
|
||||
aircraft: null,
|
||||
last_seen_at: null,
|
||||
fetched_at: null,
|
||||
stale: false,
|
||||
source: null,
|
||||
...over,
|
||||
};
|
||||
}
|
||||
|
||||
describe('pickWidgetLeg', () => {
|
||||
it('FE-NATIVE-WIDGETFLIGHT-001: prefers the flight in the air', () => {
|
||||
const ahead = leg({ reservation_id: 7 });
|
||||
const flying = leg({ reservation_id: 8, phase: 'airborne' });
|
||||
expect(pickWidgetLeg([ahead, flying], DEP)?.reservation_id).toBe(8);
|
||||
});
|
||||
|
||||
it('FE-NATIVE-WIDGETFLIGHT-002: takes the earliest within a week and drops one more than two hours landed', () => {
|
||||
const later = leg({
|
||||
reservation_id: 9,
|
||||
scheduled_departure: new Date(DEP + 48 * H).toISOString(),
|
||||
scheduled_arrival: new Date(ARR + 48 * H).toISOString(),
|
||||
});
|
||||
expect(pickWidgetLeg([later, leg()], DEP - H)?.reservation_id).toBe(6);
|
||||
expect(pickWidgetLeg([leg()], ARR + 3 * H)).toBeNull();
|
||||
expect(pickWidgetLeg([leg()], DEP - 8 * 24 * H)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('widgetFlight', () => {
|
||||
it('FE-NATIVE-WIDGETFLIGHT-003: sends the schedule in seconds and the clock at each airport', () => {
|
||||
const flight = widgetFlight([leg()], DEP - H, '/trips/1', CLOCK);
|
||||
expect(flight).toMatchObject({
|
||||
number: 'LH 400',
|
||||
from: 'FRA',
|
||||
to: 'JFK',
|
||||
dep: DEP / 1000,
|
||||
arr: ARR / 1000,
|
||||
depTime: '13:50',
|
||||
arrTime: '16:35',
|
||||
path: '/trips/1?booking=6',
|
||||
});
|
||||
expect(flight?.arrExpected).toBeUndefined();
|
||||
expect(flight?.landed).toBeUndefined();
|
||||
});
|
||||
|
||||
it('FE-NATIVE-WIDGETFLIGHT-004: adds what the feed observed while the flight is in the air', () => {
|
||||
const flight = widgetFlight(
|
||||
[
|
||||
leg({
|
||||
phase: 'airborne',
|
||||
departed_at: new Date(DEP + 20 * 60_000).toISOString(),
|
||||
estimated_arrival: new Date(ARR + 15 * 60_000).toISOString(),
|
||||
arrival_delay_min: 15,
|
||||
aircraft: { registration: 'D-AIXA', type: 'A359' },
|
||||
last_seen_at: new Date(DEP + 2 * H).toISOString(),
|
||||
}),
|
||||
],
|
||||
DEP + 2 * H,
|
||||
'/trips/1',
|
||||
CLOCK
|
||||
);
|
||||
expect(flight).toMatchObject({
|
||||
depActual: (DEP + 20 * 60_000) / 1000,
|
||||
arrExpected: (ARR + 15 * 60_000) / 1000,
|
||||
arrTimeExpected: '16:50',
|
||||
delay: 15,
|
||||
aircraft: 'A359 D-AIXA',
|
||||
});
|
||||
});
|
||||
|
||||
it('FE-NATIVE-WIDGETFLIGHT-005: says landed once touched down, and nothing without a flight', () => {
|
||||
expect(
|
||||
widgetFlight([leg({ phase: 'landed', landed_at: new Date(ARR).toISOString() })], ARR + H, '/trips/1', CLOCK)
|
||||
?.landed
|
||||
).toBe(true);
|
||||
expect(widgetFlight([], DEP, '/trips/1', CLOCK)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('liveFlightOf', () => {
|
||||
it('FE-NATIVE-WIDGETFLIGHT-006: holds the Live Activity from three hours before take-off to an hour after touchdown', () => {
|
||||
const flight = widgetFlight([leg()], DEP - H, '/trips/1', CLOCK);
|
||||
const at = (ms: number) => liveFlightOf(flight, ms / 1000);
|
||||
expect(at(DEP - 4 * H)).toBeUndefined();
|
||||
expect(at(DEP - 2 * H)).toMatchObject({
|
||||
number: 'LH 400',
|
||||
from: 'FRA',
|
||||
to: 'JFK',
|
||||
depTime: '13:50',
|
||||
arrTime: '16:35',
|
||||
});
|
||||
expect(at(ARR + 2 * H)).toBeUndefined();
|
||||
expect(liveFlightOf({ ...(flight as NonNullable<typeof flight>), landed: true }, (DEP + H) / 1000)).toBeUndefined();
|
||||
expect(liveFlightOf(undefined, DEP / 1000)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('widget phase', () => {
|
||||
it('FE-NATIVE-WIDGETFLIGHT-007: a late departure goes along as the expected one, and the flight in the air says so', () => {
|
||||
const late = widgetFlight([leg({ departure_delay_min: 40 })], DEP, '/trips/1', CLOCK);
|
||||
expect(late?.depExpected).toBe(DEP / 1000 + 40 * 60);
|
||||
expect(late?.airborne).toBeUndefined();
|
||||
expect(widgetFlight([leg({ phase: 'airborne' })], DEP + H, '/trips/1', CLOCK)?.airborne).toBe(true);
|
||||
expect(liveFlightOf(late, DEP / 1000)?.dep).toBe(DEP / 1000 + 40 * 60);
|
||||
});
|
||||
|
||||
it('FE-NATIVE-WIDGETFLIGHT-008: a flight the feed lost in the air stops being picked two hours after arrival', () => {
|
||||
expect(pickWidgetLeg([leg({ phase: 'airborne' })], ARR + 3 * H)).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,114 @@
|
||||
import type { FlightLiveLeg } from '@trek/shared';
|
||||
import { airportTime, toMs } from '../utils/flightLiveModel';
|
||||
|
||||
/**
|
||||
* The flight a home-screen widget follows: the trip's next one or the one in
|
||||
* the air. Times go along as epoch seconds so the widget moves its countdown
|
||||
* and its aircraft on with the clock between two snapshots; the clock times
|
||||
* at either airport go along formatted, since the widget has no zone data.
|
||||
* Optional on the native side (WidgetSnapshot.swift, WidgetSnapshot.kt): an
|
||||
* older app binary ignores it.
|
||||
*/
|
||||
export interface WidgetFlight {
|
||||
number: string;
|
||||
from: string;
|
||||
to: string;
|
||||
fromCity?: string;
|
||||
toCity?: string;
|
||||
/** Planned departure and arrival, epoch seconds. */
|
||||
dep: number;
|
||||
arr: number;
|
||||
/** Observed take-off, epoch seconds. */
|
||||
depActual?: number;
|
||||
/** Before take-off: the planned departure plus the delay seen so far, epoch seconds. */
|
||||
depExpected?: number;
|
||||
/** The feed sees the aircraft in the air right now. */
|
||||
airborne?: boolean;
|
||||
/** Touchdown once landed, else the estimate while in the air, epoch seconds. */
|
||||
arrExpected?: number;
|
||||
landed?: boolean;
|
||||
/** Clock times at the airports: planned departure, planned arrival, expected arrival. */
|
||||
depTime: string;
|
||||
arrTime: string;
|
||||
arrTimeExpected?: string;
|
||||
/** Minutes late against the schedule (negative when early). */
|
||||
delay?: number;
|
||||
/** "A359 D-AIXA". */
|
||||
aircraft?: string;
|
||||
/** When the feed last heard the aircraft, epoch seconds. */
|
||||
seen?: number;
|
||||
path: string;
|
||||
}
|
||||
|
||||
/** How long after landing the widget keeps the flight before moving on to the next. */
|
||||
const KEEP_AFTER_LANDING_S = 2 * 60 * 60;
|
||||
/** How far ahead a flight is worth the widget's attention. */
|
||||
const LOOK_AHEAD_S = 7 * 24 * 60 * 60;
|
||||
|
||||
const sec = (ms: number | null): number | undefined => (ms == null ? undefined : Math.round(ms / 1000));
|
||||
|
||||
/**
|
||||
* The leg to show: one in the air or rolling first, else the earliest whose
|
||||
* arrival (with two hours' grace) is still ahead and that departs within a week.
|
||||
*/
|
||||
export function pickWidgetLeg(legs: FlightLiveLeg[], nowMs: number): FlightLiveLeg | null {
|
||||
const now = nowMs / 1000;
|
||||
const arrivalOf = (l: FlightLiveLeg) =>
|
||||
sec(toMs(l.landed_at) ?? toMs(l.estimated_arrival) ?? toMs(l.scheduled_arrival));
|
||||
// A leg the feed lost in the air still counts only until two hours after its arrival.
|
||||
const moving = legs.find((l) => {
|
||||
const arr = arrivalOf(l);
|
||||
return (l.phase === 'airborne' || l.phase === 'taxiing') && (arr == null || arr + KEEP_AFTER_LANDING_S > now);
|
||||
});
|
||||
if (moving) return moving;
|
||||
return (
|
||||
legs
|
||||
.filter((l) => {
|
||||
const dep = sec(toMs(l.scheduled_departure));
|
||||
const arr = sec(toMs(l.landed_at) ?? toMs(l.scheduled_arrival));
|
||||
return dep != null && arr != null && arr + KEEP_AFTER_LANDING_S > now && dep - now < LOOK_AHEAD_S;
|
||||
})
|
||||
.sort((a, b) => (toMs(a.scheduled_departure) ?? 0) - (toMs(b.scheduled_departure) ?? 0))[0] ?? null
|
||||
);
|
||||
}
|
||||
|
||||
export function widgetFlight(
|
||||
legs: FlightLiveLeg[],
|
||||
nowMs: number,
|
||||
tripPath: string,
|
||||
clock: { locale: string; timeFormat: string }
|
||||
): WidgetFlight | undefined {
|
||||
const leg = pickWidgetLeg(legs, nowMs);
|
||||
const dep = leg ? toMs(leg.scheduled_departure) : null;
|
||||
const arr = leg ? toMs(leg.scheduled_arrival) : null;
|
||||
if (!leg || dep == null || arr == null) return undefined;
|
||||
const time = (ms: number, tz: string | null) => airportTime(ms, tz, clock.locale, clock.timeFormat);
|
||||
const landed = toMs(leg.landed_at);
|
||||
const expected = landed ?? (leg.phase === 'airborne' ? toMs(leg.estimated_arrival) : null);
|
||||
const aircraft = [leg.aircraft?.type, leg.aircraft?.registration].filter(Boolean).join(' ');
|
||||
const delay = leg.arrival_delay_min ?? leg.departure_delay_min;
|
||||
return {
|
||||
number: leg.flight_number,
|
||||
from: leg.origin.code,
|
||||
to: leg.destination.code,
|
||||
...(leg.origin.name ? { fromCity: leg.origin.name } : {}),
|
||||
...(leg.destination.name ? { toCity: leg.destination.name } : {}),
|
||||
dep: Math.round(dep / 1000),
|
||||
arr: Math.round(arr / 1000),
|
||||
...(leg.departed_at ? { depActual: sec(toMs(leg.departed_at)) } : {}),
|
||||
...(!leg.departed_at && leg.departure_delay_min != null && leg.departure_delay_min > 0
|
||||
? { depExpected: Math.round(dep / 1000) + leg.departure_delay_min * 60 }
|
||||
: {}),
|
||||
...(leg.phase === 'airborne' ? { airborne: true } : {}),
|
||||
...(expected != null
|
||||
? { arrExpected: sec(expected), arrTimeExpected: time(expected, leg.destination.timezone) }
|
||||
: {}),
|
||||
...(landed != null ? { landed: true } : {}),
|
||||
depTime: time(dep, leg.origin.timezone),
|
||||
arrTime: time(arr, leg.destination.timezone),
|
||||
...(delay != null ? { delay } : {}),
|
||||
...(aircraft ? { aircraft } : {}),
|
||||
...(leg.last_seen_at ? { seen: sec(toMs(leg.last_seen_at)) } : {}),
|
||||
path: `${tripPath}?booking=${leg.reservation_id}`,
|
||||
};
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import { isNativeApp } from './platform';
|
||||
import { reportNativeFailure } from './reportFailure';
|
||||
import { TrekShell } from './trekShell';
|
||||
import { stopLook, type WidgetBooking, type WidgetPacking, type WidgetTodos } from './widgetExtras';
|
||||
import type { WidgetFlight } from './widgetFlight';
|
||||
import type { WidgetAtlas, WidgetCollab, WidgetPackingDetail } from './widgetOnline';
|
||||
|
||||
/**
|
||||
@@ -48,6 +49,27 @@ export interface WidgetLabels {
|
||||
votes?: string;
|
||||
noMessages?: string;
|
||||
departure?: string;
|
||||
/** The flight widget: its title and the words around the moment. */
|
||||
flight?: string;
|
||||
/** Contains "{time}", where the widget puts its own running countdown. */
|
||||
flightDeparts?: string;
|
||||
flightInAir?: string;
|
||||
/** Contains "{time}". */
|
||||
flightLands?: string;
|
||||
flightLanded?: string;
|
||||
flightOnTime?: string;
|
||||
/** Contains "{m}". */
|
||||
flightLate?: string;
|
||||
/** Contains "{m}". */
|
||||
flightEarly?: string;
|
||||
flightEstimated?: string;
|
||||
/** Past the departure time with the aircraft not yet in the air. */
|
||||
flightReady?: string;
|
||||
noFlight?: string;
|
||||
/** Android, which has no running countdown text: "{d} d {h} h", "{h} h {m} min", "{m} min". */
|
||||
durDaysHours?: string;
|
||||
durHoursMinutes?: string;
|
||||
durMinutes?: string;
|
||||
}
|
||||
|
||||
interface WidgetStop {
|
||||
@@ -89,6 +111,7 @@ export interface WidgetSnapshot {
|
||||
packing?: WidgetPacking & Partial<WidgetPackingDetail>;
|
||||
atlas?: WidgetAtlas;
|
||||
collab?: WidgetCollab;
|
||||
flight?: WidgetFlight;
|
||||
labels: WidgetLabels;
|
||||
}
|
||||
|
||||
@@ -116,6 +139,7 @@ interface SnapshotExtras {
|
||||
packing?: (WidgetPacking & Partial<WidgetPackingDetail>) | null;
|
||||
atlas?: WidgetAtlas | null;
|
||||
collab?: WidgetCollab | null;
|
||||
flight?: WidgetFlight;
|
||||
}
|
||||
|
||||
export interface TripDay {
|
||||
@@ -314,6 +338,7 @@ export function buildSnapshot(
|
||||
if (extras.packing) snapshot.packing = extras.packing;
|
||||
if (extras.atlas) snapshot.atlas = extras.atlas;
|
||||
if (extras.collab) snapshot.collab = extras.collab;
|
||||
if (extras.flight) snapshot.flight = extras.flight;
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
@@ -330,6 +355,41 @@ interface TripDayState {
|
||||
colors?: (string | null)[];
|
||||
done: number;
|
||||
total: number;
|
||||
/** iOS: the day's flight while it is near or in the air, drawn as a boarding pass. */
|
||||
flight?: LiveFlight;
|
||||
}
|
||||
|
||||
/** The flight a Live Activity follows: times in epoch seconds, the clocks formatted. */
|
||||
interface LiveFlight {
|
||||
number: string;
|
||||
from: string;
|
||||
to: string;
|
||||
dep: number;
|
||||
arr: number;
|
||||
depTime: string;
|
||||
arrTime: string;
|
||||
delay?: number;
|
||||
/** In the air when the app last looked: the countdown then runs to touchdown. */
|
||||
airborne?: boolean;
|
||||
}
|
||||
|
||||
/** From three hours before take-off until an hour after touchdown the activity is about the flight. */
|
||||
export function liveFlightOf(flight: WidgetFlight | undefined, nowSeconds: number): LiveFlight | undefined {
|
||||
if (!flight || flight.landed) return undefined;
|
||||
const dep = flight.depActual ?? flight.depExpected ?? flight.dep;
|
||||
const arr = flight.arrExpected ?? flight.arr;
|
||||
if (nowSeconds < dep - 3 * 3600 || nowSeconds > arr + 3600) return undefined;
|
||||
return {
|
||||
number: flight.number,
|
||||
from: flight.from,
|
||||
to: flight.to,
|
||||
dep,
|
||||
arr,
|
||||
depTime: flight.depTime,
|
||||
arrTime: flight.arrTimeExpected ?? flight.arrTime,
|
||||
...(flight.delay != null ? { delay: flight.delay } : {}),
|
||||
...(flight.airborne ? { airborne: true } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user