feat(native): files open in the system viewer instead of the share sheet

A PDF, image or document opened in the app went straight to the share sheet.
It now opens in Quick Look on iOS, to page through and zoom with its own share
button, and in the app that views its type on Android. An app without the
viewer (feature filePreview), or a type nothing on the device shows, gets the
share sheet as before.
This commit is contained in:
Maurice
2026-11-10 00:21:28 +01:00
parent 28079197e7
commit 2f03554a60
7 changed files with 188 additions and 10 deletions
+34
View File
@@ -1,6 +1,8 @@
import { Directory, Filesystem } from '@capacitor/filesystem';
import { Share } from '@capacitor/share';
import { toBase64 } from './blobBase64';
import { hasNativeFeature } from './capabilities';
import { TrekShell } from './trekShell';
import { addToWallet, isWalletPassName } from './wallet';
/** Readable, but safe as a single path segment inside the cache directory. */
@@ -29,11 +31,43 @@ export function fileNameFromUrl(url: string): string {
export async function shareFile(blob: Blob, filename: string): Promise<void> {
// A Wallet pass goes to Apple Wallet's own sheet, not to the generic share sheet.
if (isWalletPassName(filename) && (await addToWallet(blob))) return;
await shareCached(await writeToCache(blob, filename));
}
/**
* Opens a file to look at it: a PDF to page through, an image, a document, in
* the system's viewer (Quick Look on iOS, the viewing app on Android). An app
* without the viewer, or a type nothing on the device shows, gets the share
* sheet as before, which still previews and saves it.
*/
export async function openNativeFile(blob: Blob, filename: string): Promise<void> {
if (isWalletPassName(filename) && (await addToWallet(blob))) return;
const uri = await writeToCache(blob, filename);
if (hasNativeFeature('filePreview')) {
try {
const { result } = await TrekShell.previewFile({
path: uri,
mimeType: blob.type || 'application/octet-stream',
title: filename,
});
if (result === 'shown') return;
} catch {
/* the viewer could not open it: the share sheet still can */
}
}
await shareCached(uri);
}
async function writeToCache(blob: Blob, filename: string): Promise<string> {
const { uri } = await Filesystem.writeFile({
path: cacheFileName(filename),
data: await toBase64(blob),
directory: Directory.Cache,
});
return uri;
}
async function shareCached(uri: string): Promise<void> {
try {
await Share.share({ files: [uri] });
} catch (err) {