feat: Add user-specific group retrieval and enhance UI with dynamic theming

This commit is contained in:
Dayron
2025-10-11 20:45:14 +02:00
parent 7f4fd610c5
commit 9eb55c358e
5 changed files with 64 additions and 25 deletions

View File

@@ -22,11 +22,11 @@ class _GroupContentState extends State<GroupContent> {
if (user == null || user.id == null) {
return const Center(
child: Text('Utilisateur non connecté'),
);
);
}
return StreamBuilder<List<Group>>(
stream: GroupService().getGroupsStream(),
stream: GroupService().getGroupsStreamByUser(user.id!), // Filtrer par utilisateur
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return _buildLoadingState();
@@ -52,11 +52,11 @@ class _GroupContentState extends State<GroupContent> {
}
final groups = snapshot.data ?? [];
print("Groupes reçus: ${groups.length}");
print("Groupes reçus pour l'utilisateur ${user.id}: ${groups.length}");
return RefreshIndicator(
onRefresh: () async {
setState() {};
setState(() {});
},
child: SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
@@ -74,13 +74,15 @@ class _GroupContentState extends State<GroupContent> {
),
const SizedBox(height: 20),
groups.isEmpty ? _buildEmptyState() : _buildGroupList(groups),
const SizedBox(height: 80),
],
),
),
);
},
);
}),
},
),
);
}

View File

@@ -287,12 +287,19 @@ class _CreateTripContentState extends State<CreateTripContent> {
required DateTime? date,
required VoidCallback onTap,
}) {
// Détecter le thème actuel
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
final textColor = isDarkMode ? Colors.white : Colors.black;
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(
padding: EdgeInsets.all(16),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[400]!),
border: Border.all(color: isDarkMode ? Colors.white24 : Colors.grey[400]!),
borderRadius: BorderRadius.circular(12),
),
child: Column(
@@ -300,12 +307,12 @@ class _CreateTripContentState extends State<CreateTripContent> {
children: [
Text(
label,
style: TextStyle(fontSize: 12, color: Colors.grey[600]),
style: TextStyle(fontSize: 12, color: labelColor),
),
SizedBox(height: 8),
Row(
children: [
Icon(Icons.calendar_today, size: 16, color: Colors.grey[600]),
Icon(Icons.calendar_today, size: 16, color: iconColor),
SizedBox(width: 8),
Text(
date != null
@@ -313,7 +320,7 @@ class _CreateTripContentState extends State<CreateTripContent> {
: 'Sélectionner',
style: TextStyle(
fontSize: 16,
color: date != null ? Colors.black : Colors.grey[500],
color: date != null ? textColor : placeholderColor,
),
),
],
@@ -403,25 +410,35 @@ class _CreateTripContentState extends State<CreateTripContent> {
});
}
Future<bool> _saveGroup() async {
Future<bool> _saveGroup(String currentUserId) async {
if (!_formKey.currentState!.validate()) {
return false;
}
setState(() {
_isLoading = true;
});
// Convertir les emails en IDs
final participantIds = await _changeUserEmailById(_participants);
final members = await _changeUserEmailById(_participants);
// Créer la liste des membres incluant le créateur
List<String> allMembers = [currentUserId];
// Ajouter tous les participants (éviter les doublons)
for (String participantId in participantIds) {
if (!allMembers.contains(participantId)) {
allMembers.add(participantId);
}
}
print('Membres du groupe: $allMembers');
final group = Group(
id: '',
name: _titleController.text.trim(),
members: members,
members: allMembers, // Contient le créateur + tous les participants
);
final groupService = GroupService();
bool success = await groupService.createGroup(group);
print('Groupe créé avec succès: $success');
return success;
}
@@ -474,19 +491,21 @@ class _CreateTripContentState extends State<CreateTripContent> {
final userProvider = Provider.of<UserProvider>(context, listen: false);
final currentUser = userProvider.currentUser;
if (currentUser == null) {
if (currentUser == null || currentUser.id == null) {
throw Exception('Utilisateur non connecté');
}
print('Création du voyage par: ${currentUser.id} (${currentUser.email})');
// Convertir les emails en IDs utilisateur
List<String> participantIds = [];
if (currentUser.id != null) {
participantIds.add(currentUser.id!);
List<String> participantIds = await _changeUserEmailById(_participants);
// Ajouter le créateur aux participants s'il n'y est pas déjà
if (!participantIds.contains(currentUser.id!)) {
participantIds.insert(0, currentUser.id!);
}
participantIds = await _changeUserEmailById(_participants);
print('Participants IDs (avec créateur): $participantIds');
// Créer l'objet Trip avec les IDs des participants
final trip = Trip(
@@ -497,8 +516,8 @@ class _CreateTripContentState extends State<CreateTripContent> {
startDate: _startDate!,
endDate: _endDate!,
budget: double.tryParse(_budgetController.text) ?? 0.0,
createdBy: currentUser.id ?? '',
participants: participantIds, // Contient uniquement les IDs
createdBy: currentUser.id!,
participants: participantIds, // Contient le créateur + tous les participants
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
@@ -509,11 +528,11 @@ class _CreateTripContentState extends State<CreateTripContent> {
final tripService = TripService();
final success = await tripService.addTrip(trip);
//Créer le groupe associé au voyage
final successGroup = await _saveGroup();
// Créer le groupe associé au voyage avec le créateur inclus
final successGroup = await _saveGroup(currentUser.id!);
if (success && successGroup && mounted) {
print('Voyage créé avec succès !');
print('Voyage et groupe créés avec succès !');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Voyage créé avec succès !'),

View File

@@ -3,11 +3,13 @@ class Message {
final DateTime timestamp;
final String senderId;
final String senderName;
final String groupId;
Message({
required this.text,
required this.timestamp,
required this.senderId,
required this.senderName,
required this.groupId,
});
}

View File

@@ -41,4 +41,20 @@ class GroupService {
return false;
}
}
// Stream pour obtenir les groupes d'un utilisateur spécifique
Stream<List<Group>> getGroupsStreamByUser(String userId) {
return _firestore
.collection('groups')
.where('members', arrayContains: userId)
.snapshots()
.map((snapshot) {
print('Groupes trouvés pour l\'utilisateur $userId: ${snapshot.docs.length}');
return snapshot.docs.map((doc) {
final group = Group.fromMap(doc.data(), doc.id);
print('Groupe: ${group.name}, Membres: ${group.members.length}');
return group;
}).toList();
});
}
}

View File