mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +00:00
feat(native): share a booking document to TREK and import it into a trip
TREK now appears under "Open in" and in the share sheet for PDFs, calendar entries, Wallet passes and mails (iOS document types, Android VIEW and SEND filters). The native side reads the file (up to 15 MB) and hands it to the UI, which asks which trip it belongs to, upcoming trips first, then opens that trip's booking import with the file already chosen.
This commit is contained in:
@@ -23,6 +23,7 @@ vi.mock('./capabilities', () => ({
|
||||
loadNativeCapabilities: vi.fn(async () => ({})),
|
||||
}));
|
||||
vi.mock('./geolocation', () => ({ installNativeGeolocation: vi.fn() }));
|
||||
vi.mock('./sharedFiles', () => ({ startSharedFiles: vi.fn(async () => {}) }));
|
||||
vi.mock('../mobile/overlayStack', () => ({ closeTopOverlay: vi.fn(() => false) }));
|
||||
|
||||
function backButtonHandler(): (event: { canGoBack: boolean }) => void {
|
||||
|
||||
@@ -9,6 +9,7 @@ import { installNativeGeolocation } from './geolocation';
|
||||
import { keepOfflineShellFresh } from './offlineShell';
|
||||
import { isNativeApp, isNativeIos } from './platform';
|
||||
import { reportNativeFailure } from './reportFailure';
|
||||
import { startSharedFiles } from './sharedFiles';
|
||||
import { isUnimplemented } from './trekShell';
|
||||
|
||||
/** Long enough that the warm-up never competes with the first screen's requests. */
|
||||
@@ -27,6 +28,8 @@ export function startNativeApp(): void {
|
||||
void checkAppUpdate(fetchServerFeatures);
|
||||
// The app's own location under navigator.geolocation, where the binary has it.
|
||||
void loadNativeCapabilities().then(() => installNativeGeolocation());
|
||||
// Booking documents shared to TREK, waiting for a trip to import into.
|
||||
void startSharedFiles();
|
||||
|
||||
// A tap on the status bar scrolls the list under it back to the top, as in
|
||||
// every iOS app (the status-bar plugin fires statusTap on iOS).
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { hasNativeFeature } from './capabilities';
|
||||
import { startSharedFiles, toFile, useSharedImportStore } from './sharedFiles';
|
||||
import { TrekShell } from './trekShell';
|
||||
|
||||
vi.mock('./capabilities', () => ({ hasNativeFeature: vi.fn(), loadNativeCapabilities: vi.fn(async () => ({})) }));
|
||||
vi.mock('./trekShell', () => ({
|
||||
TrekShell: { addListener: vi.fn(async () => ({ remove: async () => {} })), takeSharedFile: vi.fn() },
|
||||
isUnimplemented: () => false,
|
||||
}));
|
||||
|
||||
const PDF = { name: 'booking.pdf', mimeType: 'application/pdf', data: btoa('%PDF-1.4 hello') };
|
||||
|
||||
beforeEach(() => {
|
||||
useSharedImportStore.setState({ file: null, tripId: null });
|
||||
vi.mocked(hasNativeFeature).mockReturnValue(true);
|
||||
vi.mocked(TrekShell.takeSharedFile).mockReset().mockResolvedValue({ file: null });
|
||||
});
|
||||
|
||||
describe('shared booking documents', () => {
|
||||
it('turns the base64 the native side sends into a File', async () => {
|
||||
const file = toFile(PDF);
|
||||
expect(file.name).toBe('booking.pdf');
|
||||
expect(file.type).toBe('application/pdf');
|
||||
expect(await file.text()).toBe('%PDF-1.4 hello');
|
||||
});
|
||||
|
||||
it('picks up a document shared before the UI ran, and the next ones as they come', async () => {
|
||||
vi.mocked(TrekShell.takeSharedFile).mockResolvedValue({ file: PDF });
|
||||
await startSharedFiles();
|
||||
expect(useSharedImportStore.getState().file?.name).toBe('booking.pdf');
|
||||
|
||||
const listener = vi.mocked(TrekShell.addListener).mock.calls[0][1] as (p: typeof PDF) => void;
|
||||
listener({ ...PDF, name: 'train.ics', mimeType: 'text/calendar' });
|
||||
expect(useSharedImportStore.getState().file?.name).toBe('train.ics');
|
||||
});
|
||||
|
||||
it('hands the file over once a trip is picked, and empties the queue', () => {
|
||||
useSharedImportStore.getState().setFile(toFile(PDF));
|
||||
useSharedImportStore.getState().chooseTrip(7);
|
||||
expect(useSharedImportStore.getState().tripId).toBe(7);
|
||||
expect(useSharedImportStore.getState().take()?.name).toBe('booking.pdf');
|
||||
expect(useSharedImportStore.getState()).toMatchObject({ file: null, tripId: null });
|
||||
});
|
||||
|
||||
it('does nothing in a binary without the feature', async () => {
|
||||
vi.mocked(hasNativeFeature).mockReturnValue(false);
|
||||
vi.mocked(TrekShell.addListener).mockClear();
|
||||
await startSharedFiles();
|
||||
expect(TrekShell.addListener).not.toHaveBeenCalled();
|
||||
expect(TrekShell.takeSharedFile).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { create } from 'zustand';
|
||||
import { hasNativeFeature, loadNativeCapabilities } from './capabilities';
|
||||
import { reportNativeFailure } from './reportFailure';
|
||||
import { TrekShell, isUnimplemented, type SharedFilePayload } from './trekShell';
|
||||
|
||||
/**
|
||||
* Booking documents shared to TREK ("Open in TREK" on a PDF, a calendar entry,
|
||||
* a Wallet pass or a mail). The native side hands them over as base64
|
||||
* (TrekShell.takeSharedFile and the sharedFile event); here they become a File
|
||||
* waiting for a trip: NativeImportSheet asks which one, and the trip's booking
|
||||
* import opens with the file already chosen (BookingImportModal).
|
||||
*/
|
||||
interface SharedImportState {
|
||||
file: File | null;
|
||||
/** The trip the user picked for it; the planner of that trip takes it from here. */
|
||||
tripId: number | null;
|
||||
setFile: (file: File | null) => void;
|
||||
chooseTrip: (tripId: number) => void;
|
||||
/** The booking import took the file: the queue is empty again. */
|
||||
take: () => File | null;
|
||||
}
|
||||
|
||||
export const useSharedImportStore = create<SharedImportState>((set, get) => ({
|
||||
file: null,
|
||||
tripId: null,
|
||||
setFile: (file) => set({ file, tripId: null }),
|
||||
chooseTrip: (tripId) => set({ tripId }),
|
||||
take: () => {
|
||||
const { file } = get();
|
||||
set({ file: null, tripId: null });
|
||||
return file;
|
||||
},
|
||||
}));
|
||||
|
||||
export function toFile(payload: SharedFilePayload): File {
|
||||
const binary = atob(payload.data);
|
||||
const bytes = new Uint8Array(binary.length);
|
||||
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
||||
return new File([bytes], payload.name || 'booking', { type: payload.mimeType || 'application/octet-stream' });
|
||||
}
|
||||
|
||||
/** Picks up a document shared before the UI ran, and listens for the next ones. */
|
||||
export async function startSharedFiles(): Promise<void> {
|
||||
await loadNativeCapabilities();
|
||||
if (!hasNativeFeature('sharedFiles')) return;
|
||||
const accept = (payload: SharedFilePayload | null) => {
|
||||
if (payload?.data) useSharedImportStore.getState().setFile(toFile(payload));
|
||||
};
|
||||
void TrekShell.addListener('sharedFile', accept);
|
||||
try {
|
||||
accept((await TrekShell.takeSharedFile()).file);
|
||||
} catch (err) {
|
||||
if (!isUnimplemented(err)) reportNativeFailure(err);
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,20 @@ export interface TrekShellPlugin {
|
||||
event: 'location',
|
||||
listener: (payload: NativeLocationPayload) => void
|
||||
): Promise<{ remove: () => Promise<void> }>;
|
||||
addListener(
|
||||
event: 'sharedFile',
|
||||
listener: (payload: SharedFilePayload) => void
|
||||
): Promise<{ remove: () => Promise<void> }>;
|
||||
/** Feature sharedFiles: the booking document shared to TREK before the UI ran. */
|
||||
takeSharedFile(): Promise<{ file: SharedFilePayload | null }>;
|
||||
}
|
||||
|
||||
/** A document shared to TREK, as the native side reads it. */
|
||||
export interface SharedFilePayload {
|
||||
name: string;
|
||||
mimeType: string;
|
||||
/** base64 */
|
||||
data: string;
|
||||
}
|
||||
|
||||
/** A position, or an error shaped like GeolocationPositionError (1 denied, 2 unavailable). */
|
||||
|
||||
Reference in New Issue
Block a user