The web app can be pointed at two DriverVault stacks and switch between them in a click. The phone had one address and one session: reaching a second garage meant retyping the API base in Server settings and signing in again, losing the first server's token on the way — the same act, undone, every time you switched back. So lib/servers.dart is the web's servers.js ported rather than reinvented, down to the storage keys: cc_servers holds the list, cc_active_server the one being read, cc_session_<id> the token minted by that server and no other. The two apps describe the same thing the same way, and the upgrade path falls out of it — cc_token, cc_user and cc_server_url are read once at boot and folded onto the home entry, so the build carrying this signs nobody out. Home is the address the build ships with (kDefaultApiBase, still overridable per device from the login screen) and cannot be removed: it is what a dropped session falls back to. Any other server is added by address, with /api appended if the path is left off, because a server a phone can reach is internet-facing already. The part worth reading twice is which session a rejection ends. ApiClient no longer holds a base or a token — it pins the active server's id, base and token at the moment a request goes out, so a 401 arriving after a switch clears the session of the server that actually refused it rather than whichever one is active by then. The fallback is the web's: a remote server timing out drops its own token, the app returns to home while home is still signed in, and only when nothing is left to fall back to does the login screen come back. Log out still clears every server at once, since leaving the app means leaving all of them. Switching rebuilds the shell, keyed on the active id, because record ids belong to the server that issued them — a garage, a charging page and a settings panel still holding the other server's rows would each have to be told to forget them separately. The appearance prefs come across with the profile of whoever owns the account on the server now active. Where the picker lives is the one place the phone cannot copy the web. There is no app rail here, so it became the first button in the Garage header, beside the theme toggle and log out, which is that same cluster. It names the active server once there is a choice and goes straight to adding the second when there isn't; the eyebrow reads GARAGE · Work for the reason the rail names it — two garages otherwise look identical. The login screen gets its own way in, because a remote session can expire and land you there with that server still active, and a picker reachable only from inside the app would leave nowhere to go. One judgment call inside the sheet: saving a connected server at a new address saves and stops, rather than falling through to the sign-in it now needs. The token was minted by the PocketBase behind the old address and is dropped with it, but the credentials to replace it were never asked for, so treating the save as a login would report an empty password as the error. The strings are copied out of Web App/web/src/i18n/ like the rest of the shared wording. Two are not the web's: home reads "the address this app ships with" rather than "served with this app", since the phone has no origin to be served from, and sameOrigin has no meaning here at all and was dropped. Biometric sign-in stays global. It was never per-server and replays its stored credentials against whichever server is active; making it per-server is a change of its own, and the login screen now names the server it is about to sign into. Nothing changes on the API Server. On Android there is no origin to allow, so the CORS list the web app has to satisfy to reach a second server doesn't enter into it. Verified: flutter analyze is clean and flutter test passes, 35 tests to 46. The new ones cover the registry — a bare origin gaining its /api, a fresh install knowing one unnamed server on the built-in address, the legacy keys landing on home and being cleared, two servers holding their tokens apart, a rename keeping a session where a move drops it, removing the active server falling back to a home that is still signed in, home refusing to be removed, and a restart reading the list, the active id and every session back. Not verified: none of it has been run. There is no device or emulator on this machine and no API Server to answer, so the picker, the add sheet, a real connect, the 401 fallback and the shell rebuild on a switch exist only as code the analyzer is happy with — the tests reach the registry, not a screen. No APK was built. The legacy migration was exercised against mocked SharedPreferences, which is not a phone that had the old build on it: that is the first thing to check on a device, since the failure mode is a silent sign-out. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
125 lines
4.5 KiB
Dart
125 lines
4.5 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 "i18n.dart";
|
|
import "servers.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();
|
|
// Load the UI translation files before the first frame so t() is ready.
|
|
await loadTranslations();
|
|
// The servers the app can read from, and the session held for each — read
|
|
// before anything else so the first request knows where to go.
|
|
await serverRegistry.load();
|
|
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();
|
|
// Keyed on the active server: switching servers swaps the garage,
|
|
// the charging page and the settings together, and a fresh shell is
|
|
// what makes every one of them re-read from the new one rather than
|
|
// keep showing the cars of the old.
|
|
return RootShell(key: ValueKey(serverRegistry.activeId));
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|