Phone App: connected service, charging cost, a tab picker and data export

Both READMEs claimed web parity with data export/import as the only
omission. That was three gaps out of date: the car screen had no
connected-service tab, no per-car charging-cost tab, and no way to say what
a car's page shows - all three of which the web has had since the car view
became a property of the car rather than of the browser.

The tab bar was the thing blocking the rest. It was a fixed list of eight
Tab(text: "Information") literals, so it could neither grow a tab nor read
an arrangement, and it sat outside the translation system that the rest of
the app has used since b6bb6b1. It now builds from the car's own tabOrder
and hiddenTabs, and labels come from car.tabs.* like the web's.

Rather than retype four subtrees of strings in three languages, the shared
ones - car.*, settings.advanced, forms.charging, forms.import and the
common keys the phone was missing - are copied out of the Web App's own
language files, with the phone's existing wording winning every collision.
Polish and Danish therefore arrive complete and cannot drift between the
two apps. Only five strings are genuinely phone-only: the reorder hint, the
saved-file message, the open action and two validation lines.

The connected-service tab mirrors ProviderPanel: headline readings, the
offer to take a provider odometer that is ahead of the stored one, the
vehicle record, and one collapsible card per capability, rendered from the
server's flattened key/value pairs so a provider adding a field surfaces it
without touching this app. Two deliberate differences. The raw-payload
disclosure is dropped - Toyota's eight sections are megabytes of JSON on a
phone screen, and the flattened fields carry the same content. And the
readings cannot be dragged here, though a stored metricOrder is still
honoured, so an arrangement made on the web carries over.

The view picker takes the same line on gestures. The web rearranges by
dragging the tab bar itself and the Information rows themselves; on a touch
screen that gesture belongs to the tab bar, so both arrangements are made
in the picker with a handle instead, and hiddenTabs, hiddenFields, tabOrder
and fieldOrder all save in one PUT. The key catalogues live in
car_view_sheet.dart and mirror hideableCarTabs / arrangeableCarTabs /
hideableCarFields in cars.go, because the server rejects anything else.
arrangeKeys applies a partial stored order the way the API documents it: an
unknown key is dropped and an unnamed one follows the arranged ones, which
is what puts a tab added in a later release at the end of somebody's page
rather than the middle of it.

Charging cost is the electric twin of Fuel and is built as one - the same
stats panel, tile and form shape, measured between full charges. It is the
per-car cost log, not the Charging section in the bottom bar, which remains
the charger network and OCPP control.

Export and import needed a phone answer to two browser affordances. The
export is written to the app's documents directory under the filename the
server's Content-Disposition names, and offered to whatever opens JSON via
open_filex - the same route attachments already take. The import goes
through the system file picker, validates the file locally, and confirms
with the number of cars the file actually holds, because the server always
creates new records and never merges.

The one field worth calling out on the client: _carPayload still leaves the
provider link and the view arrangement out, matching carPayload in
records.go, so saving the car form cannot silently unlink a car or undo an
arrangement.

Known gap, deliberately not closed here: the older sheets in
record_form_sheets.dart and most of car_detail_screen.dart still carry
hardcoded English. Everything added here and every tab label goes through
t(), but translating the rest of the car screen is its own change and would
have buried this one.

Verified by flutter analyze (clean), flutter test - 13 pass, 6 of them new,
covering arrangeKeys against partial, unknown and duplicate keys, the
charging models keeping uncomputed figures null rather than a plausible
zero, the new Car fields, and ProviderSnapshot parsing an unreachable
provider as an answer rather than a failure - and flutter build apk
--debug, which succeeds. The Kotlin Gradle plugin warnings in that build
are pre-existing.

Not verified: nothing was run against a live API Server or on a device, so
the new screens have not been driven end to end - only compiled, analyzed
and unit-tested.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
tajniak81andClaude Opus 5 committed 2026-08-21 21:33:58 +02:00
1 parent 215c027ada
commit e249c2f4d8
14 files changed
+3510 -203

No files matched your search

