feat: Intégration d'un assistant IA logisticien basé sur Gemini
- Ajout d'une Cloud Function `aiEquipmentProposal` utilisant le modèle Gemini avec function calling pour suggérer du matériel et des containers. - Implémentation de plusieurs outils (tools) côté serveur pour permettre à l'IA d'interagir avec Firestore : `search_equipment`, `check_availability_batch`, `get_past_events`, `search_event_reference` et `search_containers`. - Ajout de la dépendance `@google/generative-ai` dans le backend. - Création d'un service Flutter `AiEquipmentAssistantService` pour communiquer avec la nouvelle Cloud Function. - Ajout d'une interface de dialogue `AiEquipmentAssistantDialog` permettant aux utilisateurs de discuter avec l'IA pour affiner les propositions de matériel. - Intégration de l'assistant IA dans la section de gestion du matériel des événements (`EventAssignedEquipmentSection`). - Mise à jour de `DataService` avec de nouvelles méthodes de recherche et de vérification de disponibilité optimisées pour l'assistant. - Activation du mode développement et configuration des identifiants de test dans `env.dart`. - Optimisation des paramètres de la Cloud Function (timeout de 300s et 1GiB de RAM) pour supporter les traitements IA.
This commit is contained in:
@@ -77,7 +77,8 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
return;
|
||||
}
|
||||
|
||||
final success = await _controller.submitForm(context, existingEvent: widget.event);
|
||||
final success =
|
||||
await _controller.submitForm(context, existingEvent: widget.event);
|
||||
if (success && mounted) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
@@ -158,21 +159,25 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
},
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(isEditMode ? 'Modifier un événement' : 'Créer un événement'),
|
||||
title: Text(
|
||||
isEditMode ? 'Modifier un événement' : 'Créer un événement'),
|
||||
),
|
||||
body: Center(
|
||||
child: SingleChildScrollView(
|
||||
child: (isMobile
|
||||
? Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16, vertical: 12),
|
||||
child: _buildFormContent(isMobile),
|
||||
)
|
||||
: Card(
|
||||
elevation: 6,
|
||||
margin: const EdgeInsets.all(24),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18)),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(18)),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 32),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 32, vertical: 32),
|
||||
child: _buildFormContent(isMobile),
|
||||
),
|
||||
)),
|
||||
@@ -186,15 +191,6 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
Widget _buildFormContent(bool isMobile) {
|
||||
return Consumer<EventFormController>(
|
||||
builder: (context, controller, child) {
|
||||
// Trouver le nom du type d'événement pour le passer au sélecteur d'options
|
||||
final selectedEventTypeIndex = controller.selectedEventTypeId != null
|
||||
? controller.eventTypes.indexWhere((et) => et.id == controller.selectedEventTypeId)
|
||||
: -1;
|
||||
final selectedEventType = selectedEventTypeIndex != -1
|
||||
? controller.eventTypes[selectedEventTypeIndex]
|
||||
: null;
|
||||
final selectedEventTypeName = selectedEventType?.name;
|
||||
|
||||
return Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
@@ -209,18 +205,22 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
selectedEventTypeId: controller.selectedEventTypeId,
|
||||
startDateTime: controller.startDateTime,
|
||||
endDateTime: controller.endDateTime,
|
||||
onEventTypeChanged: (typeId) => controller.onEventTypeChanged(typeId, context),
|
||||
onEventTypeChanged: (typeId) =>
|
||||
controller.onEventTypeChanged(typeId, context),
|
||||
onStartDateTimeChanged: controller.setStartDateTime,
|
||||
onEndDateTimeChanged: controller.setEndDateTime,
|
||||
onAnyFieldChanged: () {}, // Géré automatiquement par le contrôleur
|
||||
onAnyFieldChanged:
|
||||
() {}, // Géré automatiquement par le contrôleur
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
OptionSelectorWidget(
|
||||
eventType: controller.selectedEventTypeId, // Utilise l'ID au lieu du nom
|
||||
eventType: controller
|
||||
.selectedEventTypeId, // Utilise l'ID au lieu du nom
|
||||
selectedOptions: controller.selectedOptions,
|
||||
onChanged: controller.setSelectedOptions,
|
||||
onRemove: (optionId) {
|
||||
final newOptions = List<Map<String, dynamic>>.from(controller.selectedOptions);
|
||||
final newOptions = List<Map<String, dynamic>>.from(
|
||||
controller.selectedOptions);
|
||||
newOptions.removeWhere((o) => o['id'] == optionId);
|
||||
controller.setSelectedOptions(newOptions);
|
||||
},
|
||||
@@ -236,6 +236,7 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
endDate: controller.endDateTime,
|
||||
onChanged: controller.setAssignedEquipment,
|
||||
eventId: widget.event?.id,
|
||||
eventTypeId: controller.selectedEventTypeId,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
EventDetailsSection(
|
||||
@@ -247,7 +248,8 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
contactEmailController: controller.contactEmailController,
|
||||
contactPhoneController: controller.contactPhoneController,
|
||||
isMobile: isMobile,
|
||||
onAnyFieldChanged: () {}, // Géré automatiquement par le contrôleur
|
||||
onAnyFieldChanged:
|
||||
() {}, // Géré automatiquement par le contrôleur
|
||||
),
|
||||
EventStaffAndDocumentsSection(
|
||||
allUsers: controller.allUsers,
|
||||
@@ -290,9 +292,10 @@ class _EventAddEditPageState extends State<EventAddEditPage> {
|
||||
}
|
||||
},
|
||||
onSubmit: _submit,
|
||||
onSetConfirmed: !isEditMode ? () {
|
||||
} : null,
|
||||
onDelete: isEditMode ? _deleteEvent : null, // Ajout du callback de suppression
|
||||
onSetConfirmed: !isEditMode ? () {} : null,
|
||||
onDelete: isEditMode
|
||||
? _deleteEvent
|
||||
: null, // Ajout du callback de suppression
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -0,0 +1,384 @@
|
||||
import 'package:em2rp/models/event_model.dart';
|
||||
import 'package:em2rp/services/ai_equipment_assistant_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Résultat retourné par le dialog après confirmation de la proposition IA.
|
||||
class AiProposalResult {
|
||||
final List<EventEquipment> equipment;
|
||||
final List<String> containerIds;
|
||||
|
||||
const AiProposalResult({
|
||||
required this.equipment,
|
||||
required this.containerIds,
|
||||
});
|
||||
}
|
||||
|
||||
class AiEquipmentAssistantDialog extends StatefulWidget {
|
||||
final DateTime startDate;
|
||||
final DateTime endDate;
|
||||
final String? eventTypeId;
|
||||
final String? excludeEventId;
|
||||
final List<EventEquipment> currentAssignedEquipment;
|
||||
|
||||
const AiEquipmentAssistantDialog({
|
||||
super.key,
|
||||
required this.startDate,
|
||||
required this.endDate,
|
||||
required this.currentAssignedEquipment,
|
||||
this.eventTypeId,
|
||||
this.excludeEventId,
|
||||
});
|
||||
|
||||
@override
|
||||
State<AiEquipmentAssistantDialog> createState() =>
|
||||
_AiEquipmentAssistantDialogState();
|
||||
}
|
||||
|
||||
class _AiEquipmentAssistantDialogState
|
||||
extends State<AiEquipmentAssistantDialog> {
|
||||
final TextEditingController _messageController = TextEditingController();
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
final ScrollController _proposalScrollController = ScrollController();
|
||||
final List<_AssistantChatMessage> _messages = [];
|
||||
|
||||
late final AiEquipmentAssistantService _assistantService;
|
||||
|
||||
bool _isLoading = false;
|
||||
String? _errorMessage;
|
||||
AiEquipmentProposal? _latestProposal;
|
||||
late List<EventEquipment> _workingEquipment;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_assistantService = AiEquipmentAssistantService();
|
||||
_workingEquipment = List<EventEquipment>.from(widget.currentAssignedEquipment);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_messageController.dispose();
|
||||
_scrollController.dispose();
|
||||
_proposalScrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
bool get _isChatEmpty => _messages.isEmpty;
|
||||
|
||||
String get _actionButtonLabel {
|
||||
return _isChatEmpty ? 'Generer la liste automatiquement' : 'Envoyer';
|
||||
}
|
||||
|
||||
Future<void> _sendMessage() async {
|
||||
if (_isLoading) {
|
||||
return;
|
||||
}
|
||||
|
||||
final rawInput = _messageController.text.trim();
|
||||
final isAutoMode = _isChatEmpty;
|
||||
final userMessage = isAutoMode
|
||||
? (rawInput.isNotEmpty
|
||||
? rawInput
|
||||
: 'Genere automatiquement une proposition de materiel pour cet evenement.')
|
||||
: rawInput;
|
||||
|
||||
if (userMessage.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
_messageController.clear();
|
||||
setState(() {
|
||||
_errorMessage = null;
|
||||
_messages.add(_AssistantChatMessage.user(userMessage));
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
_scrollToBottom();
|
||||
|
||||
try {
|
||||
final response = await _assistantService
|
||||
.generateProposal(
|
||||
startDate: widget.startDate,
|
||||
endDate: widget.endDate,
|
||||
eventTypeId: widget.eventTypeId,
|
||||
excludeEventId: widget.excludeEventId,
|
||||
currentAssignedEquipment: widget.currentAssignedEquipment,
|
||||
workingProposalEquipment: _workingEquipment,
|
||||
userMessage: userMessage,
|
||||
history: _messages
|
||||
.map((message) => AiAssistantChatTurn(
|
||||
isUser: message.isUser, text: message.text))
|
||||
.toList(),
|
||||
);
|
||||
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_messages
|
||||
.add(_AssistantChatMessage.assistant(response.assistantMessage));
|
||||
_latestProposal = response.proposal;
|
||||
if (response.proposal != null) {
|
||||
_workingEquipment = List<EventEquipment>.from(
|
||||
response.proposal!.asEventEquipment,
|
||||
);
|
||||
}
|
||||
_isLoading = false;
|
||||
});
|
||||
_scrollToBottom();
|
||||
} on FormatException catch (error) {
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
_errorMessage = 'Reponse IA invalide: ${error.message}';
|
||||
});
|
||||
} catch (error) {
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
_errorMessage = 'Erreur IA: $error';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _scrollToBottom() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!_scrollController.hasClients) {
|
||||
return;
|
||||
}
|
||||
_scrollController.animateTo(
|
||||
_scrollController.position.maxScrollExtent,
|
||||
duration: const Duration(milliseconds: 250),
|
||||
curve: Curves.easeOut,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dialog(
|
||||
insetPadding: const EdgeInsets.symmetric(horizontal: 24, vertical: 24),
|
||||
child: SizedBox(
|
||||
width: 760,
|
||||
height: 640,
|
||||
child: Column(
|
||||
children: [
|
||||
AppBar(
|
||||
automaticallyImplyLeading: false,
|
||||
title: const Text('Assistant IA Logisticien'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
onPressed:
|
||||
_isLoading ? null : () => Navigator.of(context).pop(),
|
||||
),
|
||||
],
|
||||
),
|
||||
Expanded(
|
||||
child: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Container(
|
||||
color: Colors.grey.shade50,
|
||||
child: ListView.builder(
|
||||
controller: _scrollController,
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: _messages.length,
|
||||
itemBuilder: (context, index) {
|
||||
final message = _messages[index];
|
||||
return _buildMessageBubble(message);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_isLoading)
|
||||
const Padding(
|
||||
padding:
|
||||
EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: Row(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
SizedBox(width: 12),
|
||||
Text(
|
||||
'Generation en cours... verification du materiel et disponibilites.'),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (_errorMessage != null)
|
||||
Container(
|
||||
width: double.infinity,
|
||||
margin: const EdgeInsets.fromLTRB(16, 8, 16, 0),
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.red.shade50,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: Colors.red.shade200),
|
||||
),
|
||||
child: Text(
|
||||
_errorMessage!,
|
||||
style: TextStyle(color: Colors.red.shade800),
|
||||
),
|
||||
),
|
||||
if (_latestProposal != null)
|
||||
_buildProposalSummary(_latestProposal!),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _messageController,
|
||||
enabled: !_isLoading,
|
||||
minLines: 1,
|
||||
maxLines: 3,
|
||||
decoration: const InputDecoration(
|
||||
hintText:
|
||||
'Precisez votre besoin (style, jauge, contraintes...)',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
onSubmitted: (_) => _sendMessage(),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
ElevatedButton(
|
||||
onPressed: _isLoading ? null : _sendMessage,
|
||||
child: Text(_actionButtonLabel),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMessageBubble(_AssistantChatMessage message) {
|
||||
final bubbleColor = message.isUser ? Colors.blue.shade600 : Colors.white;
|
||||
final textColor = message.isUser ? Colors.white : Colors.black87;
|
||||
|
||||
return Align(
|
||||
alignment: message.isUser ? Alignment.centerRight : Alignment.centerLeft,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 10),
|
||||
constraints: const BoxConstraints(maxWidth: 560),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: bubbleColor,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border:
|
||||
message.isUser ? null : Border.all(color: Colors.grey.shade300),
|
||||
),
|
||||
child: Text(
|
||||
message.text,
|
||||
style: TextStyle(color: textColor),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildProposalSummary(AiEquipmentProposal proposal) {
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
margin: const EdgeInsets.fromLTRB(16, 8, 16, 0),
|
||||
padding: const EdgeInsets.all(12),
|
||||
constraints: const BoxConstraints(maxHeight: 240),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.green.shade50,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
border: Border.all(color: Colors.green.shade200),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'Recapitulatif propose',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Flexible(
|
||||
child: Scrollbar(
|
||||
controller: _proposalScrollController,
|
||||
thumbVisibility: true,
|
||||
child: SingleChildScrollView(
|
||||
controller: _proposalScrollController,
|
||||
padding: const EdgeInsets.only(right: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(proposal.summary),
|
||||
if (proposal.items.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
...proposal.items.map((item) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 4),
|
||||
child: Text(
|
||||
'- ${item.equipmentId} x${item.quantity} - ${item.rationale}',
|
||||
),
|
||||
);
|
||||
}),
|
||||
],
|
||||
if (proposal.containerIds.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'Boites proposees :',
|
||||
style: TextStyle(fontWeight: FontWeight.w600),
|
||||
),
|
||||
...proposal.containerIds.map((id) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 4),
|
||||
child: Text('- $id'),
|
||||
);
|
||||
}),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _isLoading
|
||||
? null
|
||||
: () => Navigator.of(context).pop(
|
||||
AiProposalResult(
|
||||
equipment: proposal.asEventEquipment,
|
||||
containerIds: proposal.containerIds,
|
||||
),
|
||||
),
|
||||
icon: const Icon(Icons.add_task),
|
||||
label: const Text('Confirmer et Ajouter'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _AssistantChatMessage {
|
||||
final bool isUser;
|
||||
final String text;
|
||||
|
||||
const _AssistantChatMessage._({required this.isUser, required this.text});
|
||||
|
||||
factory _AssistantChatMessage.user(String text) {
|
||||
return _AssistantChatMessage._(isUser: true, text: text);
|
||||
}
|
||||
|
||||
factory _AssistantChatMessage.assistant(String text) {
|
||||
return _AssistantChatMessage._(isUser: false, text: text);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import 'package:em2rp/providers/equipment_provider.dart';
|
||||
import 'package:em2rp/providers/container_provider.dart';
|
||||
import 'package:em2rp/utils/colors.dart';
|
||||
import 'package:em2rp/views/widgets/event/equipment_selection_dialog.dart';
|
||||
import 'package:em2rp/views/widgets/event_form/ai_equipment_assistant_dialog.dart';
|
||||
|
||||
/// Section pour afficher et gérer le matériel assigné à un événement
|
||||
class EventAssignedEquipmentSection extends StatefulWidget {
|
||||
@@ -17,6 +18,7 @@ class EventAssignedEquipmentSection extends StatefulWidget {
|
||||
final DateTime? endDate;
|
||||
final Function(List<EventEquipment>, List<String>) onChanged;
|
||||
final String? eventId; // Pour exclure l'événement actuel de la vérification
|
||||
final String? eventTypeId;
|
||||
|
||||
const EventAssignedEquipmentSection({
|
||||
super.key,
|
||||
@@ -26,14 +28,18 @@ class EventAssignedEquipmentSection extends StatefulWidget {
|
||||
required this.endDate,
|
||||
required this.onChanged,
|
||||
this.eventId,
|
||||
this.eventTypeId,
|
||||
});
|
||||
|
||||
@override
|
||||
State<EventAssignedEquipmentSection> createState() => _EventAssignedEquipmentSectionState();
|
||||
State<EventAssignedEquipmentSection> createState() =>
|
||||
_EventAssignedEquipmentSectionState();
|
||||
}
|
||||
|
||||
class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSection> {
|
||||
bool get _canAddMaterial => widget.startDate != null && widget.endDate != null;
|
||||
class _EventAssignedEquipmentSectionState
|
||||
extends State<EventAssignedEquipmentSection> {
|
||||
bool get _canAddMaterial =>
|
||||
widget.startDate != null && widget.endDate != null;
|
||||
final Map<String, EquipmentModel> _equipmentCache = {};
|
||||
final Map<String, ContainerModel> _containerCache = {};
|
||||
bool _isLoading = true;
|
||||
@@ -61,19 +67,24 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
final equipmentProvider = context.read<EquipmentProvider>();
|
||||
final containerProvider = context.read<ContainerProvider>();
|
||||
|
||||
DebugLog.info('[EventAssignedEquipmentSection] Loading caches from assigned lists');
|
||||
DebugLog.info(
|
||||
'[EventAssignedEquipmentSection] Loading caches from assigned lists');
|
||||
|
||||
// Toujours partir des données locales du formulaire pour éviter les décalages visuels.
|
||||
final equipmentIds = widget.assignedEquipment.map((eq) => eq.equipmentId).toList();
|
||||
final containers = await containerProvider.getContainersByIds(widget.assignedContainers);
|
||||
final equipmentIds =
|
||||
widget.assignedEquipment.map((eq) => eq.equipmentId).toList();
|
||||
final containers =
|
||||
await containerProvider.getContainersByIds(widget.assignedContainers);
|
||||
|
||||
final childEquipmentIds = <String>[];
|
||||
for (final container in containers) {
|
||||
childEquipmentIds.addAll(container.equipmentIds);
|
||||
}
|
||||
|
||||
final allEquipmentIds = <String>{...equipmentIds, ...childEquipmentIds}.toList();
|
||||
final equipment = await equipmentProvider.getEquipmentsByIds(allEquipmentIds);
|
||||
final allEquipmentIds =
|
||||
<String>{...equipmentIds, ...childEquipmentIds}.toList();
|
||||
final equipment =
|
||||
await equipmentProvider.getEquipmentsByIds(allEquipmentIds);
|
||||
|
||||
_equipmentCache.clear();
|
||||
_containerCache.clear();
|
||||
@@ -110,7 +121,9 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
_containerCache[containerId] = container;
|
||||
}
|
||||
} catch (e) {
|
||||
DebugLog.error('[EventAssignedEquipmentSection] Error loading equipment and containers', e);
|
||||
DebugLog.error(
|
||||
'[EventAssignedEquipmentSection] Error loading equipment and containers',
|
||||
e);
|
||||
} finally {
|
||||
setState(() => _isLoading = false);
|
||||
}
|
||||
@@ -138,7 +151,8 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
}
|
||||
|
||||
Future<void> _processSelection(Map<String, SelectedItem> selection) async {
|
||||
DebugLog.info('[EventAssignedEquipmentSection] Processing selection of ${selection.length} items');
|
||||
DebugLog.info(
|
||||
'[EventAssignedEquipmentSection] Processing selection of ${selection.length} items');
|
||||
|
||||
// Séparer équipements et conteneurs
|
||||
final newEquipment = <EventEquipment>[];
|
||||
@@ -155,23 +169,27 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
}
|
||||
}
|
||||
|
||||
DebugLog.info('[EventAssignedEquipmentSection] Found ${newEquipment.length} equipment(s) and ${newContainers.length} container(s)');
|
||||
DebugLog.info(
|
||||
'[EventAssignedEquipmentSection] Found ${newEquipment.length} equipment(s) and ${newContainers.length} container(s)');
|
||||
|
||||
// 🔧 FIX: Pour chaque container sélectionné, ajouter aussi ses équipements enfants
|
||||
if (newContainers.isNotEmpty) {
|
||||
final containerProvider = context.read<ContainerProvider>();
|
||||
final containers = await containerProvider.getContainersByIds(newContainers);
|
||||
final containers =
|
||||
await containerProvider.getContainersByIds(newContainers);
|
||||
|
||||
for (var container in containers) {
|
||||
for (var childEquipmentId in container.equipmentIds) {
|
||||
// Vérifier si l'équipement enfant n'est pas déjà dans la liste
|
||||
final existsInNew = newEquipment.any((eq) => eq.equipmentId == childEquipmentId);
|
||||
final existsInNew =
|
||||
newEquipment.any((eq) => eq.equipmentId == childEquipmentId);
|
||||
if (!existsInNew) {
|
||||
newEquipment.add(EventEquipment(
|
||||
equipmentId: childEquipmentId,
|
||||
quantity: 1,
|
||||
));
|
||||
DebugLog.info('[EventAssignedEquipmentSection] Adding child equipment $childEquipmentId from container ${container.id}');
|
||||
DebugLog.info(
|
||||
'[EventAssignedEquipmentSection] Adding child equipment $childEquipmentId from container ${container.id}');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,11 +201,12 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
// Fusionner avec l'existant
|
||||
final updatedEquipment = [...widget.assignedEquipment];
|
||||
final updatedContainers = [...widget.assignedContainers];
|
||||
|
||||
|
||||
// Pour chaque nouvel équipement
|
||||
for (var eq in newEquipment) {
|
||||
final existingIndex = updatedEquipment.indexWhere((e) => e.equipmentId == eq.equipmentId);
|
||||
|
||||
final existingIndex =
|
||||
updatedEquipment.indexWhere((e) => e.equipmentId == eq.equipmentId);
|
||||
|
||||
if (existingIndex != -1) {
|
||||
// L'équipement existe déjà : mettre à jour la quantité
|
||||
updatedEquipment[existingIndex] = EventEquipment(
|
||||
@@ -204,17 +223,64 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
updatedEquipment.add(eq);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (var containerId in newContainers) {
|
||||
if (!updatedContainers.contains(containerId)) {
|
||||
updatedContainers.add(containerId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Notifier le changement
|
||||
widget.onChanged(updatedEquipment, updatedContainers);
|
||||
}
|
||||
|
||||
Future<void> _openAiAssistantDialog() async {
|
||||
if (widget.startDate == null || widget.endDate == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final result = await showDialog<AiProposalResult>(
|
||||
context: context,
|
||||
builder: (context) => AiEquipmentAssistantDialog(
|
||||
startDate: widget.startDate!,
|
||||
endDate: widget.endDate!,
|
||||
eventTypeId: widget.eventTypeId,
|
||||
excludeEventId: widget.eventId,
|
||||
currentAssignedEquipment: widget.assignedEquipment,
|
||||
),
|
||||
);
|
||||
|
||||
if (result == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
_applyAiProposal(result);
|
||||
}
|
||||
|
||||
void _applyAiProposal(AiProposalResult result) {
|
||||
final existingById = {
|
||||
for (final equipment in widget.assignedEquipment)
|
||||
equipment.equipmentId: equipment,
|
||||
};
|
||||
|
||||
final updatedEquipment = result.equipment.map((proposed) {
|
||||
final existing = existingById[proposed.equipmentId];
|
||||
if (existing == null) {
|
||||
return proposed;
|
||||
}
|
||||
return existing.copyWith(quantity: proposed.quantity);
|
||||
}).toList();
|
||||
|
||||
final updatedContainers = [...widget.assignedContainers];
|
||||
for (final containerId in result.containerIds) {
|
||||
if (!updatedContainers.contains(containerId)) {
|
||||
updatedContainers.add(containerId);
|
||||
}
|
||||
}
|
||||
|
||||
widget.onChanged(updatedEquipment, updatedContainers);
|
||||
}
|
||||
|
||||
void _removeEquipment(String equipmentId) {
|
||||
final updated = widget.assignedEquipment
|
||||
.where((eq) => eq.equipmentId != equipmentId)
|
||||
@@ -231,9 +297,8 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
final container = _containerCache[containerId];
|
||||
|
||||
// Retirer le conteneur de la liste
|
||||
final updatedContainers = widget.assignedContainers
|
||||
.where((id) => id != containerId)
|
||||
.toList();
|
||||
final updatedContainers =
|
||||
widget.assignedContainers.where((id) => id != containerId).toList();
|
||||
|
||||
// 🔧 FIX: Ne supprimer les équipements enfants QUE s'ils ne sont pas dans un autre container
|
||||
final updatedEquipment = <EventEquipment>[];
|
||||
@@ -252,8 +317,10 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
// 1. Ne sont PAS dans le container supprimé OU
|
||||
// 2. Sont dans le container supprimé MAIS aussi dans un autre container
|
||||
for (var eq in widget.assignedEquipment) {
|
||||
final isInRemovedContainer = container.equipmentIds.contains(eq.equipmentId);
|
||||
final isInOtherContainer = equipmentIdsInOtherContainers.contains(eq.equipmentId);
|
||||
final isInRemovedContainer =
|
||||
container.equipmentIds.contains(eq.equipmentId);
|
||||
final isInOtherContainer =
|
||||
equipmentIdsInOtherContainers.contains(eq.equipmentId);
|
||||
|
||||
if (!isInRemovedContainer || isInOtherContainer) {
|
||||
updatedEquipment.add(eq);
|
||||
@@ -271,7 +338,8 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
_containerCache.remove(containerId);
|
||||
// Nettoyer le cache uniquement pour les équipements effectivement supprimés
|
||||
if (container != null) {
|
||||
final remainingEquipmentIds = updatedEquipment.map((eq) => eq.equipmentId).toSet();
|
||||
final remainingEquipmentIds =
|
||||
updatedEquipment.map((eq) => eq.equipmentId).toSet();
|
||||
for (var equipmentId in container.equipmentIds) {
|
||||
if (!remainingEquipmentIds.contains(equipmentId)) {
|
||||
_equipmentCache.remove(equipmentId);
|
||||
@@ -301,7 +369,8 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final totalItems = widget.assignedEquipment.length + widget.assignedContainers.length;
|
||||
final totalItems =
|
||||
widget.assignedEquipment.length + widget.assignedContainers.length;
|
||||
|
||||
return Card(
|
||||
elevation: 2,
|
||||
@@ -350,15 +419,25 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
],
|
||||
),
|
||||
),
|
||||
ActionChip(
|
||||
onPressed: _canAddMaterial ? _openAiAssistantDialog : null,
|
||||
avatar: const Icon(Icons.auto_fix_high, size: 18),
|
||||
label: const Text('Assistant IA'),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _canAddMaterial ? _openSelectionDialog : null,
|
||||
icon: Icon(Icons.add, color: _canAddMaterial ? Colors.white : Colors.grey),
|
||||
icon: Icon(Icons.add,
|
||||
color: _canAddMaterial ? Colors.white : Colors.grey),
|
||||
label: Text(
|
||||
'Ajouter',
|
||||
style: TextStyle(color: _canAddMaterial ? Colors.white : Colors.grey),
|
||||
style: TextStyle(
|
||||
color: _canAddMaterial ? Colors.white : Colors.grey),
|
||||
),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: _canAddMaterial ? AppColors.rouge : Colors.grey.shade300,
|
||||
backgroundColor: _canAddMaterial
|
||||
? AppColors.rouge
|
||||
: Colors.grey.shade300,
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -512,7 +591,8 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
}
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
padding:
|
||||
const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
@@ -537,7 +617,8 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
color: Colors.grey.shade600,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
eq.category.getIcon(size: 16, color: eq.category.color),
|
||||
eq.category
|
||||
.getIcon(size: 16, color: eq.category.color),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
@@ -562,7 +643,8 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEquipmentItem(EquipmentModel? equipment, EventEquipment eventEq) {
|
||||
Widget _buildEquipmentItem(
|
||||
EquipmentModel? equipment, EventEquipment eventEq) {
|
||||
if (equipment == null) {
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(bottom: 8),
|
||||
@@ -585,17 +667,15 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
}
|
||||
|
||||
final isConsumable = equipment.category == EquipmentCategory.consumable ||
|
||||
equipment.category == EquipmentCategory.cable;
|
||||
equipment.category == EquipmentCategory.cable;
|
||||
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(bottom: 8),
|
||||
child: ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: equipment.category.color.withValues(alpha: 0.2),
|
||||
child: equipment.category.getIconForAvatar(
|
||||
size: 24,
|
||||
color: equipment.category.color
|
||||
),
|
||||
child: equipment.category
|
||||
.getIconForAvatar(size: 24, color: equipment.category.color),
|
||||
),
|
||||
title: Text(
|
||||
equipment.id,
|
||||
@@ -634,4 +714,3 @@ class _EventAssignedEquipmentSectionState extends State<EventAssignedEquipmentSe
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user