diff --git a/lib/features/tasks/presentation/widgets/assign_users_dialog.dart b/lib/features/tasks/presentation/widgets/assign_users_dialog.dart index c6054de..5c538d1 100644 --- a/lib/features/tasks/presentation/widgets/assign_users_dialog.dart +++ b/lib/features/tasks/presentation/widgets/assign_users_dialog.dart @@ -27,9 +27,16 @@ class _AssignUsersDialogState extends State late Animation _fadeAnimation; late Animation _slideAnimation; + // Sauvegarder l'état initial pour permettre l'annulation + late List _initialAssignedUsers; + @override void initState() { super.initState(); + + // Sauvegarder l'état initial des assignations + _initialAssignedUsers = List.from(widget.task.assignedTo); + _loadUsers(); // Animations d'entrée @@ -96,12 +103,81 @@ class _AssignUsersDialogState extends State }); } + /// Annule toutes les modifications et restaure l'état initial + Future _cancelChanges() async { + try { + final provider = context.read(); + final currentTask = provider.allTasks.firstWhere( + (t) => t.id == widget.task.id, + ); + final currentAssignedUsers = currentTask.assignedTo; + + // Trouver les utilisateurs à retirer (qui ont été ajoutés) + final usersToRemove = currentAssignedUsers + .where((userId) => !_initialAssignedUsers.contains(userId)) + .toList(); + + // Trouver les utilisateurs à rajouter (qui ont été retirés) + final usersToAdd = _initialAssignedUsers + .where((userId) => !currentAssignedUsers.contains(userId)) + .toList(); + + // Effectuer les modifications pour restaurer l'état initial + for (final userId in usersToRemove) { + await provider.unassignUserFromTask(widget.task.id, userId); + } + + for (final userId in usersToAdd) { + await provider.assignUserToTask(widget.task.id, userId); + } + + if (mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Row( + children: [ + const Icon(Icons.undo, color: Colors.white), + const SizedBox(width: 12), + const Expanded( + child: Text( + 'Modifications annulées', + style: TextStyle(fontWeight: FontWeight.w600), + ), + ), + ], + ), + backgroundColor: AppColors.getOnSurface(context).withOpacity(0.8), + duration: const Duration(seconds: 2), + behavior: SnackBarBehavior.floating, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(12), + ), + ), + ); + + // Fermer le dialog après avoir annulé + Navigator.of(context).pop(); + } + } catch (e) { + if (mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text('Erreur lors de l\'annulation: $e'), + backgroundColor: AppColors.error, + ), + ); + } + } + } + Future _toggleUserAssignment(String userId, bool isAssigned) async { try { final provider = context.read(); if (isAssigned) { + // L'utilisateur EST déjà assigné → on le RETIRE await provider.unassignUserFromTask(widget.task.id, userId); } else { + // L'utilisateur N'EST PAS assigné → on l'AJOUTE await provider.assignUserToTask(widget.task.id, userId); } @@ -562,6 +638,17 @@ class _AssignUsersDialogState extends State ), ), ), + // Bouton Annuler - Restaure l'état initial + TextButton( + onPressed: _cancelChanges, + style: TextButton.styleFrom( + foregroundColor: AppColors.getOnSurface(context).withOpacity(0.7), + padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12), + ), + child: const Text('Annuler'), + ), + const SizedBox(width: 8), + // Bouton Modifier - Ferme le dialog (changements déjà appliqués) ElevatedButton( onPressed: () => Navigator.of(context).pop(), style: ElevatedButton.styleFrom( @@ -572,7 +659,7 @@ class _AssignUsersDialogState extends State borderRadius: BorderRadius.circular(12), ), ), - child: const Text('Terminé'), + child: const Text('Modifier'), ), ], ), diff --git a/lib/features/tasks/presentation/widgets/task_modal.dart b/lib/features/tasks/presentation/widgets/task_modal.dart index bf925e9..683f336 100644 --- a/lib/features/tasks/presentation/widgets/task_modal.dart +++ b/lib/features/tasks/presentation/widgets/task_modal.dart @@ -99,8 +99,14 @@ class _TaskModalState extends State { } if (_isEditing) { - // Modifier la tâche existante - final updatedTask = widget.task!.copyWith( + // Récupérer la tâche actuelle depuis le provider pour avoir les assignations à jour + final currentTask = taskProvider.allTasks.firstWhere( + (t) => t.id == widget.task!.id, + orElse: () => widget.task!, + ); + + // Modifier la tâche existante en gardant les assignations actuelles + final updatedTask = currentTask.copyWith( title: _titleController.text.trim(), description: _descriptionController.text.trim(), priority: _selectedPriority, @@ -108,6 +114,7 @@ class _TaskModalState extends State { tags: _selectedTags, ); print('📝 TaskModal - Modification tâche: ${updatedTask.id}'); + print(' - assignedTo conservé: ${updatedTask.assignedTo}'); taskProvider.updateTask(updatedTask); } else { // Créer une nouvelle tâche avec ownerId et ownerName