Files
DriverVault/Phone App/lib/app_settings.dart
T
tajniak81andClaude Opus 4.8 ee28b522c7 Bring the Phone App up to parity with the web app
Four rounds of web-app features never reached the phone: fuel, maintenance,
document and reminder tracking; attachments; the currency setting and the
locale split; and technical check history. The README claimed full parity
throughout, so the gap was invisible. Catch the phone up, mirroring the web
components field for field.

Car detail grows the web app's tabs, in its order: technical checks,
maintenance, fuel (with the summary panel), documents and reminders, beside
the existing service and parts lists. The derived figures are the server's
and are rendered as "—" wherever it sent null — a window with a missed fill
has no consumption, and a plausible-looking 0.0 there would be a lie.

Attachments hang off service records, technical checks, workshop visits,
refills, documents and parts on identical terms, so one field and one apply
helper cover all six rather than being copied per form. As on the web, the
form only collects intent: the file endpoints address a record that must
already exist, so a create-with-file is two calls, and a failure on the
second reports as an attachment error because the metadata is committed.

Two bugs fixed on the way:

- _carPayload omitted technicalCheckIntervalDays. The API rewrites every
  column from the body, so any car edit — including the one-tap odometer
  update — silently zeroed the car's inspection interval.
- main() never called initializeDateFormatting, so month names ignored the
  chosen language that the new Language picker exists to set.

Luxembourgish and Romansh are deliberately left off the language list: intl
ships no symbols for them and throws rather than falling back, which would
take out every date on screen. The browser has full ICU data and has no such
limit, so the web app can offer them. The server only validates a locale's
shape, so an unrenderable tag can still arrive from the web; format.dart
resolves through a supported-language check and falls back to en-US.

Labels for the language/region/currency lists are hand-kept because Dart has
no Intl.DisplayNames. The lists mirror validCurrencies in me.go.

file_picker is pinned to ^10: v8 compiles against android-34, which no longer
builds against the other plugins' compileSdk requirement of 36.

Adds the project's first test, covering the parts that fail silently rather
than loudly — null derived fields, the badge wording, and the locale guard.

The phone was not authorized over ADB, so the UI was not exercised on a
device: this is analyzer-, test- and build-clean, and every JSON field name
and route was cross-checked against models.go and server.go.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 15:15:01 +02:00

88 lines
3.0 KiB
Dart

import "package:flutter/material.dart";
import "package:shared_preferences/shared_preferences.dart";
import "models.dart";
/// App-wide appearance preferences (theme / locale / date format / currency /
/// font size), mirroring the web app's prefs.js. Persisted to SharedPreferences
/// so the chosen theme survives a restart before /api/me loads, and exposed as a
/// ChangeNotifier so MaterialApp and date formatting react to changes.
class AppSettings extends ChangeNotifier {
static const _kTheme = "cc_theme";
static const _kLocale = "cc_locale";
static const _kDateFormat = "cc_dateFormat";
static const _kCurrency = "cc_currency";
static const _kFontSize = "cc_fontSize";
String theme = "system"; // light | dark | system
String locale = "en-US"; // BCP-47 language-REGION
String dateFormat = "YMD"; // YMD | DMY_NUM | DMY | MDY
String currency = "USD"; // ISO 4217 code
String fontSize = "medium"; // small | medium | large
/// Language and region are two controls over the one stored tag, so the pair
/// can be mixed freely (English in Poland, say).
String get language => locale.split("-").first;
String get region => locale.split("-").length > 1 ? locale.split("-")[1] : "US";
Future<void> loadFromStorage() async {
final prefs = await SharedPreferences.getInstance();
theme = prefs.getString(_kTheme) ?? theme;
locale = prefs.getString(_kLocale) ?? locale;
dateFormat = prefs.getString(_kDateFormat) ?? dateFormat;
currency = prefs.getString(_kCurrency) ?? currency;
fontSize = prefs.getString(_kFontSize) ?? fontSize;
notifyListeners();
}
Future<void> _persist() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_kTheme, theme);
await prefs.setString(_kLocale, locale);
await prefs.setString(_kDateFormat, dateFormat);
await prefs.setString(_kCurrency, currency);
await prefs.setString(_kFontSize, fontSize);
}
/// Sync from a freshly fetched profile (login, boot, settings saves).
void applyFromProfile(UserProfile p) {
theme = p.theme;
locale = p.locale;
dateFormat = p.dateFormat;
currency = p.currency;
fontSize = p.fontSize;
_persist();
notifyListeners();
}
/// Optimistically apply a single changed field (used by Settings so the UI
/// reacts instantly while the PATCH is in flight).
void patch({
String? theme,
String? locale,
String? dateFormat,
String? currency,
String? fontSize,
}) {
if (theme != null) this.theme = theme;
if (locale != null) this.locale = locale;
if (dateFormat != null) this.dateFormat = dateFormat;
if (currency != null) this.currency = currency;
if (fontSize != null) this.fontSize = fontSize;
_persist();
notifyListeners();
}
ThemeMode get themeMode => switch (theme) {
"light" => ThemeMode.light,
"dark" => ThemeMode.dark,
_ => ThemeMode.system,
};
double get textScale => switch (fontSize) {
"small" => 0.9375,
"large" => 1.125,
_ => 1.0,
};
}