mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +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:
@@ -16,11 +16,13 @@ import { TrekShell } from '../../native/trekShell';
|
||||
import { applyWatchActions, buildWatchSnapshot, publishWatch, takeWatchActions, watchActive } from '../../native/watch';
|
||||
import { onWatchListsChange } from '../../native/watchListChanges';
|
||||
import { bookingsAhead, packingSummary, todosSummary, tripPlace } from '../../native/widgetExtras';
|
||||
import { widgetFlight } from '../../native/widgetFlight';
|
||||
import { widgetCover, widgetWeather } from '../../native/widgetMedia';
|
||||
import { atlasSummary, collabSummary, packingDetail, type WidgetAtlas } from '../../native/widgetOnline';
|
||||
import {
|
||||
buildSnapshot,
|
||||
liveDayDue,
|
||||
liveFlightOf,
|
||||
pickTrip,
|
||||
publishWidgets,
|
||||
todayOf,
|
||||
@@ -32,9 +34,10 @@ import {
|
||||
type WidgetLabels,
|
||||
} from '../../native/widgets';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
import { useFlightLiveStore } from '../../store/flightLiveStore';
|
||||
import { useSettingsStore } from '../../store/settingsStore';
|
||||
import { useTripStore } from '../../store/tripStore';
|
||||
import type { Day, Place, TranslationFn, Trip } from '../../types';
|
||||
import type { Day, Place, Reservation, TranslationFn, Trip } from '../../types';
|
||||
import { formatTime } from '../../utils/formatters';
|
||||
import { localDateStr } from '../../vacay/monthGrid';
|
||||
|
||||
@@ -72,6 +75,20 @@ function labels(t: TranslationFn): WidgetLabels {
|
||||
votes: t('native.widget.votes'),
|
||||
noMessages: t('native.widget.noMessages'),
|
||||
departure: t('native.widget.departure'),
|
||||
flight: t('reservations.type.flight'),
|
||||
flightDeparts: t('flightLive.departsIn'),
|
||||
flightInAir: t('flightLive.airborne'),
|
||||
flightLands: t('flightLive.landsIn'),
|
||||
flightLanded: t('flightLive.landed'),
|
||||
flightOnTime: t('flightLive.onTime'),
|
||||
flightLate: t('flightLive.late'),
|
||||
flightEarly: t('flightLive.early'),
|
||||
flightEstimated: t('flightLive.estimated'),
|
||||
flightReady: t('flightLive.taxiing'),
|
||||
noFlight: t('flightLive.noFlight'),
|
||||
durDaysHours: t('flightLive.durDaysHours'),
|
||||
durHoursMinutes: t('flightLive.durHoursMinutes'),
|
||||
durMinutes: t('flightLive.durMinutes'),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -88,9 +105,24 @@ interface SyncContext {
|
||||
language: string;
|
||||
unit: string | undefined;
|
||||
time: (value: string) => string;
|
||||
timeFormat: string;
|
||||
}
|
||||
|
||||
async function tripExtras(trip: Trip, days: Day[], { today, language, unit, time }: SyncContext) {
|
||||
/**
|
||||
* The trip's next or current flight for the flight widget, from the live
|
||||
* states the server worked out (its schedule in UTC, the feed's position). Read
|
||||
* fresh before the snapshot when the trip has flights and the device is online;
|
||||
* offline the last states held in memory serve.
|
||||
*/
|
||||
async function flightOf(trip: Trip, reservations: Reservation[], path: string, context: SyncContext) {
|
||||
if (!reservations.some((r) => r.type === 'flight')) return undefined;
|
||||
await useFlightLiveStore.getState().load(trip.id);
|
||||
const legs = Object.values(useFlightLiveStore.getState().trips[trip.id]?.legs ?? {});
|
||||
return widgetFlight(legs, Date.now(), path, { locale: context.language, timeFormat: context.timeFormat });
|
||||
}
|
||||
|
||||
async function tripExtras(trip: Trip, days: Day[], context: SyncContext) {
|
||||
const { today, language, unit, time } = context;
|
||||
const [places, reservations, todos, packing, memberRows] = await Promise.all([
|
||||
offlineDb.places.where('trip_id').equals(trip.id).toArray(),
|
||||
offlineDb.reservations.where('trip_id').equals(trip.id).toArray(),
|
||||
@@ -110,7 +142,7 @@ async function tripExtras(trip: Trip, days: Day[], { today, language, unit, time
|
||||
days.find((d) => (d.date ?? '').slice(0, 10) === weatherDate),
|
||||
places
|
||||
);
|
||||
const [weather, cover, bags, collab] = await Promise.all([
|
||||
const [weather, cover, bags, collab, flight] = await Promise.all([
|
||||
spot && inRange ? widgetWeather(spot, { date: weatherDate, lang: language, unit, place }) : Promise.resolve(null),
|
||||
widgetCover(trip.cover_image),
|
||||
packingDetail(trip.id, packing),
|
||||
@@ -120,6 +152,7 @@ async function tripExtras(trip: Trip, days: Day[], { today, language, unit, time
|
||||
[...new Set(names)],
|
||||
useAuthStore.getState().user?.id ?? null
|
||||
),
|
||||
flightOf(trip, reservations, path, context),
|
||||
]);
|
||||
const packed = packingSummary(packing, `/trips/${trip.id}?tab=listen`);
|
||||
const dayDates = new Map(days.map((d) => [d.id, (d.date ?? '').slice(0, 10)]));
|
||||
@@ -137,6 +170,7 @@ async function tripExtras(trip: Trip, days: Day[], { today, language, unit, time
|
||||
todos: todosSummary(todos, `${path}?tab=listen`),
|
||||
packing: packed && bags ? { ...packed, ...bags } : packed,
|
||||
collab,
|
||||
flight,
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -189,7 +223,13 @@ export default function NativeWidgetsSync(): null {
|
||||
const sync = async () => {
|
||||
const now = new Date();
|
||||
const today = localDateStr(now.getFullYear(), now.getMonth(), now.getDate());
|
||||
const context: SyncContext = { today, language, unit, time: (value) => formatTime(value, language, timeFormat) };
|
||||
const context: SyncContext = {
|
||||
today,
|
||||
language,
|
||||
unit,
|
||||
time: (value) => formatTime(value, language, timeFormat),
|
||||
timeFormat: timeFormat || '24h',
|
||||
};
|
||||
const trips = await offlineDb.trips.toArray();
|
||||
const hero = pickTrip(trips, today);
|
||||
// The countries do not depend on a trip: the atlas widget fills without one.
|
||||
@@ -210,8 +250,11 @@ export default function NativeWidgetsSync(): null {
|
||||
if (cancelled) return;
|
||||
// The Live Activity from three hours before the day's first timed stop, not all day long.
|
||||
const minutes = now.getHours() * 60 + now.getMinutes();
|
||||
const live =
|
||||
const flight = liveFlightOf(main.extra?.extras.flight, Math.round(now.getTime() / 1000));
|
||||
const day =
|
||||
main.day && liveDayDue(main.day, minutes) ? tripDayState(main.day, minutes, t('native.widget.dayDone')) : null;
|
||||
// A flight near or in the air holds the activity as a boarding pass.
|
||||
const live = day ? { ...day, ...(flight ? { flight } : {}) } : null;
|
||||
await publishWidgets(main.snapshot, live, choices);
|
||||
await publishToWatch(hero, main, today);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user