feat: Phase 0 mobile app scaffold — Expo 55, SQLite, Feed/Import/Settings screens

This commit is contained in:
Davide Scaini
2026-04-24 10:39:06 +02:00
parent 565f5a3ff1
commit b37df88fe1
17 changed files with 1076 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
import type { SQLiteDatabase } from 'expo-sqlite';
export async function migrateDb(db: SQLiteDatabase): Promise<void> {
await db.execAsync('PRAGMA journal_mode = WAL;');
await db.execAsync(`
CREATE TABLE IF NOT EXISTS activities (
id TEXT PRIMARY KEY,
source_hash TEXT NOT NULL,
detail_json TEXT NOT NULL,
timeseries_json TEXT,
geojson TEXT,
original_path TEXT,
synced_at INTEGER,
origin TEXT NOT NULL CHECK(origin IN ('local', 'remote')),
created_at INTEGER NOT NULL DEFAULT (unixepoch())
);
CREATE INDEX IF NOT EXISTS idx_activities_created_at
ON activities(created_at DESC);
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);
`);
}
+120
View File
@@ -0,0 +1,120 @@
import { useSQLiteContext } from 'expo-sqlite';
// ── Types ──────────────────────────────────────────────────────────────────
export type ActivityRow = {
id: string;
source_hash: string;
detail_json: string;
timeseries_json: string | null;
geojson: string | null;
original_path: string | null;
synced_at: number | null;
origin: 'local' | 'remote';
created_at: number;
};
export type ActivitySummary = {
id: string;
title: string;
sport: string;
started_at: string;
distance_m: number | null;
duration_s: number | null;
elevation_gain_m: number | null;
origin: 'local' | 'remote';
synced_at: number | null;
};
// ── Activities ─────────────────────────────────────────────────────────────
export function useActivities(): ActivitySummary[] {
const db = useSQLiteContext();
// Summaries are derived from the stored detail_json at query time.
// JSON extraction via SQLite's json_extract keeps the table schema simple.
const rows = db.getAllSync<{
id: string;
origin: 'local' | 'remote';
synced_at: number | null;
title: string;
sport: string;
started_at: string;
distance_m: number | null;
duration_s: number | null;
elevation_gain_m: number | null;
}>(`
SELECT
id, origin, synced_at,
json_extract(detail_json, '$.title') AS title,
json_extract(detail_json, '$.sport') AS sport,
json_extract(detail_json, '$.started_at') AS started_at,
json_extract(detail_json, '$.distance_m') AS distance_m,
json_extract(detail_json, '$.duration_s') AS duration_s,
json_extract(detail_json, '$.elevation_gain_m') AS elevation_gain_m
FROM activities
ORDER BY created_at DESC
`);
return rows;
}
export function useActivity(id: string): ActivityRow | null {
const db = useSQLiteContext();
return db.getFirstSync<ActivityRow>(
'SELECT * FROM activities WHERE id = ?',
[id],
) ?? null;
}
export async function insertActivity(
db: ReturnType<typeof useSQLiteContext>,
row: Pick<ActivityRow, 'id' | 'source_hash' | 'detail_json' | 'timeseries_json' | 'geojson' | 'original_path' | 'origin'>,
): Promise<void> {
await db.runAsync(
`INSERT OR IGNORE INTO activities
(id, source_hash, detail_json, timeseries_json, geojson, original_path, origin)
VALUES (?, ?, ?, ?, ?, ?, ?)`,
[
row.id,
row.source_hash,
row.detail_json,
row.timeseries_json ?? null,
row.geojson ?? null,
row.original_path ?? null,
row.origin,
],
);
}
// ── Settings ───────────────────────────────────────────────────────────────
export async function getSetting(
db: ReturnType<typeof useSQLiteContext>,
key: string,
): Promise<string | null> {
const row = db.getFirstSync<{ value: string }>(
'SELECT value FROM settings WHERE key = ?',
[key],
);
return row?.value ?? null;
}
export async function setSetting(
db: ReturnType<typeof useSQLiteContext>,
key: string,
value: string,
): Promise<void> {
await db.runAsync(
`INSERT INTO settings (key, value) VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value = excluded.value`,
[key, value],
);
}
export function useSetting(key: string): string | null {
const db = useSQLiteContext();
const row = db.getFirstSync<{ value: string }>(
'SELECT value FROM settings WHERE key = ?',
[key],
);
return row?.value ?? null;
}