tane/apps/app_seeds/lib/ui/settings_screen.dart
vjrj 2812c99280 feat(inventory): CSV/JSON export and JSON import with LWW reconciliation
Interchange export/import for Phase 1 (data-model §7):

- JSON: canonical, versioned envelope (formatVersion 1) with all sync
  metadata verbatim, photos embedded as base64, tombstones excluded.
  Species are re-resolved on import by scientific name (catalog ids are
  per-install). Reader tolerates unknown fields/enum values (§5.2) and
  rejects newer format versions with a clear error.
- CSV: export-only spreadsheet flatten, one row per lot, RFC 4180
  escaping, never any photo bytes.
- Import merges by UUIDv7 id in one transaction: insert-if-unknown
  preserving original stamps, last-writer-wins by packed HLC for known
  mutable rows, append-only movements; afterwards the local clock
  receiveEvent()s the newest imported stamp so it never runs behind.
- Settings gains a Backup section (export CSV/JSON, import JSON with
  confirmation); file dialogs behind a FileService interface backed by
  file_picker (MIT).
- Tests: codec round-trip and tolerance, reconciler LWW, repository
  export→import round-trip (fresh DB, idempotent re-import, newer-local
  wins, clock monotonicity, species re-resolution), backup widget flows.
2026-07-09 12:46:53 +02:00

99 lines
2.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../i18n/strings.g.dart';
import '../services/export_import_service.dart';
import 'backup_section.dart';
import 'theme.dart';
/// Basic settings: app language, backup (export/import) and an "about"
/// section. [exportImport] is injectable so widget tests can pass a fake;
/// the app resolves it lazily from the service locator.
class SettingsScreen extends StatelessWidget {
const SettingsScreen({this.exportImport, super.key});
final ExportImportService? exportImport;
@override
Widget build(BuildContext context) {
final t = context.t;
final current = TranslationProvider.of(context).flutterLocale.languageCode;
return Scaffold(
appBar: AppBar(title: Text(t.menu.settings)),
body: ListView(
children: [
_SectionHeader(t.settings.language),
_LanguageTile(
label: t.settings.langEs,
selected: current == 'es',
onTap: () => LocaleSettings.setLocale(AppLocale.es),
),
_LanguageTile(
label: t.settings.langEn,
selected: current == 'en',
onTap: () => LocaleSettings.setLocale(AppLocale.en),
),
ListTile(
leading: const Icon(Icons.smartphone_outlined),
title: Text(t.settings.systemLanguage),
onTap: () => LocaleSettings.useDeviceLocale(),
),
const Divider(),
_SectionHeader(t.backup.title),
BackupSection(service: exportImport),
const Divider(),
_SectionHeader(t.settings.about),
ListTile(
leading: Image.asset('assets/logo.png', width: 32),
title: Text(t.settings.aboutOpen),
subtitle: Text(t.settings.aboutText),
trailing: const Icon(Icons.chevron_right),
onTap: () => context.push('/about'),
),
],
),
);
}
}
class _SectionHeader extends StatelessWidget {
const _SectionHeader(this.text);
final String text;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 4),
child: Text(
text,
style: Theme.of(context).textTheme.titleSmall?.copyWith(
color: seedGreen,
fontWeight: FontWeight.w600,
letterSpacing: 0.4,
),
),
);
}
}
class _LanguageTile extends StatelessWidget {
const _LanguageTile({
required this.label,
required this.selected,
required this.onTap,
});
final String label;
final bool selected;
final VoidCallback onTap;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(label),
trailing: selected ? const Icon(Icons.check, color: seedGreen) : null,
onTap: onTap,
);
}
}