+132
View File
@@ -133,6 +133,29 @@ class ApiClient {
Future<void> deleteCar(String id) => _send("DELETE", "/cars/$id");
/// What this car's page shows and in which order — its own endpoint rather
/// than part of the car PATCH, so saving the car form can never silently
/// reveal a hidden tab or undo an arrangement. Every field is optional; only
/// the ones passed are written.
Future<Car> updateCarView(
String id, {
List<String>? hiddenTabs,
List<String>? hiddenFields,
List<String>? tabOrder,
List<String>? fieldOrder,
List<String>? metricOrder,
}) async {
final body = <String, dynamic>{
if (hiddenTabs != null) "hiddenTabs": hiddenTabs,
if (hiddenFields != null) "hiddenFields": hiddenFields,
if (tabOrder != null) "tabOrder": tabOrder,
if (fieldOrder != null) "fieldOrder": fieldOrder,
if (metricOrder != null) "metricOrder": metricOrder,
};
final data = await _send("PUT", "/cars/$id/view", body: body);
return Car.fromJson(Map<String, dynamic>.from(data));
}
// --- sharing (owner-only) ---
Future<List<CarShare>> listCarShares(String carId) async {
final data = await _send("GET", "/cars/$carId/shares") as List;
@@ -278,6 +301,31 @@ class ApiClient {
Future<void> deleteFuelEntry(String id) => _send("DELETE", "/fuel-entries/$id");
// --- charging sessions ---
// The electric counterpart of the fuel entries, on identical terms: the
// per-car list plus the derived summary, and CRUD on the flat collection.
Future<List<ChargingSession>> listCarChargingSessions(String carId) async {
final data = await _send("GET", "/cars/$carId/charging-sessions") as List;
return data.map((e) => ChargingSession.fromJson(Map<String, dynamic>.from(e))).toList();
}
Future<ChargingStats> getCarChargingStats(String carId) async {
final data = await _send("GET", "/cars/$carId/charging-stats");
return ChargingStats.fromJson(Map<String, dynamic>.from(data));
}
Future<ChargingSession> createChargingSession(Map<String, dynamic> body) async {
final data = await _send("POST", "/charging-sessions", body: body);
return ChargingSession.fromJson(Map<String, dynamic>.from(data));
}
Future<ChargingSession> updateChargingSession(String id, Map<String, dynamic> body) async {
final data = await _send("PATCH", "/charging-sessions/$id", body: body);
return ChargingSession.fromJson(Map<String, dynamic>.from(data));
}
Future<void> deleteChargingSession(String id) => _send("DELETE", "/charging-sessions/$id");
// --- maintenance ---
Future<List<MaintenanceEntry>> listCarMaintenance(String carId) async {
final data = await _send("GET", "/cars/$carId/maintenance") as List;
@@ -432,6 +480,90 @@ class ApiClient {
Future<void> cancelAccountDeletion() => _send("POST", "/me/delete/cancel");
Future<void> finalizeAccountDeletion() => _send("DELETE", "/me");
// --- data export / import ---
/// The whole account as a JSON file: the profile plus every car the user owns
/// with its service records and parts. Cars merely shared with them are not
/// included. Returns the bytes and the filename the server named it, which is
/// dated — the phone has to write the file itself, so it needs both.
Future<(List<int>, String)> exportData() async {
final res = await http.get(_uri("/me/export"),
headers: {if (token != null) "Authorization": "Bearer $token"});
if (res.statusCode == 401) {
onUnauthorized?.call();
throw ApiException(401, "Session expired — please log in again.");
}
if (res.statusCode < 200 || res.statusCode >= 300) {
throw ApiException(res.statusCode, _errorMessage(_tryDecode(res.body), res.reasonPhrase));
}
return (res.bodyBytes, _filenameFrom(res.headers["content-disposition"]));
}
/// Adds the cars in a previously exported file. Always creates new records —
/// nothing is merged with or overwritten, so importing the same file twice
/// leaves two copies rather than one updated one.
Future<ImportResult> importData(Map<String, dynamic> payload) async {
final data = await _send("POST", "/me/import", body: payload);
return ImportResult.fromJson(Map<String, dynamic>.from(data));
}
static dynamic _tryDecode(String body) {
try {
return jsonDecode(body);
} catch (_) {
return null;
}
}
/// The filename out of `attachment; filename="…"`, falling back to a plain
/// name so the export is still saveable if the header is missing or unquoted.
static String _filenameFrom(String? disposition) {
if (disposition == null) return "drivervault-export.json";
final m = RegExp(r'filename="?([^";]+)"?').firstMatch(disposition);
final name = m?.group(1)?.trim() ?? "";
return name.isEmpty ? "drivervault-export.json" : name;
}
// --- vehicle providers (the connected-service tab) ---
// Every call runs server-side under *this* user's manufacturer account, so a
// car shared from someone else only shows provider data when that vehicle is
// on this user's account too. A closed gate is a 200 with `unavailable` set
// rather than an error: "we asked, and here is why there is nothing".
Future<List<VehicleProvider>> listVehicleProviders() async {
final data = await _send("GET", "/vehicle-providers");
final items = (data["providers"] ?? []) as List;
return items.map((e) => VehicleProvider.fromJson(Map<String, dynamic>.from(e))).toList();
}
Future<List<ProviderVehicle>> listProviderVehicles(String provider) async {
final data = await _send("GET", "/vehicle-providers/${Uri.encodeComponent(provider)}/vehicles");
final items = (data["vehicles"] ?? []) as List;
return items.map((e) => ProviderVehicle.fromJson(Map<String, dynamic>.from(e))).toList();
}
Future<ProviderSnapshot> getCarProvider(String carId) async {
final data = await _send("GET", "/cars/$carId/provider");
return ProviderSnapshot.fromJson(Map<String, dynamic>.from(data));
}
/// Links this car to a vehicle on the caller's provider account; an empty
/// [provider] unlinks it. Returns the updated car.
Future<Car> linkCarProvider(String carId, {String provider = "", String vehicleId = ""}) async {
final data = await _send("POST", "/cars/$carId/provider",
body: {"provider": provider, "vehicleId": vehicleId});
return Car.fromJson(Map<String, dynamic>.from(data));
}
/// Re-applies the provider's data to the car. [include] selects what to take
/// (identity, fuelType, dates, odometer); omitted means everything.
Future<Car> syncCarProvider(String carId, {Map<String, bool>? include}) async {
final data = await _send("POST", "/cars/$carId/provider/sync",
body: {if (include != null) "include": include});
final car = (data is Map && data["car"] != null) ? data["car"] : data;
return Car.fromJson(Map<String, dynamic>.from(car));
}
// --- integrations (per-user plugin settings, superadmin → org → user cascade) ---
// getToyota/getAnkerSolix return the resolved view (effective/own/locked per
// field, secrets and inherited values masked); saveToyota/saveAnkerSolix write