Colota - Android GPS Location tracker
refactor(formats): single source of truth for file format metadata (#428)
Max Dietrich06ebc5c1188c
diff --git a/apps/mobile/src/screens/ImportLocationsScreen.tsx b/apps/mobile/src/screens/ImportLocationsScreen.tsxindex f4568dfb..d53eea98 100644--- a/apps/mobile/src/screens/ImportLocationsScreen.tsx+++ b/apps/mobile/src/screens/ImportLocationsScreen.tsx@@ -5,8 +5,7 @@import React, { useCallback, useEffect, useState } from "react"import { ScrollView, StyleSheet, Text, View } from "react-native"-import { Activity, Database, Download, Globe, Map, MapPin, Table2 } from "lucide-react-native"-import type { LucideIcon } from "lucide-react-native"+import { Download } from "lucide-react-native"import { fonts } from "../styles/typography"import { Button, Card, Container, Divider, LoadingOverlay, SectionTitle } from "../components"import { useTheme } from "../hooks/useTheme"@@ -14,68 +13,21 @@ import NativeLocationService from "../services/NativeLocationService"import ImportService, { type ImportFormat, type ImportPreview } from "../services/ImportService"import { showAlert, showChoice } from "../services/modalService"import { logger } from "../utils/logger"+import { FILE_FORMATS, IMPORT_FORMAT_ORDER, importDescription } from "../utils/fileFormats"import { ScreenProps } from "../types/global"import type { ThemeColors } from "../types/global"-const IMPORT_FORMAT_LABELS: Record<ImportFormat, string> = {- geojson: "GeoJSON",- google_timeline_legacy: "Google Timeline (Records.json)",- google_timeline_new: "Google Timeline",- gpx: "GPX",- kml: "KML",- csv: "CSV"-}+const IMPORT_FORMAT_LABELS = Object.fromEntries(+ (Object.keys(FILE_FORMATS) as ImportFormat[]).map((k) => [k, FILE_FORMATS[k].label])+) as Record<ImportFormat, string>// Warn before queueing this many points - the backend has to absorb one upload per row.const SYNC_WARN_THRESHOLD = 10_000-type FormatEntry = {- icon: LucideIcon- title: string- extension: string- description: string-}--const SUPPORTED_FORMATS: FormatEntry[] = [- {- icon: Globe,- title: "GeoJSON",- extension: ".geojson",- description: "FeatureCollection of Point features with a time property. Colota's own export round-trips cleanly."- },- {- icon: Database,- title: "Google Timeline (legacy)",- extension: "Records.json",- description:- "Older bulk Location History export from Google Takeout. Google removed this from Takeout in late 2024; use this for archived files."- },- {- icon: MapPin,- title: "Google Timeline",- extension: ".json",- description: "On-device export from Android Settings -> Location -> Location services -> Timeline."- },- {- icon: Activity,- title: "GPX",- extension: ".gpx",- description: "GPS Exchange Format. Sport watches, Strava exports, tracking apps."- },- {- icon: Map,- title: "KML",- extension: ".kml",- description:- "Keyhole Markup Language. Google Earth, My Maps. Placemarks with TimeStamps only - LineString-only tracks are skipped."- },- {- icon: Table2,- title: "CSV",- extension: ".csv",- description: "Comma-separated table. Header must include latitude, longitude, and a time column."- }-]+const SUPPORTED_FORMATS = IMPORT_FORMAT_ORDER.map((key) => {+ const f = FILE_FORMATS[key]+ return { icon: f.icon, title: f.label, extension: f.extension, description: importDescription(f) }+})function formatDate(unixSeconds: number | null): string {if (unixSeconds == null) return "-"@@ -127,7 +79,7 @@ function importErrorMessage(e: unknown): string {}}-const FormatRow = ({ entry, colors }: { entry: FormatEntry; colors: ThemeColors }) => {+const FormatRow = ({ entry, colors }: { entry: (typeof SUPPORTED_FORMATS)[number]; colors: ThemeColors }) => {const Icon = entry.iconreturn (<View style={styles.formatRow}>diff --git a/apps/mobile/src/utils/__tests__/exportConverters.test.ts b/apps/mobile/src/utils/__tests__/exportConverters.test.tsindex 974a9fd5..79db70cc 100644--- a/apps/mobile/src/utils/__tests__/exportConverters.test.ts+++ b/apps/mobile/src/utils/__tests__/exportConverters.test.ts@@ -5,8 +5,10 @@ import {convertTripsToGeoJSON,convertTripsToGPX,convertTripsToKML,- LARGE_FILE_THRESHOLD+ LARGE_FILE_THRESHOLD,+ TRIP_CONVERTERS} from "../exportConverters"+import { FILE_FORMATS } from "../fileFormats"const sampleTrips = [{@@ -167,3 +169,56 @@ describe("convertTripsToKML", () => {expect(kml).toContain("<TimeStamp>")})})++describe("FILE_FORMATS registry", () => {+ it("exportable formats are fully wired for export", () => {+ const exportable = Object.entries(FILE_FORMATS).filter(([, f]) => f.exportable)+ expect(exportable.map(([key]) => key).sort()).toEqual(Object.keys(TRIP_CONVERTERS).sort())+ const incomplete = exportable.filter(([, f]) => !f.mimeType || !f.subtitle).map(([key]) => key)+ expect(incomplete).toEqual([])+ })+})++describe("exporters emit every location field", () => {+ const trip = [+ {+ index: 1,+ locations: [+ {+ latitude: 48.1,+ longitude: 11.5,+ accuracy: 7,+ altitude: 333,+ speed: 4,+ bearing: 90,+ battery: 55,+ timestamp: 1700000000+ }+ ],+ startTime: 1700000000,+ endTime: 1700000000,+ distance: 0,+ locationCount: 1+ }+ ]++ it("GeoJSON properties carry every field", () => {+ const props = JSON.parse(convertTripsToGeoJSON(trip)).features[0].properties+ expect(props).toMatchObject({ accuracy: 7, altitude: 333, speed: 4, bearing: 90, battery: 55 })+ })++ it("GPX extensions carry every field", () => {+ const gpx = convertTripsToGPX(trip)+ expect(gpx).toContain("<ele>333</ele>")+ expect(gpx).toContain("<accuracy>7</accuracy>")+ expect(gpx).toContain("<speed>4</speed>")+ expect(gpx).toContain("<bearing>90</bearing>")+ expect(gpx).toContain("<battery>55</battery>")+ })++ it("CSV columns carry every field", () => {+ const lines = convertTripsToCSV(trip).split("\n")+ expect(lines[0]).toBe("trip,id,timestamp,iso_time,latitude,longitude,accuracy,altitude,speed,bearing,battery")+ expect(lines[1]).toContain("7,333,4,90,55")+ })+})diff --git a/apps/mobile/src/utils/exportConverters.ts b/apps/mobile/src/utils/exportConverters.tsindex 286e8f9c..a961a8c3 100644--- a/apps/mobile/src/utils/exportConverters.ts+++ b/apps/mobile/src/utils/exportConverters.ts@@ -4,9 +4,9 @@*/import { type LucideIcon } from "lucide-react-native"-import { Table2, Globe, Activity, Map } from "lucide-react-native"import { Trip } from "../types/global"import { getTripColor } from "./trips"+import { FILE_FORMATS, IMPORT_FORMAT_ORDER } from "./fileFormats"export const LARGE_FILE_THRESHOLD = 10 * 1024 * 1024 // 10 MB@@ -40,7 +40,7 @@ export const getByteSize = (content: string): number => {// ============================================================================export const convertTripsToCSV = (trips: Trip[]): string => {- const headers = "trip,id,timestamp,iso_time,latitude,longitude,accuracy,altitude,speed,battery\n"+ const headers = "trip,id,timestamp,iso_time,latitude,longitude,accuracy,altitude,speed,bearing,battery\n"const rows = trips.flatMap((trip) =>trip.locations.map((item, i) => {@@ -56,6 +56,7 @@ export const convertTripsToCSV = (trips: Trip[]): string => {item.accuracy ?? 0,item.altitude ?? 0,item.speed ?? 0,+ item.bearing ?? 0,item.battery ?? 0].join(",")})@@ -80,6 +81,7 @@ export const convertTripsToGeoJSON = (trips: Trip[]): string => {accuracy: item.accuracy,altitude: item.altitude,speed: item.speed,+ bearing: item.bearing,battery: item.battery,time: new Date(ts * 1000).toISOString()}@@ -113,6 +115,7 @@ export const convertTripsToGPX = (trips: Trip[]): string => {<extensions><accuracy>${item.accuracy || 0}</accuracy><speed>${item.speed || 0}</speed>+ <bearing>${item.bearing || 0}</bearing><battery>${item.battery || 0}</battery></extensions></trkpt>`)@@ -192,7 +195,9 @@ export const TRIP_CONVERTERS: Record<ExportFormat, (trips: Trip[]) => string> =export type ExportFormat = "csv" | "geojson" | "gpx" | "kml"-export const EXPORT_FORMAT_KEYS: ExportFormat[] = ["csv", "geojson", "gpx", "kml"]+export const EXPORT_FORMAT_KEYS: ExportFormat[] = IMPORT_FORMAT_ORDER.filter(+ (k) => FILE_FORMATS[k].exportable+) as ExportFormat[]export interface ExportFormatConfig {label: string@@ -203,37 +208,18 @@ export interface ExportFormatConfig {mimeType: string}-export const EXPORT_FORMATS: Record<ExportFormat, ExportFormatConfig> = {- csv: {- label: "CSV",- subtitle: "Spreadsheet Format",- description: "Excel, Google Sheets, data analysis",- icon: Table2,- extension: ".csv",- mimeType: "text/csv"- },- geojson: {- label: "GeoJSON",- subtitle: "Geographic Data",- description: "Mapbox, Leaflet, QGIS, ArcGIS",- icon: Globe,- extension: ".geojson",- mimeType: "application/json"- },- gpx: {- label: "GPX",- subtitle: "GPS Exchange",- description: "Garmin, Strava, Google Earth",- icon: Activity,- extension: ".gpx",- mimeType: "application/gpx+xml"+export const EXPORT_FORMATS: Record<ExportFormat, ExportFormatConfig> = EXPORT_FORMAT_KEYS.reduce(+ (acc, key) => {+ const f = FILE_FORMATS[key]+ acc[key] = {+ label: f.label,+ subtitle: f.subtitle!,+ description: f.description,+ icon: f.icon,+ extension: f.extension,+ mimeType: f.mimeType!+ }+ return acc},- kml: {- label: "KML",- subtitle: "Keyhole Markup Language",- description: "Google Earth, Google Maps, ArcGIS",- icon: Map,- extension: ".kml",- mimeType: "application/vnd.google-earth.kml+xml"- }-}+ {} as Record<ExportFormat, ExportFormatConfig>+)diff --git a/apps/mobile/src/utils/fileFormats.ts b/apps/mobile/src/utils/fileFormats.tsnew file mode 100644index 00000000..52b4ff8c--- /dev/null+++ b/apps/mobile/src/utils/fileFormats.ts@@ -0,0 +1,92 @@+/**+ * Copyright (C) 2026 Max Dietrich+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.+ */++import { Activity, Database, Globe, Map, MapPin, Table2 } from "lucide-react-native"+import type { LucideIcon } from "lucide-react-native"+import type { ImportFormat } from "../services/ImportService"++// Single source of truth for per-format metadata, shared by the export and import+// screens. `description` is the general blurb shown in both menus;+// `importHint` is an optional import-only caveat appended to+// it on the import screen (where parsing constraints actually matter).+export interface FileFormat {+ label: string+ icon: LucideIcon+ extension: string+ exportable: boolean+ mimeType?: string // present for every exportable format+ subtitle?: string // export picker only+ description: string+ importHint?: string+}++export const FILE_FORMATS: Record<ImportFormat, FileFormat> = {+ geojson: {+ label: "GeoJSON",+ icon: Globe,+ extension: ".geojson",+ exportable: true,+ mimeType: "application/json",+ subtitle: "Geographic Data",+ description: "Mapbox, Leaflet, QGIS. Best for backups - re-imports into Colota without losing data."+ },+ google_timeline_legacy: {+ label: "Google Timeline (legacy)",+ icon: Database,+ extension: "Records.json",+ exportable: false,+ description:+ "Older bulk Location History export from Google Takeout. Google removed this from Takeout in late 2024; use this for archived files."+ },+ google_timeline_new: {+ label: "Google Timeline",+ icon: MapPin,+ extension: ".json",+ exportable: false,+ description: "On-device export from Android Settings -> Location -> Location services -> Timeline."+ },+ gpx: {+ label: "GPX",+ icon: Activity,+ extension: ".gpx",+ exportable: true,+ mimeType: "application/gpx+xml",+ subtitle: "GPS Exchange",+ description: "GPS Exchange Format - Garmin, Strava, sport watches, tracking apps."+ },+ kml: {+ label: "KML",+ icon: Map,+ extension: ".kml",+ exportable: true,+ mimeType: "application/vnd.google-earth.kml+xml",+ subtitle: "Keyhole Markup Language",+ description: "Google Earth, Google Maps, ArcGIS.",+ importHint: "Only timestamped placemarks are read - LineString-only tracks are skipped."+ },+ csv: {+ label: "CSV",+ icon: Table2,+ extension: ".csv",+ exportable: true,+ mimeType: "text/csv",+ subtitle: "Spreadsheet Format",+ description: "Comma-separated table - Excel, Google Sheets, data analysis.",+ importHint: "The header must include latitude, longitude and a time column."+ }+}++export function importDescription(f: FileFormat): string {+ return f.importHint ? `${f.description} ${f.importHint}` : f.description+}++export const IMPORT_FORMAT_ORDER: ImportFormat[] = [+ "geojson",+ "gpx",+ "kml",+ "google_timeline_new",+ "google_timeline_legacy",+ "csv"+]