From bc1614c71bcf281722818504666cc8746b5f6150 Mon Sep 17 00:00:00 2001 From: mark-fedora Date: Wed, 19 Aug 2026 12:42:35 +0200 Subject: [PATCH] fix attachments --- lib/core/widgets/qr_upload_dialog.dart | 173 +++++++++++------- .../shared_forms/attachments_section.dart | 61 ++++-- .../shared_forms/shared_files_section.dart | 11 +- .../operations/ui/operation_form_screen.dart | 18 ++ pubspec.yaml | 2 +- 5 files changed, 177 insertions(+), 88 deletions(-) diff --git a/lib/core/widgets/qr_upload_dialog.dart b/lib/core/widgets/qr_upload_dialog.dart index eb5a199..e397a29 100644 --- a/lib/core/widgets/qr_upload_dialog.dart +++ b/lib/core/widgets/qr_upload_dialog.dart @@ -19,84 +19,119 @@ class QrUploadDialog extends StatelessWidget { // Usiamo i colori del tema per renderlo coerente col tuo design final theme = Theme.of(context); - return BlocListener( - listener: (context, state) { - Navigator.of(context).pop(); - }, + return BlocConsumer( listenWhen: (previous, current) => previous.allFiles.length < current.allFiles.length, - child: AlertDialog( - shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)), - backgroundColor: theme.colorScheme.surface, - title: Column( - children: [ - Icon( - Icons.qr_code_scanner, - size: 48, - color: theme.colorScheme.primary, + listener: (context, state) { + final newCount = state.allFiles.length; + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + 'File caricato con successo via QR Code! 📄 ($newCount allegati in totale)', ), - const SizedBox(height: 16), - Text( - title, - textAlign: TextAlign.center, - style: const TextStyle(fontWeight: FontWeight.bold), - ), - ], - ), - content: SizedBox( - height: 400, - width: 350, - child: SingleChildScrollView( - child: Column( - mainAxisSize: MainAxisSize.min, // Fondamentale per i dialog - children: [ - const Text( - "Inquadra questo codice con la fotocamera del tuo telefono per scattare e caricare i documenti direttamente qui.", - textAlign: TextAlign.center, - style: TextStyle(fontSize: 14, color: Colors.grey), - ), - const SizedBox(height: 24), - Container( - padding: const EdgeInsets.all(16), - decoration: BoxDecoration( - color: Colors - .white, // Lo sfondo bianco salva la vita sui temi scuri - borderRadius: BorderRadius.circular(16), + duration: const Duration(seconds: 2), + backgroundColor: Colors.green.shade700, + ), + ); + }, + builder: (context, state) { + final fileCount = state.allFiles.length; + + return AlertDialog( + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(20), + ), + backgroundColor: theme.colorScheme.surface, + title: Column( + children: [ + Icon( + Icons.qr_code_scanner, + size: 48, + color: theme.colorScheme.primary, + ), + const SizedBox(height: 16), + Text( + title, + textAlign: TextAlign.center, + style: const TextStyle(fontWeight: FontWeight.bold), + ), + ], + ), + content: SizedBox( + height: 400, + width: 350, + child: SingleChildScrollView( + child: Column( + mainAxisSize: MainAxisSize.min, // Fondamentale per i dialog + children: [ + const Text( + "Inquadra questo codice con la fotocamera del tuo telefono per scattare e caricare i documenti direttamente qui.", + textAlign: TextAlign.center, + style: TextStyle(fontSize: 14, color: Colors.grey), ), - child: QrImageView( - data: deepLinkUrl, - version: QrVersions.auto, - size: 200.0, - //Opzionale: puoi metterci il logo di FLUX in mezzo! - embeddedImage: const AssetImage('assets/images/logo.png'), - embeddedImageStyle: const QrEmbeddedImageStyle( - size: Size(40, 40), + const SizedBox(height: 24), + Container( + padding: const EdgeInsets.all(16), + decoration: BoxDecoration( + color: Colors + .white, // Lo sfondo bianco salva la vita sui temi scuri + borderRadius: BorderRadius.circular(16), + ), + child: QrImageView( + data: deepLinkUrl, + version: QrVersions.auto, + size: 200.0, + embeddedImage: const AssetImage( + 'assets/images/logo.png', + ), + embeddedImageStyle: const QrEmbeddedImageStyle( + size: Size(40, 40), + ), ), ), - ), - const SizedBox(height: 16), - Text( - "In attesa di file...", - style: TextStyle( - fontSize: 12, - fontWeight: FontWeight.bold, - color: theme.colorScheme.primary, + const SizedBox(height: 16), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Icon( + fileCount > 0 + ? Icons.check_circle_outline + : Icons.hourglass_empty, + size: 18, + color: fileCount > 0 + ? Colors.green + : theme.colorScheme.primary, + ), + const SizedBox(width: 8), + Text( + fileCount > 0 + ? "$fileCount file ricevuti! (In ascolto...)" + : "In attesa di file...", + style: TextStyle( + fontSize: 13, + fontWeight: FontWeight.bold, + color: fileCount > 0 + ? Colors.green + : theme.colorScheme.primary, + ), + ), + ], ), - ), - const SizedBox(height: 8), - const LinearProgressIndicator(), // Per far capire che è "in ascolto" - ], + const SizedBox(height: 8), + const LinearProgressIndicator(), // Per far capire che è "in ascolto" + ], + ), ), ), - ), - actions: [ - TextButton( - onPressed: () => Navigator.of(context).pop(), - child: Text(context.l10n.commonClose), - ), - ], - actionsAlignment: MainAxisAlignment.center, - ), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(), + child: Text(context.l10n.commonClose), + ), + ], + actionsAlignment: MainAxisAlignment.center, + ); + }, ); } } diff --git a/lib/core/widgets/shared_forms/attachments_section.dart b/lib/core/widgets/shared_forms/attachments_section.dart index 63ba0ea..f03e1e4 100644 --- a/lib/core/widgets/shared_forms/attachments_section.dart +++ b/lib/core/widgets/shared_forms/attachments_section.dart @@ -56,6 +56,24 @@ class _SharedAttachmentsSectionState extends State { void initState() { super.initState(); _loadExportDirectory(); + _syncParentIdIfNeeded(); + } + + @override + void didUpdateWidget(covariant SharedAttachmentsSection oldWidget) { + super.didUpdateWidget(oldWidget); + if (widget.parentId != oldWidget.parentId) { + _syncParentIdIfNeeded(); + } + } + + void _syncParentIdIfNeeded() { + if (widget.parentId != null && widget.parentId!.isNotEmpty) { + final attachmentsBloc = context.read(); + if (attachmentsBloc.state.parentId != widget.parentId) { + attachmentsBloc.add(ParentEntitySavedEvent(widget.parentId!)); + } + } } // --- GESTIONE CARTELLA CITRIX (SOLO DESKTOP) --- @@ -91,9 +109,9 @@ class _SharedAttachmentsSectionState extends State { // --- SELEZIONE FILE DAL PC/TELEFONO --- Future _pickFiles() async { final attachmentsBloc = context.read(); - String? targetId = attachmentsBloc.state.parentId; + String? targetId = attachmentsBloc.state.parentId ?? widget.parentId; - // 🥷 SE L'ID NON C'È (Nuova Operazione), FORZIAMO IL SALVATAGGIO PREVENTIVO! + // 🥷 SE L'ID NON C'È (Nuova Operazione), PROVIAMO IL SALVATAGGIO PREVENTIVO if (targetId == null || targetId.isEmpty) { if (widget.onEnsureEntitySaved != null) { ScaffoldMessenger.of(context).showSnackBar( @@ -103,15 +121,13 @@ class _SharedAttachmentsSectionState extends State { ), ); - // Chiamiamo la funzione passata dal TicketForm/OperationForm targetId = await widget.onEnsureEntitySaved!(); } + } - // Se il salvataggio fallisce (es. form non valido), ci fermiamo per evitare file orfani - if (targetId == null || targetId.isEmpty) return; - - // Comunichiamo immediatamente al BLoC che l'entità padre è stata salvata e ha un nuovo ID. - // Questo eviterà che i file finiscano nei `localFiles` temporanei. + if (targetId != null && + targetId.isNotEmpty && + attachmentsBloc.state.parentId != targetId) { attachmentsBloc.add(ParentEntitySavedEvent(targetId)); } @@ -640,10 +656,10 @@ class _SharedAttachmentsSectionState extends State { icon: const Icon(Icons.qr_code_scanner), color: theme.colorScheme.primary, // Sempre colorato! onPressed: () async { - String? targetId = state.parentId; + String? targetId = state.parentId ?? widget.parentId; // SE L'ID NON C'È, CHIAMIAMO IL SALVATAGGIO IN BACKGROUND! - if (targetId == null) { + if (targetId == null || targetId.isEmpty) { if (widget.onEnsureEntitySaved != null) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( @@ -654,12 +670,29 @@ class _SharedAttachmentsSectionState extends State { ), ); - // Aspettiamo che il TicketFormCubit faccia il suo lavoro targetId = await widget.onEnsureEntitySaved!(); } + } - // Se fallisce (es. validazione form non passata), ci fermiamo - if (targetId == null) return; + if (targetId == null || targetId.isEmpty) { + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text( + 'Salva la scheda prima di generare il QR Code per gli allegati.', + ), + backgroundColor: Colors.orange, + ), + ); + } + return; + } + + if (!context.mounted) return; + + final attachmentBloc = context.read(); + if (attachmentBloc.state.parentId != targetId) { + attachmentBloc.add(ParentEntitySavedEvent(targetId)); } // GENERAZIONE DEL DEEP LINK AGNOSTICO @@ -672,8 +705,6 @@ class _SharedAttachmentsSectionState extends State { 'https://flux.catelli.it/upload/${state.parentType.name}/$targetId?companyId=$companyId'; if (context.mounted) { - final attachmentBloc = context.read(); - showDialog( context: context, builder: (_) => BlocProvider.value( diff --git a/lib/core/widgets/shared_forms/shared_files_section.dart b/lib/core/widgets/shared_forms/shared_files_section.dart index 7d60642..6c3b7ba 100644 --- a/lib/core/widgets/shared_forms/shared_files_section.dart +++ b/lib/core/widgets/shared_forms/shared_files_section.dart @@ -76,11 +76,16 @@ class SharedFilesSection extends StatelessWidget { 'https://flux.catelli.it/upload/${state.parentType.name}/$targetId?companyId=$companyId'; if (context.mounted) { + final attachmentBloc = + context.read(); showDialog( context: context, - builder: (_) => QrUploadDialog( - deepLinkUrl: deepLink, - title: 'Carica File: $titleNameForUpload', + builder: (_) => BlocProvider.value( + value: attachmentBloc, + child: QrUploadDialog( + deepLinkUrl: deepLink, + title: 'Carica File: $titleNameForUpload', + ), ), ); } diff --git a/lib/features/operations/ui/operation_form_screen.dart b/lib/features/operations/ui/operation_form_screen.dart index 63fe4ad..9cbecc5 100644 --- a/lib/features/operations/ui/operation_form_screen.dart +++ b/lib/features/operations/ui/operation_form_screen.dart @@ -171,6 +171,15 @@ class _OperationFormScreenState extends State { !_isInitialized) { _syncTextControllers(formState.operation); } + if (formState.operation.id != null && + formState.operation.id!.isNotEmpty) { + final attachmentsBloc = formContext.read(); + if (attachmentsBloc.state.parentId != formState.operation.id) { + attachmentsBloc.add( + ParentEntitySavedEvent(formState.operation.id!), + ); + } + } if ((formState.status == OperationFormStatus.success && _isClosingIntent) || formState.status == OperationFormStatus.discarded) { @@ -612,6 +621,15 @@ class _OperationFormScreenState extends State { parentType: AttachmentParentType.operation, parentId: state.operation.id, titleForUpload: state.operation.customer?.name ?? 'Nuova Pratica', + onEnsureEntitySaved: () async { + if (!_formKey.currentState!.validate()) return null; + _flushControllersToCubit(); + final cubit = context.read(); + await cubit.saveOperation( + targetStatus: state.operation.status, + ); + return cubit.state.operation.id; + }, ), ], ); diff --git a/pubspec.yaml b/pubspec.yaml index 0c92fbf..05e6c4d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: flux description: "Gestione attività negozio di telefonia" publish_to: 'none' -version: 1.3.6+52 +version: 1.3.7+53 environment: sdk: ^3.11.3