From f8504d466a73632d46cbe9b741b528e12869faa8 Mon Sep 17 00:00:00 2001 From: mark-cachy Date: Mon, 8 Jun 2026 14:36:31 +0200 Subject: [PATCH] fix attachments --- .../shared_forms/attachments_section.dart | 12 ++++----- .../operations/ui/operation_form_screen.dart | 27 ++++++++++++++----- pubspec.yaml | 2 +- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/core/widgets/shared_forms/attachments_section.dart b/lib/core/widgets/shared_forms/attachments_section.dart index ed9aa18..bbe8a4a 100644 --- a/lib/core/widgets/shared_forms/attachments_section.dart +++ b/lib/core/widgets/shared_forms/attachments_section.dart @@ -34,14 +34,14 @@ class SharedAttachmentsSection extends StatefulWidget { final String? parentId; final String titleForUpload; final AttachmentParentType parentType; - final Future Function()? onGenerateIdForQr; + final Future Function()? onEnsureEntitySaved; const SharedAttachmentsSection({ super.key, this.parentId, this.titleForUpload = 'Cliente_sconosciuto', required this.parentType, - this.onGenerateIdForQr, + this.onEnsureEntitySaved, }); @override @@ -95,7 +95,7 @@ class _SharedAttachmentsSectionState extends State { // 🥷 SE L'ID NON C'È (Nuova Operazione), FORZIAMO IL SALVATAGGIO PREVENTIVO! if (targetId == null || targetId.isEmpty) { - if (widget.onGenerateIdForQr != null) { + if (widget.onEnsureEntitySaved != null) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Salvataggio rapido scheda per allegati... ⏳'), @@ -104,7 +104,7 @@ class _SharedAttachmentsSectionState extends State { ); // Chiamiamo la funzione passata dal TicketForm/OperationForm - targetId = await widget.onGenerateIdForQr!(); + targetId = await widget.onEnsureEntitySaved!(); } // Se il salvataggio fallisce (es. form non valido), ci fermiamo per evitare file orfani @@ -507,7 +507,7 @@ class _SharedAttachmentsSectionState extends State { // SE L'ID NON C'È, CHIAMIAMO IL SALVATAGGIO IN BACKGROUND! if (targetId == null) { - if (widget.onGenerateIdForQr != null) { + if (widget.onEnsureEntitySaved != null) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text( @@ -518,7 +518,7 @@ class _SharedAttachmentsSectionState extends State { ); // Aspettiamo che il TicketFormCubit faccia il suo lavoro - targetId = await widget.onGenerateIdForQr!(); + targetId = await widget.onEnsureEntitySaved!(); } // Se fallisce (es. validazione form non passata), ci fermiamo diff --git a/lib/features/operations/ui/operation_form_screen.dart b/lib/features/operations/ui/operation_form_screen.dart index b82fce4..5dc66a0 100644 --- a/lib/features/operations/ui/operation_form_screen.dart +++ b/lib/features/operations/ui/operation_form_screen.dart @@ -26,6 +26,9 @@ class OperationFormScreen extends StatefulWidget { class _OperationFormScreenState extends State { final _formKey = GlobalKey(); + // 🥷 IL NUOVO FLAG: Ci ricorderà se vogliamo davvero uscire! + bool _isClosingIntent = false; + final _referenceController = TextEditingController(); final _noteController = TextEditingController(); final _freeTextSubtypeController = TextEditingController(); @@ -101,10 +104,11 @@ class _OperationFormScreenState extends State { required bool keepAdding, }) { if (_formKey.currentState!.validate()) { + // Se non stiamo facendo "Salva e Aggiungi Altro", il nostro intento è chiudere + _isClosingIntent = !keepAdding; + _flushControllersToCubit(); - // Aggiorniamo prima lo stato bersaglio nel cubit context.read().updateFields(status: targetStatus); - // Poi chiamiamo il salvataggio context.read().saveOperation( targetStatus: targetStatus, keepAdding: keepAdding, @@ -112,11 +116,15 @@ class _OperationFormScreenState extends State { } } - Future _generateIdForQr() async { + // RINOMINATA E RESA SICURA + Future _ensureEntitySaved() async { if (!_formKey.currentState!.validate()) return null; + + // 🥷 Diciamo esplicitamente al sistema che NON vogliamo uscire dalla pagina + _isClosingIntent = false; + _flushControllersToCubit(); - // Lo leggiamo pulito pulito dal context, perché c'è! final attachmentsBloc = context.read(); final newId = await context.read().saveOperationDraft(); @@ -151,8 +159,15 @@ class _OperationFormScreenState extends State { if (state.status == OperationFormStatus.ready && !_isInitialized) { _syncTextControllers(state.operation); } + + // 🥷 ORA POPPA SOLO SE L'INTENTO ERA QUELLO DI USCIRE! if (state.status == OperationFormStatus.success) { - Navigator.of(context).pop(); + if (_isClosingIntent) { + Navigator.of(context).pop(); + } else { + // È stato un salvataggio background (Pick Files o QR). + // Non facciamo nulla, l'utente resta sulla pagina e i file vengono caricati! + } } else if (state.status == OperationFormStatus.successAndAddAnother) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( @@ -626,7 +641,7 @@ class _OperationFormScreenState extends State { parentType: AttachmentParentType.operation, parentId: state.operation.id, titleForUpload: state.operation.customer?.name ?? 'Nuova Pratica', - onGenerateIdForQr: _generateIdForQr, + onEnsureEntitySaved: _ensureEntitySaved, // 🥷 Il nuovo nome! ), ], ); diff --git a/pubspec.yaml b/pubspec.yaml index 6fc868d..ec205b3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: flux description: "Gestione attività negozio di telefonia" publish_to: 'none' -version: 1.1.21+39 +version: 1.1.22+40 environment: sdk: ^3.11.3