fix: camera unmount, sticker trash, update reset

This commit is contained in:
2026-06-11 17:25:01 +02:00
parent f26a15f851
commit f776cb3ed6
8 changed files with 76 additions and 36 deletions
+23 -9
View File
@@ -2,10 +2,10 @@ import { File, Paths } from 'expo-file-system';
import * as FileSystem from 'expo-file-system/legacy';
import { FRAMES } from '../data/frames';
export const APP_VERSION = 2; // Increment this whenever you want settings to reset on update
import * as Application from 'expo-application';
export interface AppSettings {
appVersion: number;
appLastUpdate: number | null;
countdownDuration: number; // in seconds
printerIp: string;
adminPassword: string;
@@ -33,7 +33,7 @@ export interface AppSettings {
const settingsFile = new File(Paths.document, 'settings.json');
const DEFAULT_SETTINGS: AppSettings = {
appVersion: APP_VERSION,
appLastUpdate: null,
countdownDuration: 3,
printerIp: '192.168.1.100',
adminPassword: '1234',
@@ -56,21 +56,35 @@ const DEFAULT_SETTINGS: AppSettings = {
* Load settings from persistent storage.
*/
export async function loadSettings(): Promise<AppSettings> {
let currentUpdateTime: number | null = null;
try {
const updateTime = await Application.getLastUpdateTimeAsync();
currentUpdateTime = updateTime ? updateTime.getTime() : null;
} catch (e) {
console.warn('Could not get app update time:', e);
}
try {
const content = await settingsFile.text();
const parsed = JSON.parse(content);
// Reset to defaults if the app version has changed (app updated)
if (parsed.appVersion !== APP_VERSION) {
console.log(`Settings version mismatch (old: ${parsed.appVersion}, new: ${APP_VERSION}). Resetting to default.`);
return DEFAULT_SETTINGS;
// Reset to defaults if the app installation/update time has changed
// Allow a small drift margin (e.g. 5 seconds) just in case
if (parsed.appLastUpdate && currentUpdateTime) {
if (Math.abs(parsed.appLastUpdate - currentUpdateTime) > 5000) {
console.log(`App update detected. Resetting settings to default.`);
const defaults = { ...DEFAULT_SETTINGS, appLastUpdate: currentUpdateTime };
// Instantly save the new default settings to disk so they persist!
await saveSettings(defaults);
return defaults;
}
}
return { ...DEFAULT_SETTINGS, ...parsed };
return { ...DEFAULT_SETTINGS, ...parsed, appLastUpdate: currentUpdateTime };
} catch (e) {
console.error('Failed to load settings, using defaults:', e);
}
return DEFAULT_SETTINGS;
return { ...DEFAULT_SETTINGS, appLastUpdate: currentUpdateTime };
}
/**