A build date you may only half know; one look for an empty cell
Two changes, both about showing what is actually known rather than a tidier version of it. The build date asked for a day. A car's build date is often only a year, or a month and a year - the VIN plate is stamped with a month, the papers carry a day, a grey import neither - so a field insisting on all three is answered either with an invented day or with nothing, and both throw away what the owner did know. The field now picks its own precision: a full date, a month and year, or a year, each with the control that suits it. A year is typed rather than picked, because a date picker that makes you walk back to 1998 is worse than four keystrokes. Stored as the ISO prefix - "2015", "2015-03", "2015-03-10" - which is ISO 8601 reduced precision, and printed back at exactly that precision. The three shapes sort and compare as strings in date order, which is why the prefix is stored rather than a date with a precision field beside it. The formatter takes the string apart rather than parsing it: "2015-03" read as a UTC instant and printed in local time hands back February west of Greenwich. Narrowing the precision keeps what is still true, so a day dropped from "2015-03-10" leaves "2015-03". Widening clears the field. That is the awkward half of the control and it is deliberate: there is nothing to widen a year with, and leaving "2015" behind an empty month box would store a date the screen is not showing. The column was free text with no validation at all, which was tolerable while only a date picker could write it and is not now that three shapes are legal. normalizeBuildDate parses rather than pattern-matches, so "2015-13" and "2015-02-31" are refused instead of stored as something no reader can print. The phone needed changing to avoid destroying this. It parsed buildDate with DateTime.tryParse, which returns null for "2015" - so a half-known date would have shown as a dash, and saving the car from the phone would have written "" back over it. It holds both date fields as the string they arrived as now, prints them at their own precision, and hands back anything it cannot set. Its picker still only makes full dates; a precision control there is a separate job. Separately: an empty cell of the service table had three different looks in one row. The dash under Notes was body-coloured, as though it were content; the one under File was 12px, having borrowed the size of the Download button that would otherwise be there; the one under Changed parts was muted at 14px. They are one constant now, muted at the row's own size, which is what Next date and Next km already did for a missing value. The Download link keeps its own styling - it is an action, not a value. Verified in a browser: a stored "2015-03" loads as month precision in a month picker, month to year narrows to "2015", year to day clears, "19x98abc" typed into the year box sanitises to "1998", saving sends buildDate:"1998" and the Information tab then reads "1998" - while a full first-registration date beside it still reads 06-08-2026. All five empty cells across the three columns now compute to the same size, colour and weight, with the filled ones unchanged. go vet and go test ./... pass with a new test over the three valid shapes and six rejects; flutter analyze is clean and 22 tests pass, one new, covering a half-known date in two date formats and the time zone that could shift it; npm run build is clean. Not verified: First registration still demands a full date. The same argument applies to it and the field is now a reusable component, but it was not asked for and is one line away. The web formatter's month-name paths - the DMY and MDY formats, which spell the month out - are covered only by the phone's mirror of the logic, the web app still having no test runner. A car created through the Toyota import bypasses the new validation; it only ever produces full dates, so nothing invalid gets in that way, but it is not guarded. Both apps need redeploying before any of this is visible. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c5d431c560
commit
d4033dbcef
17 files changed
+358
-28
No files matched your search
@@ -37,6 +37,34 @@ String formatDate(DateTime? d) {
|
||||
return DateFormat(pattern, _locale).format(d);
|
||||
}
|
||||
|
||||
/// A date somebody may only half know, as an ISO 8601 reduced-precision date:
|
||||
/// "2015", "2015-03" or "2015-03-10". Mirrors formatPartialDate in the web
|
||||
/// app's format.js, which is where such a value is entered — a car's build date
|
||||
/// is often only a year, and this app must not print a day nobody supplied.
|
||||
///
|
||||
/// Taken apart as a string rather than parsed: DateTime.parse rejects "2015"
|
||||
/// and "2015-03" outright, which is exactly why the value has to be handled
|
||||
/// here at all.
|
||||
String formatPartialDate(String iso) {
|
||||
final value = iso.trim();
|
||||
if (value.isEmpty) return "—";
|
||||
final match = RegExp(r"^(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?$").firstMatch(value);
|
||||
if (match == null) return formatDate(DateTime.tryParse(value));
|
||||
final year = match.group(1)!;
|
||||
final month = match.group(2);
|
||||
if (match.group(3) != null) return formatDate(DateTime.tryParse(value));
|
||||
if (month == null) return year;
|
||||
|
||||
// Month and year: the month's name where the user's format spells months out,
|
||||
// its number where the format is numeric, in the app's usual order either way.
|
||||
final when = DateTime(int.parse(year), int.parse(month));
|
||||
return switch (appSettings.dateFormat) {
|
||||
"DMY_NUM" => "$month-$year",
|
||||
"DMY" || "MDY" => DateFormat("MMM yyyy", _locale).format(when),
|
||||
_ => "$year-$month",
|
||||
};
|
||||
}
|
||||
|
||||
/// A timestamp: the user's chosen date format with the wall clock beside it.
|
||||
/// Only the provider snapshot needs one — everything else in the app is
|
||||
/// date-only — but it follows the same settings as [formatDate] so the two never
|
||||
|
||||
@@ -61,7 +61,10 @@ class Car {
|
||||
final String brakeFluidSpec;
|
||||
final String coolantSpec;
|
||||
final String fuelType; // petrol | petrol_lpg | diesel | diesel_lpg | hybrid | electric | hydrogen
|
||||
final String buildDate; // ISO YYYY-MM-DD (date-only)
|
||||
// ISO 8601 reduced precision: "2015", "2015-03" or "2015-03-10". Entered on
|
||||
// the web, which can say how much of it is known; this app's date picker only
|
||||
// makes full ones, but it reads and preserves the rest. See formatPartialDate.
|
||||
final String buildDate;
|
||||
final String firstRegistrationDate; // ISO YYYY-MM-DD (date-only)
|
||||
final int serviceIntervalDays;
|
||||
final int serviceIntervalKm;
|
||||
|
||||
@@ -727,8 +727,10 @@ class _InfoTab extends StatelessWidget {
|
||||
"registrationCountry": _orDash(car.registrationCountry),
|
||||
"vin": _orDash(car.vin),
|
||||
"fuelType": _fuelLabel(car.fuelType),
|
||||
"buildDate": _dateOrDash(car.buildDate),
|
||||
"firstRegistration": _dateOrDash(car.firstRegistrationDate),
|
||||
// Half-known dates print at their own precision: a build date is often
|
||||
// only a year, and _dateOrDash would show a dash for one.
|
||||
"buildDate": formatPartialDate(car.buildDate),
|
||||
"firstRegistration": formatPartialDate(car.firstRegistrationDate),
|
||||
};
|
||||
|
||||
@override
|
||||
@@ -787,11 +789,6 @@ class _InfoTab extends StatelessWidget {
|
||||
static String _fuelLabel(String v) =>
|
||||
kFuelTypes.contains(v) ? t("enums.fuelType.$v") : "—";
|
||||
|
||||
static String _dateOrDash(String iso) {
|
||||
final d = DateTime.tryParse(iso);
|
||||
return d == null ? "—" : formatDate(d);
|
||||
}
|
||||
|
||||
Widget _kv(BuildContext context, String k, String v) => Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 3),
|
||||
child: Row(
|
||||
|
||||
@@ -32,8 +32,14 @@ class CarFormSheet extends StatefulWidget {
|
||||
class _CarFormSheetState extends State<CarFormSheet> {
|
||||
late final Map<String, TextEditingController> _c;
|
||||
String _fuelType = "";
|
||||
DateTime? _buildDate;
|
||||
DateTime? _firstRegistrationDate;
|
||||
// Held as the ISO string they arrived as, not as a DateTime. A build date may
|
||||
// be a year or a month rather than a day — the web app can enter one, this
|
||||
// sheet's date picker cannot — and parsing it into a DateTime would hand back
|
||||
// null, which on save would round somebody's "2015" down to nothing. Picking a
|
||||
// date replaces the value; the clear button empties it; anything else is
|
||||
// handed back exactly as it came.
|
||||
String _buildDate = "";
|
||||
String _firstRegistrationDate = "";
|
||||
bool _saving = false;
|
||||
String? _error;
|
||||
|
||||
@@ -44,9 +50,8 @@ class _CarFormSheetState extends State<CarFormSheet> {
|
||||
super.initState();
|
||||
final car = widget.car;
|
||||
_fuelType = car?.fuelType ?? "";
|
||||
_buildDate = (car?.buildDate.isNotEmpty ?? false) ? DateTime.tryParse(car!.buildDate) : null;
|
||||
_firstRegistrationDate =
|
||||
(car?.firstRegistrationDate.isNotEmpty ?? false) ? DateTime.tryParse(car!.firstRegistrationDate) : null;
|
||||
_buildDate = car?.buildDate ?? "";
|
||||
_firstRegistrationDate = car?.firstRegistrationDate ?? "";
|
||||
_c = {
|
||||
"name": TextEditingController(text: car?.name ?? ""),
|
||||
"make": TextEditingController(text: car?.make ?? ""),
|
||||
@@ -98,8 +103,8 @@ class _CarFormSheetState extends State<CarFormSheet> {
|
||||
"registrationCountry": _c["registrationCountry"]!.text.trim(),
|
||||
"vin": _c["vin"]!.text.trim(),
|
||||
"fuelType": _fuelType,
|
||||
"buildDate": _isoOrEmpty(_buildDate),
|
||||
"firstRegistrationDate": _isoOrEmpty(_firstRegistrationDate),
|
||||
"buildDate": _buildDate,
|
||||
"firstRegistrationDate": _firstRegistrationDate,
|
||||
"oilSpec": _c["oilSpec"]!.text.trim(),
|
||||
"transmissionOilSpec": _c["transmissionOilSpec"]!.text.trim(),
|
||||
"differentialOilSpec": _c["differentialOilSpec"]!.text.trim(),
|
||||
@@ -149,32 +154,37 @@ class _CarFormSheetState extends State<CarFormSheet> {
|
||||
|
||||
/// A tappable read-only field that opens a date picker. Shows a clear button
|
||||
/// when a date is set, otherwise a calendar icon.
|
||||
Widget _dateField(String label, DateTime? value, ValueChanged<DateTime?> onChanged) {
|
||||
///
|
||||
/// The value is an ISO string rather than a DateTime so that a half-known date
|
||||
/// entered on the web — "2015", "2015-03" — survives a save here: it is shown
|
||||
/// at the precision it has and handed back untouched unless the owner picks a
|
||||
/// new date, which this picker can only ever make a full one.
|
||||
Widget _dateField(String label, String value, ValueChanged<String> onChanged) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 10),
|
||||
child: InkWell(
|
||||
onTap: () async {
|
||||
final picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: value ?? DateTime.now(),
|
||||
initialDate: DateTime.tryParse(value) ?? DateTime.now(),
|
||||
firstDate: DateTime(1950),
|
||||
lastDate: DateTime.now().add(const Duration(days: 365)),
|
||||
);
|
||||
if (picked != null) setState(() => onChanged(picked));
|
||||
if (picked != null) setState(() => onChanged(_isoOrEmpty(picked)));
|
||||
},
|
||||
child: InputDecorator(
|
||||
decoration: InputDecoration(
|
||||
labelText: label,
|
||||
border: const OutlineInputBorder(),
|
||||
isDense: true,
|
||||
suffixIcon: value == null
|
||||
suffixIcon: value.isEmpty
|
||||
? const Icon(Icons.calendar_today, size: 18)
|
||||
: IconButton(
|
||||
icon: const Icon(Icons.clear, size: 18),
|
||||
onPressed: () => setState(() => onChanged(null)),
|
||||
onPressed: () => setState(() => onChanged("")),
|
||||
),
|
||||
),
|
||||
child: Text(value == null ? "—" : formatDate(value)),
|
||||
child: Text(formatPartialDate(value)),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -156,6 +156,24 @@ void main() {
|
||||
appSettings.locale = "pl-PL";
|
||||
});
|
||||
|
||||
test("a half-known date prints at its own precision, not padded to a day", () {
|
||||
// What the web app can now enter for a build date. The phone's picker only
|
||||
// makes full dates, but it has to read and keep these.
|
||||
appSettings.dateFormat = "DMY_NUM";
|
||||
expect(formatPartialDate("2015"), "2015");
|
||||
expect(formatPartialDate("2015-03"), "03-2015");
|
||||
expect(formatPartialDate("2015-03-10"), "10-03-2015");
|
||||
expect(formatPartialDate(""), "—");
|
||||
|
||||
// The year and the month must not be shuffled by the local time zone, which
|
||||
// is what parsing "2015-03" as a UTC instant would risk.
|
||||
appSettings.dateFormat = "YMD";
|
||||
expect(formatPartialDate("2015-01"), "2015-01");
|
||||
expect(formatPartialDate("2015-12"), "2015-12");
|
||||
|
||||
appSettings.dateFormat = "DMY_NUM";
|
||||
});
|
||||
|
||||
test("Car carries the technical check interval", () {
|
||||
final car = Car.fromJson({"id": "c", "name": "Yaris", "technicalCheckIntervalDays": 730});
|
||||
expect(car.technicalCheckIntervalDays, 730);
|
||||
|
||||
Reference in new issue
Block a user