import { File, Directory, Paths } from 'expo-file-system';
import { TrackPoint } from '../types';
function recordingsDir(): Directory {
return new Directory(Paths.document, 'recordings');
}
export function ensureRecordingsDir(): void {
const dir = recordingsDir();
if (!dir.exists) dir.create();
}
export function saveGpx(trackPoints: TrackPoint[], title: string): string {
ensureRecordingsDir();
const filename = `${sanitizeFilename(title)}_${Date.now()}.gpx`;
const file = new File(recordingsDir(), filename);
file.write(buildGpx(trackPoints, title));
return file.uri;
}
export function buildGpx(trackPoints: TrackPoint[], title: string): string {
const trkpts = trackPoints.map(buildTrkpt).join('\n');
return `
${escapeXml(title)}
${escapeXml(title)}
${trkpts}
`;
}
function buildTrkpt(pt: TrackPoint): string {
const ext = buildExtensions(pt);
return `
${pt.ele.toFixed(1)}
${ext}
`;
}
function buildExtensions(pt: TrackPoint): string {
if (pt.hr == null && pt.power == null && pt.cad == null) return '';
const hr = pt.hr != null ? `${pt.hr}` : '';
const power = pt.power != null ? `${pt.power}` : '';
const cad = pt.cad != null ? `${pt.cad}` : '';
return `
${hr}${power}${cad}
`;
}
function escapeXml(s: string): string {
return s.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"');
}
function sanitizeFilename(s: string): string {
return s.replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64);
}