feat: Enhance trip management features and improve UI responsiveness
- Implemented AutomaticKeepAliveClientMixin in HomeContent to maintain state during navigation. - Modified trip loading logic to trigger after the first frame for better performance. - Updated trip loading events to use LoadTripsByUserId for consistency. - Added temporary success messages for trip creation and operations. - Improved UI elements for better user experience, including updated text styles and spacing. - Refactored trip model to support Firestore timestamps and improved error handling during parsing. - Streamlined trip repository methods for better clarity and performance. - Enhanced trip service methods to ensure correct mapping from Firestore documents. - Removed unnecessary trip reset logic on logout.
This commit is contained in:
@@ -6,14 +6,20 @@ import '../../blocs/user/user_bloc.dart';
|
||||
import '../../blocs/user/user_state.dart' as user_state;
|
||||
import '../../blocs/trip/trip_bloc.dart';
|
||||
import '../../blocs/trip/trip_event.dart';
|
||||
import '../../blocs/trip/trip_state.dart';
|
||||
import '../../blocs/group/group_bloc.dart';
|
||||
import '../../blocs/group/group_event.dart';
|
||||
import '../../data/models/group.dart';
|
||||
import '../../data/models/group_member.dart';
|
||||
import '../../services/user_service.dart';
|
||||
import '../../repositories/group_repository.dart';
|
||||
|
||||
class CreateTripContent extends StatefulWidget {
|
||||
const CreateTripContent({super.key});
|
||||
final Trip? tripToEdit;
|
||||
const CreateTripContent({
|
||||
super.key,
|
||||
this.tripToEdit,
|
||||
});
|
||||
|
||||
@override
|
||||
State<CreateTripContent> createState() => _CreateTripContentState();
|
||||
@@ -27,6 +33,7 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
final _locationController = TextEditingController();
|
||||
final _budgetController = TextEditingController();
|
||||
final _userService = UserService();
|
||||
final _groupRepository = GroupRepository();
|
||||
|
||||
DateTime? _startDate;
|
||||
DateTime? _endDate;
|
||||
@@ -35,6 +42,58 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
final List<String> _participants = [];
|
||||
final _participantController = TextEditingController();
|
||||
|
||||
bool get isEditing => widget.tripToEdit != null;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_initializeFormWithTrip();
|
||||
}
|
||||
|
||||
Future<void> _initializeFormWithTrip() async {
|
||||
if (widget.tripToEdit != null) {
|
||||
final trip = widget.tripToEdit!;
|
||||
|
||||
setState(() {
|
||||
_titleController.text = trip.title;
|
||||
_descriptionController.text = trip.description;
|
||||
_locationController.text = trip.location;
|
||||
_budgetController.text = trip.budget?.toString() ?? '';
|
||||
_startDate = trip.startDate;
|
||||
_endDate = trip.endDate;
|
||||
});
|
||||
|
||||
await _loadParticipantEmails(trip.participants);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadParticipantEmails(List<String> participantIds) async {
|
||||
final userState = context.read<UserBloc>().state;
|
||||
String? currentUserId;
|
||||
|
||||
if (userState is user_state.UserLoaded) {
|
||||
currentUserId = userState.user.id;
|
||||
}
|
||||
|
||||
for (String userId in participantIds) {
|
||||
if (userId == currentUserId) continue;
|
||||
|
||||
try {
|
||||
final userDoc = await _userService.getUserById(userId);
|
||||
if (userDoc != null && userDoc.email.isNotEmpty) {
|
||||
setState(() {
|
||||
_participants.add(userDoc.email);
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
_errorService.logError(
|
||||
'create_trip_content.dart',
|
||||
'Erreur chargement participant $userId: $e',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_titleController.dispose();
|
||||
@@ -47,235 +106,271 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<UserBloc, user_state.UserState>(
|
||||
builder: (context, userState) {
|
||||
if (userState is! user_state.UserLoaded) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text('Créer un voyage')),
|
||||
body: Center(child: Text('Veuillez vous connecter')),
|
||||
);
|
||||
return BlocListener<TripBloc, TripState>(
|
||||
listener: (context, tripState) {
|
||||
// Écouter l'état TripCreated pour récupérer l'ID du voyage
|
||||
if (tripState is TripCreated) {
|
||||
_createGroupForTrip(tripState.tripId);
|
||||
} else if (tripState is TripOperationSuccess) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(tripState.message),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
Navigator.pop(context);
|
||||
if (isEditing) {
|
||||
Navigator.pop(context); // Retour supplémentaire en mode édition
|
||||
}
|
||||
}
|
||||
} else if (tripState is TripError) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(tripState.message),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
child: BlocBuilder<UserBloc, user_state.UserState>(
|
||||
builder: (context, userState) {
|
||||
if (userState is! user_state.UserLoaded) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(isEditing ? 'Modifier le voyage' : 'Créer un voyage'),
|
||||
),
|
||||
body: Center(child: Text('Veuillez vous connecter')),
|
||||
);
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Créer un voyage'),
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildSectionTitle('Informations générales'),
|
||||
SizedBox(height: 16),
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(isEditing ? 'Modifier le voyage' : 'Créer un voyage'),
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildSectionTitle('Informations générales'),
|
||||
SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _titleController,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return 'Titre requis';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Titre du voyage *',
|
||||
hintText: 'ex: Voyage à Paris',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.travel_explore),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _descriptionController,
|
||||
maxLines: 3,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Description',
|
||||
hintText: 'Décrivez votre voyage...',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.description),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _locationController,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return 'Destination requise';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Destination *',
|
||||
hintText: 'ex: Paris, France',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.location_on),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 24),
|
||||
|
||||
_buildSectionTitle('Dates du voyage'),
|
||||
SizedBox(height: 16),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildDateField(
|
||||
label: 'Date de début *',
|
||||
date: _startDate,
|
||||
onTap: () => _selectStartDate(context),
|
||||
TextFormField(
|
||||
controller: _titleController,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return 'Titre requis';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Titre du voyage *',
|
||||
hintText: 'ex: Voyage à Paris',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.travel_explore),
|
||||
),
|
||||
SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: _buildDateField(
|
||||
label: 'Date de fin *',
|
||||
date: _endDate,
|
||||
onTap: () => _selectEndDate(context),
|
||||
),
|
||||
|
||||
SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _descriptionController,
|
||||
maxLines: 3,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Description',
|
||||
hintText: 'Décrivez votre voyage...',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.description),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _locationController,
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return 'Destination requise';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Destination *',
|
||||
hintText: 'ex: Paris, France',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.location_on),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 24),
|
||||
|
||||
_buildSectionTitle('Dates du voyage'),
|
||||
SizedBox(height: 16),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _buildDateField(
|
||||
label: 'Date de début *',
|
||||
date: _startDate,
|
||||
onTap: () => _selectStartDate(context),
|
||||
),
|
||||
),
|
||||
SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: _buildDateField(
|
||||
label: 'Date de fin *',
|
||||
date: _endDate,
|
||||
onTap: () => _selectEndDate(context),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
SizedBox(height: 24),
|
||||
|
||||
_buildSectionTitle('Budget'),
|
||||
SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _budgetController,
|
||||
keyboardType: TextInputType.numberWithOptions(decimal: true),
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Budget estimé',
|
||||
hintText: 'ex: 1200.50',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.euro),
|
||||
suffixText: '€',
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 24),
|
||||
|
||||
_buildSectionTitle('Participants'),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
'Ajoutez les emails des personnes que vous souhaitez inviter',
|
||||
style: TextStyle(color: Colors.grey[600], fontSize: 14),
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: _participantController,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Email du participant',
|
||||
hintText: 'ex: ami@email.com',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.person_add),
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
ElevatedButton(
|
||||
onPressed: _addParticipant,
|
||||
style: ElevatedButton.styleFrom(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
padding: EdgeInsets.all(16),
|
||||
),
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
SizedBox(height: 16),
|
||||
|
||||
if (_participants.isNotEmpty) ...[
|
||||
Text(
|
||||
'Participants ajoutés (${_participants.length})',
|
||||
style: TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: Colors.grey[300]!),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: _participants
|
||||
.map(
|
||||
(email) => Chip(
|
||||
label: Text(email, style: TextStyle(fontSize: 12)),
|
||||
deleteIcon: Icon(Icons.close, size: 18),
|
||||
onDeleted: () => _removeParticipant(email),
|
||||
backgroundColor: Theme.of(context)
|
||||
.colorScheme
|
||||
.primary
|
||||
.withValues(alpha: 0.1),
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
SizedBox(height: 24),
|
||||
SizedBox(height: 32),
|
||||
|
||||
_buildSectionTitle('Budget'),
|
||||
SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _budgetController,
|
||||
keyboardType: TextInputType.numberWithOptions(decimal: true),
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Budget estimé',
|
||||
hintText: 'ex: 1200.50',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.euro),
|
||||
suffixText: '€',
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 24),
|
||||
|
||||
_buildSectionTitle('Participants'),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
'Ajoutez les emails des personnes que vous souhaitez inviter',
|
||||
style: TextStyle(color: Colors.grey[600], fontSize: 14),
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: _participantController,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Email du participant',
|
||||
hintText: 'ex: ami@email.com',
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
prefixIcon: Icon(Icons.person_add),
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
ElevatedButton(
|
||||
onPressed: _addParticipant,
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 50,
|
||||
child: ElevatedButton(
|
||||
onPressed: _isLoading ? null : () => _saveTrip(userState.user),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Colors.white,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
padding: EdgeInsets.all(16),
|
||||
),
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
SizedBox(height: 16),
|
||||
|
||||
if (_participants.isNotEmpty) ...[
|
||||
Text(
|
||||
'Participants ajoutés (${_participants.length})',
|
||||
style: TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: Colors.grey[300]!),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: _participants
|
||||
.map(
|
||||
(email) => Chip(
|
||||
label: Text(email, style: TextStyle(fontSize: 12)),
|
||||
deleteIcon: Icon(Icons.close, size: 18),
|
||||
onDeleted: () => _removeParticipant(email),
|
||||
backgroundColor: Theme.of(
|
||||
context,
|
||||
).colorScheme.primary.withAlpha(25),
|
||||
child: _isLoading
|
||||
? CircularProgressIndicator(color: Colors.white)
|
||||
: Text(
|
||||
isEditing ? 'Mettre à jour le voyage' : 'Créer le voyage',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 20),
|
||||
],
|
||||
|
||||
SizedBox(height: 32),
|
||||
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
height: 50,
|
||||
child: ElevatedButton(
|
||||
onPressed: _isLoading ? null : () => _saveTrip(userState.user),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
foregroundColor: Colors.white,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
child: _isLoading
|
||||
? CircularProgressIndicator(color: Colors.white)
|
||||
: Text(
|
||||
'Créer le voyage',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 20),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -300,7 +395,7 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
final labelColor = isDarkMode ? Colors.white70 : Colors.grey[600];
|
||||
final iconColor = isDarkMode ? Colors.white70 : Colors.grey[600];
|
||||
final placeholderColor = isDarkMode ? Colors.white38 : Colors.grey[500];
|
||||
|
||||
|
||||
return InkWell(
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
@@ -319,9 +414,7 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
Icon(Icons.calendar_today, size: 16, color: iconColor),
|
||||
SizedBox(width: 8),
|
||||
Text(
|
||||
date != null
|
||||
? '${date.day}/${date.month}/${date.year}'
|
||||
: 'Sélectionner',
|
||||
date != null ? '${date.day}/${date.month}/${date.year}' : 'Sélectionner',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: date != null ? textColor : placeholderColor,
|
||||
@@ -382,18 +475,15 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
|
||||
if (!emailRegex.hasMatch(email)) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Email invalide'))
|
||||
);
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Email invalide')));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (_participants.contains(email)) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Ce participant est déjà ajouté'))
|
||||
);
|
||||
ScaffoldMessenger.of(context)
|
||||
.showSnackBar(SnackBar(content: Text('Ce participant est déjà ajouté')));
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -410,6 +500,128 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
});
|
||||
}
|
||||
|
||||
// Mettre à jour le groupe avec les nouveaux membres
|
||||
Future<void> _updateGroupMembers(
|
||||
String tripId,
|
||||
user_state.UserModel currentUser,
|
||||
List<Map<String, String>> participantsData,
|
||||
) async {
|
||||
try {
|
||||
final group = await _groupRepository.getGroupByTripId(tripId);
|
||||
|
||||
if (group == null) {
|
||||
_errorService.logError(
|
||||
'create_trip_content.dart',
|
||||
'Groupe non trouvé pour le voyage $tripId',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final newMembers = <GroupMember>[
|
||||
GroupMember(
|
||||
userId: currentUser.id,
|
||||
firstName: currentUser.prenom,
|
||||
pseudo: currentUser.prenom,
|
||||
role: 'admin',
|
||||
),
|
||||
...participantsData.map((p) => GroupMember(
|
||||
userId: p['id'] as String,
|
||||
firstName: p['firstName'] as String,
|
||||
pseudo: p['firstName'] as String,
|
||||
role: 'member',
|
||||
)),
|
||||
];
|
||||
|
||||
final currentMembers = await _groupRepository.getGroupMembers(group.id);
|
||||
final currentMemberIds = currentMembers.map((m) => m.userId).toSet();
|
||||
final newMemberIds = newMembers.map((m) => m.userId).toSet();
|
||||
|
||||
final membersToAdd = newMembers.where((m) => !currentMemberIds.contains(m.userId)).toList();
|
||||
|
||||
final membersToRemove = currentMembers
|
||||
.where((m) => !newMemberIds.contains(m.userId) && m.role != 'admin')
|
||||
.toList();
|
||||
|
||||
for (final member in membersToAdd) {
|
||||
context.read<GroupBloc>().add(AddMemberToGroup(group.id, member));
|
||||
}
|
||||
|
||||
for (final member in membersToRemove) {
|
||||
context.read<GroupBloc>().add(RemoveMemberFromGroup(group.id, member.userId));
|
||||
}
|
||||
} catch (e) {
|
||||
_errorService.logError(
|
||||
'create_trip_content.dart',
|
||||
'Erreur lors de la mise à jour du groupe: $e',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// NOUVELLE MÉTHODE : Créer le groupe après la création du voyage
|
||||
Future<void> _createGroupForTrip(String tripId) async {
|
||||
try {
|
||||
final userState = context.read<UserBloc>().state;
|
||||
if (userState is! user_state.UserLoaded) return;
|
||||
|
||||
final currentUser = userState.user;
|
||||
final participantsData = await _getParticipantsData(_participants);
|
||||
|
||||
// Créer le groupe avec le tripId récupéré
|
||||
final group = Group(
|
||||
id: '', // Sera généré par Firestore
|
||||
name: _titleController.text.trim(),
|
||||
tripId: tripId, // ✅ ID du voyage récupéré
|
||||
createdBy: currentUser.id,
|
||||
);
|
||||
|
||||
final groupMembers = <GroupMember>[
|
||||
GroupMember(
|
||||
userId: currentUser.id,
|
||||
firstName: currentUser.prenom,
|
||||
pseudo: currentUser.prenom,
|
||||
role: 'admin',
|
||||
),
|
||||
...participantsData.map((p) => GroupMember(
|
||||
userId: p['id'] as String,
|
||||
firstName: p['firstName'] as String,
|
||||
pseudo: p['firstName'] as String,
|
||||
role: 'member',
|
||||
)),
|
||||
];
|
||||
|
||||
context.read<GroupBloc>().add(CreateGroupWithMembers(
|
||||
group: group,
|
||||
members: groupMembers,
|
||||
));
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Voyage et groupe créés avec succès !'),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
|
||||
Navigator.pop(context);
|
||||
}
|
||||
} catch (e) {
|
||||
_errorService.logError(
|
||||
'create_trip_content.dart',
|
||||
'Erreur lors de la création du groupe: $e',
|
||||
);
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _saveTrip(user_state.UserModel currentUser) async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
@@ -431,14 +643,13 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
try {
|
||||
final participantsData = await _getParticipantsData(_participants);
|
||||
List<String> participantIds = participantsData.map((p) => p['id'] as String).toList();
|
||||
|
||||
|
||||
if (!participantIds.contains(currentUser.id)) {
|
||||
participantIds.insert(0, currentUser.id);
|
||||
}
|
||||
|
||||
// Créer le voyage
|
||||
final trip = Trip(
|
||||
id: '',
|
||||
id: isEditing ? widget.tripToEdit!.id : '',
|
||||
title: _titleController.text.trim(),
|
||||
description: _descriptionController.text.trim(),
|
||||
location: _locationController.text.trim(),
|
||||
@@ -447,42 +658,23 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
budget: double.tryParse(_budgetController.text) ?? 0.0,
|
||||
createdBy: currentUser.id,
|
||||
participants: participantIds,
|
||||
createdAt: DateTime.now(),
|
||||
createdAt: isEditing ? widget.tripToEdit!.createdAt : DateTime.now(),
|
||||
updatedAt: DateTime.now(),
|
||||
);
|
||||
|
||||
context.read<TripBloc>().add(TripCreateRequested(trip: trip));
|
||||
if (isEditing) {
|
||||
// Mode mise à jour
|
||||
context.read<TripBloc>().add(TripUpdateRequested(trip: trip));
|
||||
|
||||
// Attendre que le trip soit créé (simplifié)
|
||||
await Future.delayed(Duration(milliseconds: 500));
|
||||
|
||||
final group = Group(
|
||||
id: '',
|
||||
name: _titleController.text.trim(),
|
||||
tripId: '',
|
||||
createdBy: currentUser.id,
|
||||
);
|
||||
|
||||
final groupMembers = <GroupMember>[
|
||||
GroupMember(
|
||||
userId: currentUser.id,
|
||||
firstName: currentUser.prenom,
|
||||
pseudo: currentUser.prenom, // Par défaut = prénom
|
||||
role: 'admin',
|
||||
),
|
||||
...participantsData.map((p) => GroupMember(
|
||||
userId: p['id'] as String,
|
||||
firstName: p['firstName'] as String,
|
||||
pseudo: p['firstName'] as String, // Par défaut = prénom
|
||||
role: 'member',
|
||||
)),
|
||||
];
|
||||
|
||||
|
||||
context.read<GroupBloc>().add(CreateGroupWithMembers(
|
||||
group: group,
|
||||
members: groupMembers,
|
||||
));
|
||||
await _updateGroupMembers(
|
||||
widget.tripToEdit!.id!,
|
||||
currentUser,
|
||||
participantsData,
|
||||
);
|
||||
} else {
|
||||
// Mode création - Le groupe sera créé dans le listener TripCreated
|
||||
context.read<TripBloc>().add(TripCreateRequested(trip: trip));
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -491,9 +683,7 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
@@ -501,9 +691,6 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
}
|
||||
}
|
||||
|
||||
// ...existing code...
|
||||
|
||||
// Récupérer les IDs et prénoms des participants
|
||||
Future<List<Map<String, String>>> _getParticipantsData(List<String> emails) async {
|
||||
List<Map<String, String>> participantsData = [];
|
||||
|
||||
@@ -511,10 +698,9 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
try {
|
||||
final userId = await _userService.getUserIdByEmail(email);
|
||||
if (userId != null) {
|
||||
// Récupérer le prénom de l'utilisateur
|
||||
final userDoc = await _userService.getUserById(userId);
|
||||
final firstName = userDoc?.prenom ?? 'Utilisateur';
|
||||
|
||||
|
||||
participantsData.add({
|
||||
'id': userId,
|
||||
'firstName': firstName,
|
||||
@@ -530,7 +716,10 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
_errorService.logError('Erreur lors de la récupération de l\'utilisateur $email: $e', StackTrace.current);
|
||||
_errorService.logError(
|
||||
'create_trip_content.dart',
|
||||
'Erreur lors de la récupération de l\'utilisateur $email: $e',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user