Files
DriverVault/Phone App/lib/main.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

115 lines
3.9 KiB
Dart

import "package:flutter/material.dart";
import "package:intl/date_symbol_data_local.dart";
import "api.dart";
import "app_settings.dart";
import "auth.dart";
import "biometric.dart";
import "theme.dart";
import "screens/lock_screen.dart";
import "screens/login_screen.dart";
import "screens/root_shell.dart";
// Simple app-wide singletons (small app — no DI framework needed).
final apiClient = ApiClient();
final authService = AuthService(apiClient);
final appSettings = AppSettings();
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// Month and day names are rendered in the user's chosen language, so every
// locale's date symbols have to be loaded before the first formatDate call —
// DateFormat throws on an uninitialized locale rather than falling back.
await initializeDateFormatting();
await apiClient.loadServerUrl();
await appSettings.loadFromStorage();
await authService.loadFromStorage();
// Prime the biometric-enabled cache; if a session survived from a previous
// run and biometric login is on, start locked so the app requires an unlock
// instead of jumping straight to the dashboard.
final bioEnabled = await biometricAuth.isEnabled();
if (authService.isAuthenticated && bioEnabled) {
authService.lock();
}
// If a token survived from a previous run, refresh the profile so the saved
// appearance prefs (theme/font/date format) apply on boot.
if (authService.isAuthenticated) {
apiClient.getMe().then((p) => appSettings.applyFromProfile(p)).catchError((_) {});
}
runApp(const CarControlApp());
}
class CarControlApp extends StatefulWidget {
const CarControlApp({super.key});
@override
State<CarControlApp> createState() => _CarControlAppState();
}
class _CarControlAppState extends State<CarControlApp> with WidgetsBindingObserver {
// Grace period: a quick app-switch (returning within this window) does NOT
// re-lock; staying in the background longer does.
static const _lockGrace = Duration(seconds: 30);
DateTime? _backgroundedAt;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
// Re-lock when the app returns from the background, but only if it was away
// longer than the grace period — so quick app-switches don't re-lock.
// Handle only 'paused'/'resumed' (not 'inactive', which also fires
// transiently while the OS biometric prompt is showing).
if (state == AppLifecycleState.paused) {
_backgroundedAt = DateTime.now();
} else if (state == AppLifecycleState.resumed) {
final since = _backgroundedAt;
_backgroundedAt = null;
if (since != null &&
DateTime.now().difference(since) > _lockGrace &&
authService.isAuthenticated &&
biometricAuth.enabledCached) {
authService.lock();
}
}
}
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: appSettings,
builder: (context, _) => MaterialApp(
title: "DriverVault",
debugShowCheckedModeBanner: false,
theme: DriverVault.theme(Brightness.light),
darkTheme: DriverVault.theme(Brightness.dark),
themeMode: appSettings.themeMode,
// Apply the user's font-size preference app-wide.
builder: (context, child) => MediaQuery(
data: MediaQuery.of(context).copyWith(
textScaler: TextScaler.linear(appSettings.textScale),
),
child: child!,
),
home: ListenableBuilder(
listenable: authService,
builder: (context, _) {
if (!authService.isAuthenticated) return const LoginScreen();
if (authService.locked) return const LockScreen();
return const RootShell();
},
),
),
);
}
}