The previous commit left the car screen half translated: its tab labels went
through t(), and everything underneath them did not. A Polish user opening a
car got translated tabs over English tiles, English forms and English
dialogs, which is worse than either extreme because it reads as a bug rather
than as a missing translation.
So the whole screen and everything it opens now reads from the language
files: the record tiles, the share and delete-car dialogs, the service and
part sheets it hosts, record_form_sheets.dart, car_form_sheet.dart, and the
attachment field whose buttons surface inside all of them.
Almost none of these strings are new. The Web App has said all of this in
three languages since b6bb6b1, so forms.*, enums.*, attachment.* and errors.*
are copied out of its language files the same way car.* was, and Polish and
Danish arrive complete. What is written here is only what the phone alone
needs, and the categories are worth naming because they are the reason the
two apps' files are not identical: tooltips, because the web labels its
buttons; the tiles' running prose, because the web lays the same data out as
table columns; client-side validation, because the web leans on the browser's
`required`; and the snackbars.
Three things changed shape rather than just wording.
The per-record delete prompts were one template with a noun slotted in -
"Delete this $what?" - which does not survive translation into a language
that inflects the noun. Each collection now names its own confirmation
string, which is what the web already had.
The delete-car dialog counted with a hand-rolled `"$n $noun${n == 1 ? '' :
's'}"`. Polish has three plural forms, so that could not be translated at
all; it now goes through the CLDR plurals in car.delete.*. It also only ever
named service records and parts, while the cascade takes maintenance, fuel,
charges and documents too - the translated body names all six, so it is now
passed the whole data set rather than two counts.
The enum labels (fuel types, maintenance type/status, document and reminder
types) were four const maps duplicated between the tiles and the pickers.
They are one lookup against enums.* now, with an unknown value falling back
to the raw key rather than a blank - the server owns that enum, and a value
added there should stay legible in an app that has not caught up.
Found and fixed while testing: the view picker rendered the literal string
"car.tabs.provider" as a row label on an unlinked car. That key does not
exist by design - a linked car's tab is named after the service, an unlinked
one falls back to car.tabs.connected - and the picker was the one caller that
did not know it.
Verified by flutter analyze (clean), flutter test - 19 pass, 7 of them new -
and flutter build apk --debug. The new tests cover what the analyzer cannot
see: the lookups built from a key at render time (car.tabs.$key,
enums.fuelType.$v, the delete dialog's plural counts, the connected service's
readings) are checked to have a real label in all three languages, so a
catalogue entry with no translation fails a test instead of reaching a screen
as a raw key path. That is the check that caught the bug above. A one-off
script also confirmed all 550 static t() keys resolve in en.json.
Not verified: still nothing run against a live API Server or on a device.
Known gaps, deliberately left: admin_users_screen.dart is still English, and
settings.integrations.* / charging.control.* exist in en.json only. The
second one is not the phone's alone - the Web App has exactly the same gap,
so translating that OCPP and connector vocabulary belongs to both apps in one
pass rather than letting the phone run ahead of the app the strings are
copied from. Both are now recorded in TRANSLATIONS.md, which had claimed the
car screen as untranslated and the web app as complete.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
250 lines
8.5 KiB
Dart
250 lines
8.5 KiB
Dart
import "dart:io";
|
|
|
|
import "package:file_picker/file_picker.dart";
|
|
import "package:flutter/material.dart";
|
|
import "package:open_filex/open_filex.dart";
|
|
import "package:path_provider/path_provider.dart";
|
|
|
|
import "../api.dart";
|
|
import "../i18n.dart";
|
|
import "../main.dart";
|
|
import "../models.dart";
|
|
import "../theme.dart";
|
|
|
|
/// The extensions an attachment may carry, mirroring attachmentFileTypes in the
|
|
/// API's attachments.go. The list is restrictive on purpose: these are scans and
|
|
/// photos, and the server rejects anything else.
|
|
const _allowedExtensions = ["pdf", "jpg", "jpeg", "png", "webp", "heic"];
|
|
|
|
/// A form's pending attachment change: a newly picked file, or a request to
|
|
/// detach whatever is already there. Both empty means "leave it alone".
|
|
class PendingAttachment {
|
|
PlatformFile? file;
|
|
bool remove = false;
|
|
|
|
bool get isEmpty => file == null && !remove;
|
|
}
|
|
|
|
/// Applies a form's pending attachment change to the record it has just saved.
|
|
///
|
|
/// This necessarily runs after the metadata write: the file endpoints address a
|
|
/// record that must already exist. The order means a create-with-file is two
|
|
/// calls, and the second one failing leaves a saved record with no attachment —
|
|
/// which is why callers report it as an attachment error rather than a failed
|
|
/// save, because the metadata is already committed.
|
|
Future<void> applyAttachment(String path, String id, PendingAttachment pending) async {
|
|
final picked = pending.file;
|
|
if (picked != null) {
|
|
final bytes = picked.bytes ??
|
|
(picked.path != null ? await File(picked.path!).readAsBytes() : null);
|
|
if (bytes == null) throw ApiException(0, t("errors.noFile"));
|
|
await apiClient.uploadAttachment(path, id, bytes, picked.name);
|
|
return;
|
|
}
|
|
if (pending.remove) await apiClient.deleteAttachment(path, id);
|
|
}
|
|
|
|
/// Downloads a record's attachment and hands it to the phone's viewer for that
|
|
/// file type. The bytes are fetched through the API Server (never a public URL),
|
|
/// then cached to a temp file because the OS viewers open paths, not buffers.
|
|
Future<void> openAttachment(
|
|
BuildContext context,
|
|
String path,
|
|
String id,
|
|
String fileName,
|
|
) async {
|
|
final messenger = ScaffoldMessenger.of(context);
|
|
try {
|
|
final bytes = await apiClient.getAttachmentBytes(path, id);
|
|
if (bytes == null) {
|
|
messenger.showSnackBar(SnackBar(content: Text(t("errors.noFile"))));
|
|
return;
|
|
}
|
|
final dir = await getTemporaryDirectory();
|
|
final safe = fileName.isEmpty ? "attachment" : fileName.split(RegExp(r"[\\/]")).last;
|
|
final f = File("${dir.path}/$safe");
|
|
await f.writeAsBytes(bytes);
|
|
final res = await OpenFilex.open(f.path);
|
|
if (res.type != ResultType.done) {
|
|
messenger.showSnackBar(SnackBar(
|
|
content: Text(t("errors.openFailed", params: {"error": res.message}))));
|
|
}
|
|
} catch (e) {
|
|
messenger.showSnackBar(
|
|
SnackBar(content: Text(t("errors.openFailed", params: {"error": e}))));
|
|
}
|
|
}
|
|
|
|
/// The attachment picker shared by every form that can carry a file.
|
|
///
|
|
/// It only collects intent into [pending] — actually moving the bytes is the
|
|
/// parent's job (see [applyAttachment]), because the endpoint addresses a record
|
|
/// that must already exist.
|
|
class AttachmentField extends StatefulWidget {
|
|
/// The saved record, when editing; null while creating. Read for the name of
|
|
/// whatever is already attached.
|
|
final HasAttachment? record;
|
|
|
|
/// The collection's API path (e.g. "/service-records"), used to fetch an
|
|
/// existing attachment for viewing.
|
|
final String path;
|
|
|
|
/// The saved record's id; null while creating (nothing to view yet).
|
|
final String? recordId;
|
|
|
|
final PendingAttachment pending;
|
|
final VoidCallback onChanged;
|
|
/// Blank means the shared wording (attachment.legend / attachment.hint);
|
|
/// callers with a more specific one — "Receipt", "Photo or spec sheet" —
|
|
/// pass it translated.
|
|
final String legend;
|
|
final String hint;
|
|
|
|
const AttachmentField({
|
|
super.key,
|
|
required this.path,
|
|
required this.pending,
|
|
required this.onChanged,
|
|
this.record,
|
|
this.recordId,
|
|
this.legend = "",
|
|
this.hint = "",
|
|
});
|
|
|
|
@override
|
|
State<AttachmentField> createState() => _AttachmentFieldState();
|
|
}
|
|
|
|
class _AttachmentFieldState extends State<AttachmentField> {
|
|
Future<void> _pick() async {
|
|
final res = await FilePicker.platform.pickFiles(
|
|
type: FileType.custom,
|
|
allowedExtensions: _allowedExtensions,
|
|
withData: true,
|
|
);
|
|
if (res == null || res.files.isEmpty) return;
|
|
setState(() {
|
|
widget.pending.file = res.files.first;
|
|
// Picking a replacement supersedes a pending detach.
|
|
widget.pending.remove = false;
|
|
});
|
|
widget.onChanged();
|
|
}
|
|
|
|
void _setRemove(bool v) {
|
|
setState(() {
|
|
widget.pending.remove = v;
|
|
if (v) widget.pending.file = null;
|
|
});
|
|
widget.onChanged();
|
|
}
|
|
|
|
void _clearPick() {
|
|
setState(() => widget.pending.file = null);
|
|
widget.onChanged();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
final muted = theme.textTheme.bodySmall?.color;
|
|
final picked = widget.pending.file;
|
|
final existing = widget.record;
|
|
final hasExisting = existing?.hasFile == true;
|
|
|
|
return InputDecorator(
|
|
decoration: InputDecoration(
|
|
labelText: widget.legend.isEmpty ? t("attachment.legend") : widget.legend,
|
|
border: const OutlineInputBorder(),
|
|
helperText: widget.hint.isEmpty ? t("attachment.hint") : widget.hint,
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
OutlinedButton.icon(
|
|
onPressed: _pick,
|
|
icon: const Icon(Icons.attach_file, size: 18),
|
|
label: Text(
|
|
t(hasExisting || picked != null ? "attachment.replace" : "attachment.choose")),
|
|
),
|
|
const SizedBox(width: 8),
|
|
if (picked != null)
|
|
Expanded(
|
|
child: Text(
|
|
picked.name,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: theme.textTheme.bodySmall,
|
|
),
|
|
),
|
|
if (picked != null)
|
|
IconButton(
|
|
onPressed: _clearPick,
|
|
icon: const Icon(Icons.close, size: 18),
|
|
tooltip: t("attachment.clear"),
|
|
),
|
|
],
|
|
),
|
|
if (picked == null && hasExisting && !widget.pending.remove)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 6),
|
|
child: Wrap(
|
|
crossAxisAlignment: WrapCrossAlignment.center,
|
|
spacing: 8,
|
|
children: [
|
|
Text(t("attachment.attached", params: {"name": existing!.fileName}),
|
|
style: theme.textTheme.bodySmall),
|
|
if (widget.recordId != null)
|
|
_LinkButton(
|
|
label: t("attachment.view"),
|
|
onPressed: () => openAttachment(
|
|
context, widget.path, widget.recordId!, existing.fileName),
|
|
),
|
|
_LinkButton(
|
|
label: t("common.remove"),
|
|
color: DriverVault.danger,
|
|
onPressed: () => _setRemove(true),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
if (widget.pending.remove)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 6),
|
|
child: Wrap(
|
|
crossAxisAlignment: WrapCrossAlignment.center,
|
|
spacing: 8,
|
|
children: [
|
|
Text(t("attachment.willBeRemoved"),
|
|
style: theme.textTheme.bodySmall?.copyWith(color: muted)),
|
|
_LinkButton(label: t("common.undo"), onPressed: () => _setRemove(false)),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _LinkButton extends StatelessWidget {
|
|
final String label;
|
|
final Color? color;
|
|
final VoidCallback onPressed;
|
|
const _LinkButton({required this.label, required this.onPressed, this.color});
|
|
|
|
@override
|
|
Widget build(BuildContext context) => InkWell(
|
|
onTap: onPressed,
|
|
child: Text(
|
|
label,
|
|
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
|
color: color ?? DriverVault.brandOnTint(context),
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
),
|
|
);
|
|
}
|