import * as FileSystem from 'expo-file-system/legacy' import { IMAGE_CACHE_CLEARED_AT_KEY } from '@/src/lib/image-cache-ttl' /** * Plain-file persistence for the image cache TTL timestamp. * * Deliberately not StorageServiceCasual: that writes to expo-secure-store, and * a cache-maintenance timestamp is not a secret. expo-file-system ships as a * dependency of `expo`, so no extra package is needed. * * The document directory survives app restarts and updates (but not a * reinstall, which also clears the image cache — exactly what we want). */ function timestampFileUri(): string | null { if (!FileSystem.documentDirectory) return null return `${FileSystem.documentDirectory}${IMAGE_CACHE_CLEARED_AT_KEY}` } /** Epoch ms of the last successful disk-cache clear, or null when unknown. */ export async function readImageCacheClearedAt(): Promise { const uri = timestampFileUri() if (!uri) return null try { const info = await FileSystem.getInfoAsync(uri) if (!info.exists) return null const raw = await FileSystem.readAsStringAsync(uri) const parsed = Number(raw) return Number.isFinite(parsed) ? parsed : null } catch { return null } } /** Persist the last successful clear. Throws so the caller can retry later. */ export async function writeImageCacheClearedAt(timestamp: number): Promise { const uri = timestampFileUri() if (!uri) return await FileSystem.writeAsStringAsync(uri, String(timestamp)) }