feat: Add group creation functionality and refactor trip service for improved data handling
This commit is contained in:
@@ -3,6 +3,8 @@ import 'package:provider/provider.dart';
|
|||||||
import 'package:travel_mate/models/trip.dart';
|
import 'package:travel_mate/models/trip.dart';
|
||||||
import 'package:travel_mate/providers/user_provider.dart';
|
import 'package:travel_mate/providers/user_provider.dart';
|
||||||
import 'package:travel_mate/services/trip_service.dart';
|
import 'package:travel_mate/services/trip_service.dart';
|
||||||
|
import 'package:travel_mate/services/group_service.dart';
|
||||||
|
import 'package:travel_mate/models/group.dart';
|
||||||
|
|
||||||
|
|
||||||
class CreateTripContent extends StatefulWidget {
|
class CreateTripContent extends StatefulWidget {
|
||||||
@@ -44,18 +46,6 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
|||||||
title: Text('Créer un voyage'),
|
title: Text('Créer un voyage'),
|
||||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||||
foregroundColor: Colors.white,
|
foregroundColor: Colors.white,
|
||||||
actions: [
|
|
||||||
TextButton(
|
|
||||||
onPressed: _isLoading ? null : _saveTrip,
|
|
||||||
child: Text(
|
|
||||||
'Sauvegarder',
|
|
||||||
style: TextStyle(
|
|
||||||
color: Colors.white,
|
|
||||||
fontWeight: FontWeight.bold,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
body: SingleChildScrollView(
|
body: SingleChildScrollView(
|
||||||
padding: EdgeInsets.all(16),
|
padding: EdgeInsets.all(16),
|
||||||
@@ -413,6 +403,55 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<bool> _saveGroup() async {
|
||||||
|
if (!_formKey.currentState!.validate()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
_isLoading = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
final members = await _changeUserEmailById(_participants);
|
||||||
|
|
||||||
|
final group = Group(
|
||||||
|
id: '',
|
||||||
|
name: _titleController.text.trim(),
|
||||||
|
members: members,
|
||||||
|
);
|
||||||
|
|
||||||
|
final groupService = GroupService();
|
||||||
|
bool success = await groupService.createGroup(group);
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<String>> _changeUserEmailById(List<String> participants) async {
|
||||||
|
final userProvider = Provider.of<UserProvider>(context, listen: false);
|
||||||
|
List<String> ids = [];
|
||||||
|
|
||||||
|
for (String email in participants) {
|
||||||
|
try {
|
||||||
|
final id = await userProvider.getUserIdByEmail(email);
|
||||||
|
if (id != null) {
|
||||||
|
ids.add(id);
|
||||||
|
} else {
|
||||||
|
print('Utilisateur non trouvé pour l\'ID: $email');
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text('Utilisateur non trouvé pour l\'email: $email'),
|
||||||
|
backgroundColor: Colors.orange,
|
||||||
|
duration: Duration(seconds: 2),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('Erreur lors de la récupération de l\'utilisateur $email: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ids;
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _saveTrip() async {
|
Future<void> _saveTrip() async {
|
||||||
if (!_formKey.currentState!.validate()) {
|
if (!_formKey.currentState!.validate()) {
|
||||||
return;
|
return;
|
||||||
@@ -443,34 +482,11 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
|||||||
|
|
||||||
// Convertir les emails en IDs utilisateur
|
// Convertir les emails en IDs utilisateur
|
||||||
List<String> participantIds = [];
|
List<String> participantIds = [];
|
||||||
|
|
||||||
// Ajouter automatiquement le créateur
|
|
||||||
if (currentUser.id != null) {
|
if (currentUser.id != null) {
|
||||||
participantIds.add(currentUser.id!);
|
participantIds.add(currentUser.id!);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convertir chaque email en ID utilisateur
|
participantIds = await _changeUserEmailById(_participants);
|
||||||
for (String email in _participants) {
|
|
||||||
try {
|
|
||||||
final userId = await userProvider.getUserIdByEmail(email);
|
|
||||||
if (userId != null && !participantIds.contains(userId)) {
|
|
||||||
participantIds.add(userId);
|
|
||||||
print('Email $email converti en ID: $userId');
|
|
||||||
} else if (userId == null) {
|
|
||||||
print('Utilisateur non trouvé pour l\'email: $email');
|
|
||||||
// Optionnel: afficher un warning à l'utilisateur
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
SnackBar(
|
|
||||||
content: Text('Utilisateur non trouvé pour l\'email: $email'),
|
|
||||||
backgroundColor: Colors.orange,
|
|
||||||
duration: Duration(seconds: 2),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
print('Erreur lors de la recherche de l\'utilisateur $email: $e');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Créer l'objet Trip avec les IDs des participants
|
// Créer l'objet Trip avec les IDs des participants
|
||||||
final trip = Trip(
|
final trip = Trip(
|
||||||
@@ -487,14 +503,16 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
|||||||
updatedAt: DateTime.now(),
|
updatedAt: DateTime.now(),
|
||||||
);
|
);
|
||||||
|
|
||||||
print('Participants IDs: $participantIds');
|
|
||||||
print('Données du voyage: ${trip.toMap()}');
|
print('Données du voyage: ${trip.toMap()}');
|
||||||
|
|
||||||
// Sauvegarder le voyage
|
// Sauvegarder le voyage
|
||||||
final tripService = TripService();
|
final tripService = TripService();
|
||||||
final success = await tripService.addTrip(trip);
|
final success = await tripService.addTrip(trip);
|
||||||
|
|
||||||
if (success && mounted) {
|
//Créer le groupe associé au voyage
|
||||||
|
final successGroup = await _saveGroup();
|
||||||
|
|
||||||
|
if (success && successGroup && mounted) {
|
||||||
print('Voyage créé avec succès !');
|
print('Voyage créé avec succès !');
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(
|
SnackBar(
|
||||||
@@ -525,4 +543,4 @@ class _CreateTripContentState extends State<CreateTripContent> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
import 'package:travel_mate/models/user.dart';
|
|
||||||
|
|
||||||
class Group {
|
class Group {
|
||||||
final String? id;
|
final String? id;
|
||||||
final String name;
|
final String name;
|
||||||
final List<User> members;
|
final List<String> members;
|
||||||
|
|
||||||
Group({
|
Group({
|
||||||
this.id,
|
this.id,
|
||||||
@@ -15,14 +13,14 @@ class Group {
|
|||||||
return Group(
|
return Group(
|
||||||
id: documentId,
|
id: documentId,
|
||||||
name: data['name'] ?? '',
|
name: data['name'] ?? '',
|
||||||
members: List<User>.from(data['members']?.map((member) => User.fromMap(member)) ?? []),
|
members: List<String>.from(data['members'] ?? []),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> toMap() {
|
Map<String, dynamic> toMap() {
|
||||||
return {
|
return {
|
||||||
'name': name,
|
'name': name,
|
||||||
'members': members.map((member) => member.toMap()).toList(),
|
'members': members,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,15 +12,33 @@ class GroupService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> createGroup(Group group) async {
|
Future<bool> createGroup(Group group) async {
|
||||||
await _firestore.collection('groups').add(group.toMap());
|
try {
|
||||||
|
await _firestore.collection('groups').add(group.toMap());
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
print('Erreur lors de la création du groupe: $e');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> updateGroup(Group group) async {
|
Future<bool> updateGroup(Group group) async {
|
||||||
await _firestore.collection('groups').doc(group.id).update(group.toMap());
|
try {
|
||||||
|
await _firestore.collection('groups').doc(group.id).update(group.toMap());
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
print('Erreur lors de la mise à jour du groupe: $e');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> deleteGroup(String groupId) async {
|
Future<bool> deleteGroup(String groupId) async {
|
||||||
await _firestore.collection('groups').doc(groupId).delete();
|
try {
|
||||||
|
await _firestore.collection('groups').doc(groupId).delete();
|
||||||
|
return true;
|
||||||
|
} catch (e) {
|
||||||
|
print('Erreur lors de la suppression du groupe: $e');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -30,9 +30,6 @@ class TripService {
|
|||||||
// Retirer l'ID vide du map
|
// Retirer l'ID vide du map
|
||||||
tripData.remove('id');
|
tripData.remove('id');
|
||||||
|
|
||||||
// Les participants contiennent déjà uniquement des IDs
|
|
||||||
// Pas besoin d'ajouter le créateur car il est déjà inclus
|
|
||||||
|
|
||||||
// Convertir les dates en Timestamp pour Firestore
|
// Convertir les dates en Timestamp pour Firestore
|
||||||
tripData['startDate'] = Timestamp.fromDate(trip.startDate);
|
tripData['startDate'] = Timestamp.fromDate(trip.startDate);
|
||||||
tripData['endDate'] = Timestamp.fromDate(trip.endDate);
|
tripData['endDate'] = Timestamp.fromDate(trip.endDate);
|
||||||
|
|||||||
Reference in New Issue
Block a user