diff --git a/API Server/README.md b/API Server/README.md index 05db82c..1709725 100644 --- a/API Server/README.md +++ b/API Server/README.md @@ -194,9 +194,10 @@ POST /api/vehicle-providers/{provider}/import # which PATCH /api/me {carOrder} sets) GET /api/cars POST /api/cars GET /api/cars/{id} PATCH /api/cars/{id} DELETE /api/cars/{id} -PUT /api/cars/{id}/view # which tabs, Information rows and service-history - # columns this car shows, and the order of the tabs, - # the rows, the columns and the provider readings +PUT /api/cars/{id}/view # which tabs, Information rows, service-history + # columns and service parts this car shows, and the + # order of the tabs, the rows, the columns and the + # provider readings GET /api/cars/{id}/provider POST /api/cars/{id}/provider POST /api/cars/{id}/provider/sync GET /api/cars/{id}/service-records GET /api/cars/{id}/technical-checks diff --git a/API Server/internal/api/cars.go b/API Server/internal/api/cars.go index 6ecc9f2..a32e904 100644 --- a/API Server/internal/api/cars.go +++ b/API Server/internal/api/cars.go @@ -295,6 +295,20 @@ var hideableServiceColumns = map[string]bool{ "notes": true, "file": true, } +// hideableServiceParts are the parts a service record can say were changed, and +// so the ones a car can take off its list: an EV changes no oil, and offering it +// on every service form is a checkbox nobody there will ever tick. Keys mirror +// SERVICE_PARTS in the web app's lib/serviceParts.js and kServiceParts in the +// phone's service_parts.dart — one per boolean on the service_records +// collection. +// +// Every part is hideable, unlike the tabs and the columns: there is no part the +// form needs, because a service that changed nothing at all is a service with an +// empty Changed parts section, which is a thing that happens. +var hideableServiceParts = map[string]bool{ + "oil": true, "engineFilter": true, "cabinFilter": true, +} + // arrangeableServiceColumns are the columns that table can be rearranged into: // the hideable ones plus Date, which cannot be switched off but has no reason to // be stuck at the left. Derived from hideableServiceColumns so the two sets @@ -346,10 +360,11 @@ func normalizeKeys(in []string, allowed map[string]bool, what string) ([]string, } // PUT /api/cars/{id}/view — choose what this car's page shows: which tabs, which -// rows of the Information tab, which columns of the Service history table, and -// the order the tabs, the Information rows, those columns and the connected -// service's headline readings are laid out in. Body: {hiddenTabs?: [...], -// hiddenFields?: [...], hiddenServiceColumns?: [...], tabOrder?: [...], +// rows of the Information tab, which columns of the Service history table, which +// parts its service form offers, and the order the tabs, the Information rows, +// those columns and the connected service's headline readings are laid out in. +// Body: {hiddenTabs?: [...], hiddenFields?: [...], hiddenServiceColumns?: [...], +// hiddenServiceParts?: [...], tabOrder?: [...], // fieldOrder?: [...], serviceColumnOrder?: [...], metricOrder?: [...]}; only the // lists present are written, so a client can rearrange one group without // resending the others. Its own endpoint rather than fields on the car edit, so an ordinary @@ -361,6 +376,7 @@ func (s *Server) updateCarView(w http.ResponseWriter, r *http.Request) { HiddenTabs *[]string `json:"hiddenTabs"` HiddenFields *[]string `json:"hiddenFields"` HiddenServiceColumns *[]string `json:"hiddenServiceColumns"` + HiddenServiceParts *[]string `json:"hiddenServiceParts"` TabOrder *[]string `json:"tabOrder"` FieldOrder *[]string `json:"fieldOrder"` ServiceColumnOrder *[]string `json:"serviceColumnOrder"` @@ -405,6 +421,14 @@ func (s *Server) updateCarView(w http.ResponseWriter, r *http.Request) { } payload["hidden_service_columns"] = columns } + if in.HiddenServiceParts != nil { + parts, err := normalizeKeys(*in.HiddenServiceParts, hideableServiceParts, "service part") + if err != nil { + writeError(w, http.StatusBadRequest, err.Error()) + return + } + payload["hidden_service_parts"] = parts + } if in.TabOrder != nil { // A wider set than the hidden tabs: Information is arrangeable although it // cannot be switched off. A partial list is accepted, and the tabs it diff --git a/API Server/internal/api/cartabs_test.go b/API Server/internal/api/cartabs_test.go index a66a298..4b8a8fe 100644 --- a/API Server/internal/api/cartabs_test.go +++ b/API Server/internal/api/cartabs_test.go @@ -200,6 +200,49 @@ func TestNormalizeHiddenServiceColumns(t *testing.T) { } } +// The parts a service can change hide against their own set — one key per +// boolean on the service_records collection, and no order beside it, because +// they are a checkbox list inside a single column. +func TestNormalizeHiddenServiceParts(t *testing.T) { + got, err := normalizeKeys([]string{" oil ", "cabinFilter", "oil", ""}, hideableServiceParts, "service part") + if err != nil { + t.Fatalf("normalizeKeys: %v", err) + } + assertKeys(t, got, []string{"oil", "cabinFilter"}) // trimmed, blanks dropped, deduped + + // Every part is hideable, unlike the tabs and the columns: a service that + // changed nothing is a real service, so there is no part the form must keep. + for _, key := range []string{"oil", "engineFilter", "cabinFilter"} { + if !hideableServiceParts[key] { + t.Errorf("service part %q should be hideable", key) + } + } + if len(hideableServiceParts) != 3 { + t.Errorf("hideableServiceParts has %d entries, want the 3 booleans on a service record", len(hideableServiceParts)) + } + + // "parts" is the column those three share; it is not one of them. Sending + // the column key here would hide nothing and quietly succeed if the two sets + // were not kept apart. + if _, err := normalizeKeys([]string{"parts"}, hideableServiceParts, "service part"); err == nil { + t.Error("normalizeKeys accepted the parts column as a part, want an error") + } + if _, err := normalizeKeys([]string{"notes"}, hideableServiceParts, "service part"); err == nil { + t.Error("normalizeKeys accepted a column key as a service part, want an error") + } + if _, err := normalizeKeys([]string{"oil", "nonsense"}, hideableServiceParts, "service part"); err == nil { + t.Error("normalizeKeys accepted an unknown service part, want an error") + } + + // And no part is a column, which is what keeps the table from widening as + // parts are added. + for key := range hideableServiceParts { + if arrangeableServiceColumns[key] { + t.Errorf("service part %q must not also be a column", key) + } + } +} + // The columns arrange against a wider set than they hide against, the way the // tabs do: the date cannot be switched off, but it can be moved off the left. func TestNormalizeServiceColumnOrder(t *testing.T) { diff --git a/API Server/internal/api/records.go b/API Server/internal/api/records.go index 41f3fce..caa352b 100644 --- a/API Server/internal/api/records.go +++ b/API Server/internal/api/records.go @@ -68,7 +68,8 @@ type carRecord struct { Created string `json:"created"` Updated string `json:"updated"` - // Switched-off tabs, Information fields and Service history columns, plus + // Switched-off tabs, Information fields, Service history columns and service + // parts, plus // the arrangements of the tabs, the Information rows, those columns and the // connected service's readings. Raw // because PocketBase hands back whatever a json field holds — null on a car @@ -76,6 +77,7 @@ type carRecord struct { HiddenTabs json.RawMessage `json:"hidden_tabs"` HiddenFields json.RawMessage `json:"hidden_fields"` HiddenServiceColumns json.RawMessage `json:"hidden_service_columns"` + HiddenServiceParts json.RawMessage `json:"hidden_service_parts"` TabOrder json.RawMessage `json:"tab_order"` FieldOrder json.RawMessage `json:"field_order"` ServiceColumnOrder json.RawMessage `json:"service_column_order"` @@ -109,6 +111,7 @@ func (rec carRecord) toModel() models.Car { HiddenTabs: decodeStringList(rec.HiddenTabs), HiddenFields: decodeStringList(rec.HiddenFields), HiddenServiceColumns: decodeStringList(rec.HiddenServiceColumns), + HiddenServiceParts: decodeStringList(rec.HiddenServiceParts), TabOrder: decodeStringList(rec.TabOrder), FieldOrder: decodeStringList(rec.FieldOrder), ServiceColumnOrder: decodeStringList(rec.ServiceColumnOrder), diff --git a/API Server/internal/bootstrap/schema.go b/API Server/internal/bootstrap/schema.go index 16e88e6..046ba61 100644 --- a/API Server/internal/bootstrap/schema.go +++ b/API Server/internal/bootstrap/schema.go @@ -52,6 +52,9 @@ var collectionsSchema = map[string][]fieldDef{ // never records what was changed). Date is not hideable and so never // appears here. fJSON("hidden_service_columns", 2000), + // And the parts its services never change (["oil"] on an EV), which come + // off the service form and out of that column together. + fJSON("hidden_service_parts", 2000), // The order the tabs are laid out in, as tab keys, the same for the // Information rows, the Service history columns, and the connected // service's headline readings. Empty means the page's own default order. diff --git a/API Server/internal/models/models.go b/API Server/internal/models/models.go index 7465684..1442236 100644 --- a/API Server/internal/models/models.go +++ b/API Server/internal/models/models.go @@ -91,6 +91,16 @@ type Car struct { // nothing. HiddenServiceColumns []string `json:"hiddenServiceColumns"` + // HiddenServiceParts is the parts this car's services never change, as part + // keys (["oil"] on an EV, which has none to change). They come off the + // Changed parts section of the service form and out of the history's parts + // column together: a part nobody records is one nobody wants offered either. + // The hidden set like the ones above, so a part added later is on by + // default, and there is no order beside it — the parts are a checkbox list + // inside one column, and their position says nothing a tab's or a column's + // does. + HiddenServiceParts []string `json:"hiddenServiceParts"` + // ServiceColumnOrder is the arrangement of those columns, covering the // hidden ones so a column switched back on returns to where it was. It does // include "date", which cannot be switched off but can be moved off the diff --git a/Phone App/README.md b/Phone App/README.md index 86052f1..93cbc83 100644 --- a/Phone App/README.md +++ b/Phone App/README.md @@ -27,9 +27,11 @@ navigation bar** — Garage, Charging, Settings, and Users for admins — in an **Which tabs a car shows, and in what order, belongs to the car** — the same arrangement the web app reads, so everyone it is shared with sees the same - page. Edit it under the **tune** icon: switch tabs, Information rows and - Service history columns on or off, and drag any of the three lists by its - handle to reorder it. (The web rearranges by dragging the tab bar and the + page. Edit it under the **tune** icon: switch tabs, Information rows, Service + history columns and the parts a service can change on or off, and drag any of + the first three lists by its handle to reorder it. (The parts have no order — + they are a checkbox list inside one column, and their position says nothing a + tab's or a column's does.) (The web rearranges by dragging the tab bar and the column headings themselves; on a touch screen those gestures belong to the tab bar and the scroll, so every arrangement is made in the picker instead.) Two things cannot be switched off: Information — a page with no tabs left would be @@ -57,7 +59,10 @@ navigation bar** — Garage, Charging, Settings, and Users for admins — in an have changed shares the one **parts** column — they are a growing list, and a column apiece would widen the web's table without end — and both that column and the form's Changed parts section come from `lib/service_parts.dart`, the - twin of the web's `lib/serviceParts.js`. + twin of the web's `lib/serviceParts.js`. Which of them this car records is + the car's own choice as well: a part switched off leaves the form's + checkboxes and the cards' chips together, and nothing is written to the + records, so switching it back on brings the old chips back. - **Technical check history** — the mandatory roadworthiness inspections (przegląd techniczny, MOT, TÜV). Result, cost, station, and the certificate's valid-until, which overrides the car's interval when present. A failed check diff --git a/Phone App/assets/i18n/da.json b/Phone App/assets/i18n/da.json index 80b7336..43b8278 100644 --- a/Phone App/assets/i18n/da.json +++ b/Phone App/assets/i18n/da.json @@ -377,6 +377,8 @@ "fieldsOrderHint": "Træk felterne på fanen Oplysninger for at ændre deres rækkefølge.", "serviceColumnsHeading": "Kolonner i servicehistorik", "columnAlwaysOn": "{column} vises altid.", + "servicePartsHeading": "Udskiftede dele", + "servicePartsHint": "En fravalgt del forsvinder fra serviceformularen og fra historikken. Det, der allerede er registreret, bevares og kommer tilbage med den.", "reorderHint": "Træk en række i håndtaget for at ændre rækkefølgen.", "tabsReorder": "Faneblade", "fieldsReorder": "Informationsfelter" diff --git a/Phone App/assets/i18n/en.json b/Phone App/assets/i18n/en.json index bbacb18..24d28b3 100644 --- a/Phone App/assets/i18n/en.json +++ b/Phone App/assets/i18n/en.json @@ -377,6 +377,8 @@ "fieldsOrderHint": "Drag the fields on the Information tab to change the order they appear in.", "serviceColumnsHeading": "Service history columns", "columnAlwaysOn": "{column} is always shown.", + "servicePartsHeading": "Changed parts", + "servicePartsHint": "A part switched off comes off the service form and out of the history. What is already recorded is kept, and comes back with it.", "reorderHint": "Drag a row by its handle to change the order.", "tabsReorder": "Tabs", "fieldsReorder": "Information fields" diff --git a/Phone App/assets/i18n/pl.json b/Phone App/assets/i18n/pl.json index db7d0e2..002b02c 100644 --- a/Phone App/assets/i18n/pl.json +++ b/Phone App/assets/i18n/pl.json @@ -381,6 +381,8 @@ "fieldsOrderHint": "Przeciągnij pola na zakładce Informacje, aby zmienić ich kolejność.", "serviceColumnsHeading": "Kolumny historii serwisowej", "columnAlwaysOn": "Kolumna {column} jest zawsze widoczna.", + "servicePartsHeading": "Wymienione części", + "servicePartsHint": "Wyłączona część znika z formularza serwisu i z historii. Zapisane dane pozostają i wracają razem z nią.", "reorderHint": "Przeciągnij wiersz za uchwyt, aby zmienić kolejność.", "tabsReorder": "Zakładki", "fieldsReorder": "Pola informacyjne" diff --git a/Phone App/lib/api.dart b/Phone App/lib/api.dart index 52aaa6e..95565e0 100644 --- a/Phone App/lib/api.dart +++ b/Phone App/lib/api.dart @@ -146,6 +146,7 @@ class ApiClient { List? metricOrder, List? hiddenServiceColumns, List? serviceColumnOrder, + List? hiddenServiceParts, }) async { final body = { if (hiddenTabs != null) "hiddenTabs": hiddenTabs, @@ -155,6 +156,7 @@ class ApiClient { if (metricOrder != null) "metricOrder": metricOrder, if (hiddenServiceColumns != null) "hiddenServiceColumns": hiddenServiceColumns, if (serviceColumnOrder != null) "serviceColumnOrder": serviceColumnOrder, + if (hiddenServiceParts != null) "hiddenServiceParts": hiddenServiceParts, }; final data = await _send("PUT", "/cars/$id/view", body: body); return Car.fromJson(Map.from(data)); diff --git a/Phone App/lib/models.dart b/Phone App/lib/models.dart index 7906a32..62c5c5f 100644 --- a/Phone App/lib/models.dart +++ b/Phone App/lib/models.dart @@ -107,6 +107,12 @@ class Car { final List hiddenServiceColumns; final List serviceColumnOrder; + /// The parts this car's services never change, as part keys. They come off + /// the service form and out of the history's chips together — a part nobody + /// records is one nobody wants offered. No order beside it: the parts are a + /// checkbox list inside one column, and their position says nothing. + final List hiddenServiceParts; + Car({ required this.id, required this.name, @@ -138,6 +144,7 @@ class Car { this.metricOrder = const [], this.hiddenServiceColumns = const [], this.serviceColumnOrder = const [], + this.hiddenServiceParts = const [], }); factory Car.fromJson(Map j) => Car( @@ -171,6 +178,7 @@ class Car { metricOrder: _asStrList(j["metricOrder"]), hiddenServiceColumns: _asStrList(j["hiddenServiceColumns"]), serviceColumnOrder: _asStrList(j["serviceColumnOrder"]), + hiddenServiceParts: _asStrList(j["hiddenServiceParts"]), ); bool get isOwner => access == "owner"; diff --git a/Phone App/lib/screens/car_detail_screen.dart b/Phone App/lib/screens/car_detail_screen.dart index 5a62765..568e2e0 100644 --- a/Phone App/lib/screens/car_detail_screen.dart +++ b/Phone App/lib/screens/car_detail_screen.dart @@ -470,6 +470,7 @@ class _CarDetailScreenState extends State { final columns = arrangeKeys(kServiceColumnKeys, car.serviceColumnOrder) .where((key) => !car.hiddenServiceColumns.contains(key)) .toList(); + final parts = visibleParts(car); return _TabList( empty: data.services.isEmpty ? t("car.services.empty") : null, onAdd: car.canWrite ? () => _addService(car) : null, @@ -478,6 +479,7 @@ class _CarDetailScreenState extends State { .map((s) => _ServiceTile( record: s, columns: columns, + parts: parts, onEdit: car.canWrite ? () => _editService(car, s) : null, onDelete: car.canWrite ? () => _deleteService(s) : null, )) @@ -825,11 +827,16 @@ class _ServiceTile extends StatelessWidget { /// The visible column keys, already arranged. Never empty: date cannot be /// hidden. final List columns; + + /// The parts this car still records. Passed in rather than read off the car + /// for the same reason the columns are: every row shows the same ones. + final List parts; final VoidCallback? onEdit; final VoidCallback? onDelete; const _ServiceTile({ required this.record, required this.columns, + required this.parts, this.onEdit, this.onDelete, }); @@ -912,7 +919,7 @@ class _ServiceTile extends StatelessWidget { /// them in one table cell and so names the first and counts the rest behind a /// panel; a card has the width to simply show them all. Widget _parts(BuildContext context) { - final changed = changedParts(record); + final changed = changedParts(record, parts); if (changed.isEmpty) return _empty(context, "parts"); return Wrap( spacing: 6, @@ -1976,7 +1983,14 @@ class _ServiceSheetState extends State<_ServiceSheet> { /// Which parts this record says were changed, keyed the way [kServiceParts] /// keys them, so a part added to that list turns up in this sheet without a /// second edit here. + /// + /// Every part, not only the shown ones: an edit has to send back what a part + /// this car has switched off already said, because the API rewrites all of the + /// booleans from the body and an omitted one would come back false. late final Map _changed; + + /// The parts this car still records, and so the only ones with a checkbox. + late final List _parts; final _pending = PendingAttachment(); bool _saving = false; String? _error; @@ -1990,11 +2004,14 @@ class _ServiceSheetState extends State<_ServiceSheet> { _date = r?.date ?? DateTime.now(); _km = TextEditingController(text: r == null ? "" : "${r.km}"); _notes = TextEditingController(text: r?.notes ?? ""); + _parts = visibleParts(widget.car); // An existing record is read through the part's own accessor; a new one - // starts from the part's default, which is why an oil change comes ticked. + // starts from the part's default, which is why an oil change comes ticked — + // unless this car has switched that part off, since ticking a box nobody was + // shown is not a default, it's a guess. _changed = { for (final part in kServiceParts) - part.key: r == null ? part.initial : part.changed(r), + part.key: r == null ? part.initial && _parts.contains(part) : part.changed(r), }; } @@ -2092,7 +2109,7 @@ class _ServiceSheetState extends State<_ServiceSheet> { ], ), const SizedBox(height: 8), - for (final part in kServiceParts) + for (final part in _parts) CheckboxListTile( value: _changed[part.key] ?? false, onChanged: (v) => setState(() => _changed[part.key] = v ?? false), diff --git a/Phone App/lib/screens/car_view_sheet.dart b/Phone App/lib/screens/car_view_sheet.dart index cf4f77b..d90f096 100644 --- a/Phone App/lib/screens/car_view_sheet.dart +++ b/Phone App/lib/screens/car_view_sheet.dart @@ -3,6 +3,7 @@ import "package:flutter/material.dart"; import "../i18n.dart"; import "../main.dart"; import "../models.dart"; +import "../service_parts.dart"; import "../theme.dart"; /// Every tab a car's page can show, in the order they appear when the car has no @@ -179,6 +180,7 @@ class _CarViewSheetState extends State { late Set _visibleTabs; late Set _visibleFields; late Set _visibleColumns; + late Set _visibleServiceParts; bool _saving = false; String? _error; @@ -194,6 +196,7 @@ class _CarViewSheetState extends State { _visibleColumns = kHideableServiceColumnKeys .where((k) => !car.hiddenServiceColumns.contains(k)) .toSet(); + _visibleServiceParts = visibleParts(car).map((p) => p.key).toSet(); } /// Whether [key] is one of the rows on offer below: Information never is @@ -253,6 +256,10 @@ class _CarViewSheetState extends State { tabOrder: _tabOrder, fieldOrder: _fieldOrder, serviceColumnOrder: _columnOrder, + hiddenServiceParts: kServiceParts + .where((p) => !_visibleServiceParts.contains(p.key)) + .map((p) => p.key) + .toList(), ); if (mounted) Navigator.pop(context, car); } catch (e) { @@ -381,6 +388,35 @@ class _CarViewSheetState extends State { style: TextStyle(fontSize: 11, color: DriverVault.muted(context)), ), ), + const SizedBox(height: 20), + _heading(context, t("car.viewPicker.servicePartsHeading")), + // No drag handle here, and no stored order: the parts are a + // checkbox list inside one column, so their position carries + // nothing a tab's or a column's does. + for (final part in kServiceParts) + Padding( + padding: const EdgeInsets.symmetric(vertical: 2), + child: Row( + children: [ + Checkbox( + value: _visibleServiceParts.contains(part.key), + onChanged: (v) => setState(() => (v ?? false) + ? _visibleServiceParts.add(part.key) + : _visibleServiceParts.remove(part.key)), + ), + Expanded( + child: Text(t(part.label), style: const TextStyle(fontSize: 14)), + ), + ], + ), + ), + Padding( + padding: const EdgeInsets.only(top: 6), + child: Text( + t("car.viewPicker.servicePartsHint"), + style: TextStyle(fontSize: 11, color: DriverVault.muted(context)), + ), + ), if (_error != null) Padding( padding: const EdgeInsets.only(top: 12), diff --git a/Phone App/lib/service_parts.dart b/Phone App/lib/service_parts.dart index 2498a80..accf458 100644 --- a/Phone App/lib/service_parts.dart +++ b/Phone App/lib/service_parts.dart @@ -76,9 +76,22 @@ final List kServiceParts = [ ), ]; -/// The parts this record says were changed. A record written before a part -/// existed simply doesn't carry its field, which the model reads as false — -/// "not changed" rather than "unknown", because that service genuinely didn't -/// change it. -List changedParts(ServiceRecord service) => - kServiceParts.where((part) => part.changed(service)).toList(); +/// The parts a car actually records, which is every one it hasn't switched off. +/// An EV changes no oil, and a checkbox nobody will ever tick is one more thing +/// to read past on every service. The hidden set rather than the visible one, so +/// a part added in a later release is on by default. +List visibleParts(Car car) => + kServiceParts.where((part) => !car.hiddenServiceParts.contains(part.key)).toList(); + +/// The parts this record says were changed, among the ones [parts] still shows. +/// A record written before a part existed simply doesn't carry its field, which +/// the model reads as false — "not changed" rather than "unknown", because that +/// service genuinely didn't change it. +/// +/// A part switched off disappears from the history as well as from the form, the +/// same way a hidden column does: what "I don't record this" means is that it +/// stops taking up room, not that it takes up room saying nothing. Its stored +/// boolean is left alone, so switching it back on brings the old records' chips +/// back with it. +List changedParts(ServiceRecord service, List parts) => + parts.where((part) => part.changed(service)).toList(); diff --git a/Phone App/test/car_view_sheet_test.dart b/Phone App/test/car_view_sheet_test.dart index b623cd6..8849f44 100644 --- a/Phone App/test/car_view_sheet_test.dart +++ b/Phone App/test/car_view_sheet_test.dart @@ -12,6 +12,7 @@ import "package:drivervault_phone/i18n.dart"; import "package:drivervault_phone/main.dart"; import "package:drivervault_phone/models.dart"; import "package:drivervault_phone/screens/car_view_sheet.dart"; +import "package:drivervault_phone/service_parts.dart"; void main() { TestWidgetsFlutterBinding.ensureInitialized(); @@ -83,14 +84,37 @@ void main() { await tester.pumpAndSettle(); } - Car car({List hiddenColumns = const [], List columnOrder = const []}) => + Car car({ + List hiddenColumns = const [], + List columnOrder = const [], + List hiddenParts = const [], + }) => Car.fromJson({ "id": "car1", "name": "bZ4X", "hiddenServiceColumns": hiddenColumns, "serviceColumnOrder": columnOrder, + "hiddenServiceParts": hiddenParts, }); + // The sheet stacks its sections in one list: tabs, Information rows, the + // Service history columns, then the parts. Counted from the end rather than + // the start so the tab rows — whose number depends on showProvider — don't + // have to be worked out here. + List ticks(WidgetTester tester) => tester + .widgetList(find.byType(Checkbox)) + .map((b) => b.value ?? false) + .toList(); + List columnTicks(WidgetTester tester) { + final all = ticks(tester); + final end = all.length - kServiceParts.length; + return all.sublist(end - kServiceColumnKeys.length, end); + } + List partTicks(WidgetTester tester) { + final all = ticks(tester); + return all.sublist(all.length - kServiceParts.length); + } + testWidgets("offers every column, with the date locked on", (tester) async { await pump(tester, car()); @@ -114,15 +138,33 @@ void main() { testWidgets("a switched-off column comes back unticked", (tester) async { await pump(tester, car(hiddenColumns: ["parts", "file"])); - // Read the ticks in the order the rows are laid out — the sheet lists the - // tabs and the Information rows above the columns, so the columns are the - // last seven. - final ticks = tester + expect(columnTicks(tester), [true, true, true, true, false, true, false]); + }); + + testWidgets("offers every part, none of them locked", (tester) async { + await pump(tester, car()); + + expect(find.text(t("car.viewPicker.servicePartsHeading").toUpperCase()), + findsOneWidget); + for (final part in kServiceParts) { + expect(find.text(t(part.label)), findsWidgets, + reason: "the ${part.key} part is not on offer"); + } + expect(partTicks(tester), List.filled(kServiceParts.length, true)); + + // Only the date is locked. Every part can be switched off: a service that + // changed nothing is a real service, so the form needs none of them. + final locked = tester .widgetList(find.byType(Checkbox)) - .map((b) => b.value ?? false) - .toList(); - final columns = ticks.sublist(ticks.length - kServiceColumnKeys.length); - expect(columns, [true, true, true, true, false, true, false]); + .where((b) => b.onChanged == null); + expect(locked, hasLength(1)); + }); + + testWidgets("a switched-off part comes back unticked", (tester) async { + await pump(tester, car(hiddenParts: ["oil", "cabinFilter"])); + expect(partTicks(tester), [false, true, false]); + // And it changes nothing about the columns, which are their own set. + expect(columnTicks(tester), List.filled(kServiceColumnKeys.length, true)); }); testWidgets("the rows follow the car's arrangement", (tester) async { diff --git a/Phone App/test/models_format_test.dart b/Phone App/test/models_format_test.dart index 3f37638..97602de 100644 --- a/Phone App/test/models_format_test.dart +++ b/Phone App/test/models_format_test.dart @@ -299,18 +299,40 @@ void main() { "changedCabinAirFilter": cabin, }); - expect(changedParts(record()).isEmpty, isTrue); - expect(changedParts(record(oil: true)).map((p) => p.key), ["oil"]); + final all = kServiceParts; + expect(changedParts(record(), all).isEmpty, isTrue); + expect(changedParts(record(oil: true), all).map((p) => p.key), ["oil"]); // Catalogue order, not the order the fields happen to be read in. expect( - changedParts(record(cabin: true, oil: true)).map((p) => p.key), + changedParts(record(cabin: true, oil: true), all).map((p) => p.key), ["oil", "cabinFilter"], ); // A record written before a part existed carries no field for it, which // reads as "not changed" rather than as a missing value. final old = ServiceRecord.fromJson({"id": "s", "car": "c", "km": 0}); - expect(changedParts(old).isEmpty, isTrue); + expect(changedParts(old, all).isEmpty, isTrue); + }); + + test("a part a car has switched off leaves its records alone", () { + final car = Car.fromJson({"id": "c", "name": "bZ4X", "hiddenServiceParts": ["oil"]}); + expect(car.hiddenServiceParts, ["oil"]); + expect(visibleParts(car).map((p) => p.key), ["engineFilter", "cabinFilter"]); + + // A car nobody has configured offers every part — the hidden set, so one + // added in a later release is on by default. + expect(visibleParts(Car.fromJson({"id": "d", "name": "Yaris"})).length, kServiceParts.length); + + // The chips follow the car, so an oil change recorded before the part was + // switched off stops being shown... + final oiled = ServiceRecord.fromJson({ + "id": "s", "car": "c", "km": 90000, + "changedOil": true, "changedCabinAirFilter": true, + }); + expect(changedParts(oiled, visibleParts(car)).map((p) => p.key), ["cabinFilter"]); + // ...but the record still says so, which is what brings the chip back if the + // part is switched on again. Nothing here writes to the record. + expect(oiled.changedOil, isTrue); }); test("arrangeKeys: partial orders keep every key, unknown ones are dropped", () { diff --git a/Web App/README.md b/Web App/README.md index c719be5..57e8810 100644 --- a/Web App/README.md +++ b/Web App/README.md @@ -141,6 +141,15 @@ Config (`server/.env`, copy from `.env.example`): the column and the form's Changed parts section are driven by one list in `lib/serviceParts.js`, so adding a part is one entry there plus its boolean on the API's `service_records` collection. + + **Which parts a car records is the car's** too, under the same picker. An EV + changes no oil, and a checkbox nobody will ever tick is one more thing to read + past on every service. A part switched off leaves the form, the column's + summary and the panel together — "I don't record this" means it stops taking + up room, not that it takes up room saying nothing. Nothing is written to the + records: an edit sends a hidden part's stored boolean straight back, because + the API rewrites all of them from the body, so switching the part on again + brings the old services' chips back with it. - **Arranging the Service history columns** — the column headings on that tab drag into any order, saved on drop, and it covers the hidden columns too. Date is arrangeable although it can't be switched off, the same rule Information diff --git a/Web App/web/src/components/ServiceFormModal.vue b/Web App/web/src/components/ServiceFormModal.vue index 199e975..2e5cd8d 100644 --- a/Web App/web/src/components/ServiceFormModal.vue +++ b/Web App/web/src/components/ServiceFormModal.vue @@ -3,7 +3,7 @@ import { ref } from "vue"; import { api } from "../api"; import { formatKm } from "../lib/format.js"; import { applyAttachment } from "../lib/attachment.js"; -import { SERVICE_PARTS } from "../lib/serviceParts.js"; +import { SERVICE_PARTS, visibleParts } from "../lib/serviceParts.js"; import { t } from "../i18n"; import AttachmentField from "./AttachmentField.vue"; import DateField from "./DateField.vue"; @@ -19,6 +19,11 @@ const emit = defineEmits(["saved", "close"]); const isEdit = !!props.service; const saving = ref(false); const error = ref(""); +// The parts this car records. Read once rather than as a computed: the dialog is +// mounted per open, and a part switching off under an open form would rearrange +// it mid-edit. +const shown = visibleParts(props.car); +const hidden = SERVICE_PARTS.filter((part) => !shown.includes(part)).map((part) => part.key); const form = ref({ date: props.service ? toDateInput(props.service.date) : new Date().toISOString().slice(0, 10), km: props.service?.km ?? "", @@ -26,8 +31,19 @@ const form = ref({ // part added to it turns up in this dialog without a second edit. An existing // record written before a part existed has no field for it, which reads as // unchecked. + // + // Every part, not only the shown ones: an edit has to send back what a hidden + // part already said, because the API rewrites all three booleans from the body + // and an omitted one would come back false. A *new* record starts a hidden + // part at false rather than its `initial`, since ticking a box nobody was + // shown is not a default, it's a guess. ...Object.fromEntries( - SERVICE_PARTS.map((part) => [part.field, props.service ? !!props.service[part.field] : part.initial]) + SERVICE_PARTS.map((part) => [ + part.field, + props.service + ? !!props.service[part.field] + : part.initial && !hidden.includes(part.key), + ]) ), notes: props.service?.notes ?? "", }); @@ -82,10 +98,10 @@ async function submit() { -
+
{{ t("forms.service.changedParts") }}