feat: Introduce memberIds for efficient group querying and management, updating related UI components and .gitignore.

This commit is contained in:
Van Leemput Dayron
2025-11-27 15:36:46 +01:00
parent 9198493dd5
commit cad9d42128
5 changed files with 435 additions and 157 deletions

4
.gitignore vendored
View File

@@ -44,7 +44,9 @@ app.*.map.json
/android/app/profile
/android/app/release
.vscode
.VSCodeCounter/
.VSCodeCounter/*
.env
.env.local
.env.*.local
firestore.rules
storage.rules

View File

@@ -36,6 +36,37 @@ class _CalendarPageState extends State<CalendarPage> {
}).toList();
}
Future<void> _selectTimeAndSchedule(Activity activity, DateTime date) async {
final TimeOfDay? pickedTime = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
builder: (BuildContext context, Widget? child) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(alwaysUse24HourFormat: true),
child: child!,
);
},
);
if (pickedTime != null && mounted) {
final scheduledDate = DateTime(
date.year,
date.month,
date.day,
pickedTime.hour,
pickedTime.minute,
);
context.read<ActivityBloc>().add(
UpdateActivityDate(
tripId: widget.trip.id!,
activityId: activity.id,
date: scheduledDate,
),
);
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
@@ -172,7 +203,9 @@ class _CalendarPageState extends State<CalendarPage> {
selectedActivities[index];
return ListTile(
title: Text(activity.name),
subtitle: Text(activity.category),
subtitle: Text(
'${activity.category} - ${DateFormat('HH:mm').format(activity.date!)}',
),
trailing: IconButton(
icon: const Icon(Icons.close),
onPressed: () {
@@ -243,14 +276,9 @@ class _CalendarPageState extends State<CalendarPage> {
icon: const Icon(Icons.arrow_back),
onPressed: () {
if (_selectedDay != null) {
context
.read<ActivityBloc>()
.add(
UpdateActivityDate(
tripId: widget.trip.id!,
activityId: activity.id,
date: _selectedDay,
),
_selectTimeAndSchedule(
activity,
_selectedDay!,
);
}
},
@@ -260,6 +288,33 @@ class _CalendarPageState extends State<CalendarPage> {
},
),
),
// Zone de drop pour le calendrier
DragTarget<Activity>(
onWillAccept: (data) => true,
onAccept: (activity) {
if (_selectedDay != null) {
_selectTimeAndSchedule(activity, _selectedDay!);
}
},
builder: (context, candidateData, rejectedData) {
return Container(
height: 50,
color: candidateData.isNotEmpty
? theme.colorScheme.primary.withValues(
alpha: 0.1,
)
: null,
child: Center(
child: Text(
'Glisser ici pour planifier',
style: TextStyle(
color: theme.colorScheme.primary,
),
),
),
);
},
),
],
),
),

View File

@@ -455,6 +455,9 @@ class _ShowTripDetailsContentState extends State<ShowTripDetailsContent> {
),
],
),
const SizedBox(height: 32),
_buildNextActivitiesSection(),
_buildExpensesCard(),
],
),
),
@@ -654,6 +657,21 @@ class _ShowTripDetailsContentState extends State<ShowTripDetailsContent> {
return FutureBuilder(
future: _groupRepository.getGroupByTripId(widget.trip.id!),
builder: (context, groupSnapshot) {
if (groupSnapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
if (groupSnapshot.hasError ||
!groupSnapshot.hasData ||
groupSnapshot.data == null) {
return const Center(child: Text('Aucun participant'));
}
final groupId = groupSnapshot.data!.id;
return StreamBuilder<List<GroupMember>>(
stream: _groupRepository.watchGroupMembers(groupId),
builder: (context, snapshot) {
// En attente
if (snapshot.connectionState == ConnectionState.waiting) {
@@ -670,13 +688,7 @@ class _ShowTripDetailsContentState extends State<ShowTripDetailsContent> {
);
}
// Pas de groupe trouvé
if (!snapshot.hasData || snapshot.data == null) {
return const Center(child: Text('Aucun participant'));
}
final group = snapshot.data!;
final members = group.members;
final members = snapshot.data ?? [];
if (members.isEmpty) {
return const Center(child: Text('Aucun participant'));
@@ -703,6 +715,8 @@ class _ShowTripDetailsContentState extends State<ShowTripDetailsContent> {
);
},
);
},
);
}
/// Construire un avatar pour un participant
@@ -950,4 +964,175 @@ class _ShowTripDetailsContentState extends State<ShowTripDetailsContent> {
),
);
}
Widget _buildNextActivitiesSection() {
final theme = Theme.of(context);
return Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Prochaines activités',
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
color: theme.colorScheme.onSurface,
),
),
TextButton(
onPressed: () => _navigateToActivities(),
child: Text(
'Tout voir',
style: TextStyle(
color: Colors.teal,
fontWeight: FontWeight.w600,
),
),
),
],
),
const SizedBox(height: 8),
_buildActivityCard(
title: 'Visite du Colisée',
date: '11 août, 10:00',
icon: Icons.museum,
),
const SizedBox(height: 12),
_buildActivityCard(
title: 'Dîner à Trastevere',
date: '11 août, 20:30',
icon: Icons.restaurant,
),
],
);
}
Widget _buildActivityCard({
required String title,
required String date,
required IconData icon,
}) {
final theme = Theme.of(context);
final isDarkMode = theme.brightness == Brightness.dark;
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: theme.cardColor,
borderRadius: BorderRadius.circular(16),
border: Border.all(
color: isDarkMode
? Colors.white.withValues(alpha: 0.1)
: Colors.black.withValues(alpha: 0.05),
width: 1,
),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(alpha: 0.05),
blurRadius: 10,
offset: const Offset(0, 2),
),
],
),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.teal.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(12),
),
child: Icon(icon, color: Colors.teal, size: 24),
),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: theme.textTheme.bodyLarge?.copyWith(
fontWeight: FontWeight.bold,
color: theme.colorScheme.onSurface,
),
),
const SizedBox(height: 4),
Text(
date,
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onSurface.withValues(alpha: 0.6),
),
),
],
),
),
Icon(
Icons.chevron_right,
color: theme.colorScheme.onSurface.withValues(alpha: 0.4),
),
],
),
);
}
Widget _buildExpensesCard() {
final theme = Theme.of(context);
return Container(
margin: const EdgeInsets.only(top: 24),
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: const Color(0xFFFDF4E3), // Light beige background
borderRadius: BorderRadius.circular(16),
),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(10),
decoration: const BoxDecoration(
color: Colors.orange,
shape: BoxShape.circle,
),
child: const Icon(
Icons.warning_amber_rounded,
color: Colors.white,
size: 24,
),
),
const SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Dépenses',
style: theme.textTheme.bodyLarge?.copyWith(
fontWeight: FontWeight.bold,
color: const Color(0xFF5D4037), // Brown text
),
),
const SizedBox(height: 4),
Text(
'Vous devez 25€ à Clara',
style: theme.textTheme.bodyMedium?.copyWith(
color: const Color(0xFF8D6E63), // Lighter brown
),
),
],
),
),
TextButton(
onPressed: () => _showComingSoon('Régler les dépenses'),
child: Text(
'Régler',
style: TextStyle(
color: const Color(0xFF5D4037),
fontWeight: FontWeight.bold,
),
),
),
],
),
);
}
}

View File

@@ -27,6 +27,9 @@ class Group {
/// List of members in this group
final List<GroupMember> members;
/// List of member IDs for efficient querying and security rules
final List<String> memberIds;
/// Creates a new [Group] instance.
///
/// [id], [name], [tripId], and [createdBy] are required.
@@ -40,9 +43,11 @@ class Group {
DateTime? createdAt,
DateTime? updatedAt,
List<GroupMember>? members,
List<String>? memberIds,
}) : createdAt = createdAt ?? DateTime.now(),
updatedAt = updatedAt ?? DateTime.now(),
members = members ?? [];
members = members ?? [],
memberIds = memberIds ?? [];
/// Creates a [Group] instance from a Firestore document map.
///
@@ -59,6 +64,7 @@ class Group {
createdAt: DateTime.fromMillisecondsSinceEpoch(map['createdAt'] ?? 0),
updatedAt: DateTime.fromMillisecondsSinceEpoch(map['updatedAt'] ?? 0),
members: [],
memberIds: List<String>.from(map['memberIds'] ?? []),
);
}
@@ -69,6 +75,7 @@ class Group {
'createdBy': createdBy,
'createdAt': createdAt.millisecondsSinceEpoch,
'updatedAt': updatedAt.millisecondsSinceEpoch,
'memberIds': memberIds,
};
}
@@ -80,6 +87,7 @@ class Group {
DateTime? createdAt,
DateTime? updatedAt,
List<GroupMember>? members,
List<String>? memberIds,
}) {
return Group(
id: id ?? this.id,
@@ -89,6 +97,7 @@ class Group {
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
members: members ?? this.members,
memberIds: memberIds ?? this.memberIds,
);
}
}

View File

@@ -1,10 +1,12 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:travel_mate/services/error_service.dart';
import '../models/group.dart';
import '../models/group_member.dart';
class GroupRepository {
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final FirebaseAuth _auth = FirebaseAuth.instance;
final _errorService = ErrorService();
CollectionReference get _groupsCollection => _firestore.collection('groups');
@@ -21,7 +23,9 @@ class GroupRepository {
return await _firestore.runTransaction<String>((transaction) async {
final groupRef = _groupsCollection.doc();
final groupData = group.toMap();
// Ajouter les IDs des membres à la liste memberIds
final memberIds = members.map((m) => m.userId).toList();
final groupData = group.copyWith(memberIds: memberIds).toMap();
transaction.set(groupRef, groupData);
for (var member in members) {
@@ -38,50 +42,13 @@ class GroupRepository {
Stream<List<Group>> getGroupsByUserId(String userId) {
return _groupsCollection
.where('memberIds', arrayContains: userId)
.snapshots()
.asyncMap((snapshot) async {
List<Group> userGroups = [];
for (var groupDoc in snapshot.docs) {
try {
final groupId = groupDoc.id;
// Vérifier si l'utilisateur est membre
final memberDoc = await groupDoc.reference
.collection('members')
.doc(userId)
.get();
if (memberDoc.exists) {
final groupData = groupDoc.data() as Map<String, dynamic>;
final group = Group.fromMap(groupData, groupId);
final members = await getGroupMembers(groupId);
userGroups.add(group.copyWith(members: members));
} else {
_errorService.logInfo('group_repository.dart','Utilisateur NON membre de $groupId');
}
} catch (e, stackTrace) {
_errorService.logError(e.toString(), stackTrace);
}
}
return userGroups;
})
.distinct((prev, next) {
// Comparer les listes pour éviter les doublons
if (prev.length != next.length) {
return false;
}
// Vérifier si les IDs sont identiques
final prevIds = prev.map((g) => g.id).toSet();
final nextIds = next.map((g) => g.id).toSet();
final identical = prevIds.difference(nextIds).isEmpty &&
nextIds.difference(prevIds).isEmpty;
return identical;
.map((snapshot) {
return snapshot.docs.map((doc) {
final groupData = doc.data() as Map<String, dynamic>;
return Group.fromMap(groupData, doc.id);
}).toList();
})
.handleError((error, stackTrace) {
_errorService.logError(error, stackTrace);
@@ -106,11 +73,30 @@ class GroupRepository {
Future<Group?> getGroupByTripId(String tripId) async {
try {
final querySnapshot = await _groupsCollection
final userId = _auth.currentUser?.uid;
if (userId == null) return null;
// Tentative 1: Requête optimisée avec memberIds
var querySnapshot = await _groupsCollection
.where('tripId', isEqualTo: tripId)
.where('memberIds', arrayContains: userId)
.limit(1)
.get();
// Tentative 2: Fallback pour le créateur (si memberIds est manquant - anciennes données)
if (querySnapshot.docs.isEmpty) {
querySnapshot = await _groupsCollection
.where('tripId', isEqualTo: tripId)
.where('createdBy', isEqualTo: userId)
.limit(1)
.get();
// Si on trouve le groupe via le fallback, on lance une migration
if (querySnapshot.docs.isNotEmpty) {
_migrateGroupData(querySnapshot.docs.first.id);
}
}
if (querySnapshot.docs.isEmpty) return null;
final doc = querySnapshot.docs.first;
@@ -123,17 +109,27 @@ class GroupRepository {
}
}
/// Méthode utilitaire pour migrer les anciennes données
Future<void> _migrateGroupData(String groupId) async {
try {
final members = await getGroupMembers(groupId);
final memberIds = members.map((m) => m.userId).toList();
if (memberIds.isNotEmpty) {
await _groupsCollection.doc(groupId).update({'memberIds': memberIds});
print('Migration réussie pour le groupe $groupId');
}
} catch (e) {
print('Erreur de migration pour le groupe $groupId: $e');
}
}
Future<List<GroupMember>> getGroupMembers(String groupId) async {
try {
final snapshot = await _membersCollection(groupId).get();
return snapshot.docs
.map((doc) {
return GroupMember.fromMap(
doc.data() as Map<String, dynamic>,
doc.id,
);
})
.toList();
return snapshot.docs.map((doc) {
return GroupMember.fromMap(doc.data() as Map<String, dynamic>, doc.id);
}).toList();
} catch (e) {
throw Exception('Erreur lors de la récupération des membres: $e');
}
@@ -141,10 +137,22 @@ class GroupRepository {
Future<void> addMember(String groupId, GroupMember member) async {
try {
// 1. Récupérer le groupe pour avoir le tripId
final group = await getGroupById(groupId);
if (group == null) throw Exception('Groupe introuvable');
// 2. Ajouter le membre dans la sous-collection members du groupe
await _membersCollection(groupId).doc(member.userId).set(member.toMap());
// 3. Mettre à jour la liste memberIds du groupe
await _groupsCollection.doc(groupId).update({
'updatedAt': DateTime.now().millisecondsSinceEpoch,
'memberIds': FieldValue.arrayUnion([member.userId]),
});
// 4. Mettre à jour la liste participants du voyage
await _firestore.collection('trips').doc(group.tripId).update({
'participants': FieldValue.arrayUnion([member.userId]),
});
} catch (e) {
throw Exception('Erreur lors de l\'ajout du membre: $e');
@@ -153,10 +161,22 @@ class GroupRepository {
Future<void> removeMember(String groupId, String userId) async {
try {
// 1. Récupérer le groupe pour avoir le tripId
final group = await getGroupById(groupId);
if (group == null) throw Exception('Groupe introuvable');
// 2. Supprimer le membre de la sous-collection members du groupe
await _membersCollection(groupId).doc(userId).delete();
// 3. Mettre à jour la liste memberIds du groupe
await _groupsCollection.doc(groupId).update({
'updatedAt': DateTime.now().millisecondsSinceEpoch,
'memberIds': FieldValue.arrayRemove([userId]),
});
// 4. Mettre à jour la liste participants du voyage
await _firestore.collection('trips').doc(group.tripId).update({
'participants': FieldValue.arrayRemove([userId]),
});
} catch (e) {
throw Exception('Erreur lors de la suppression du membre: $e');
@@ -165,8 +185,11 @@ class GroupRepository {
Future<void> updateGroup(String groupId, Group group) async {
try {
await _groupsCollection.doc(groupId).update(
group.toMap()..['updatedAt'] = DateTime.now().millisecondsSinceEpoch,
await _groupsCollection
.doc(groupId)
.update(
group.toMap()
..['updatedAt'] = DateTime.now().millisecondsSinceEpoch,
);
} catch (e) {
throw Exception('Erreur lors de la mise à jour du groupe: $e');
@@ -175,8 +198,12 @@ class GroupRepository {
Future<void> deleteGroup(String tripId) async {
try {
final userId = _auth.currentUser?.uid;
if (userId == null) throw Exception('Utilisateur non connecté');
final querySnapshot = await _groupsCollection
.where('tripId', isEqualTo: tripId)
.where('createdBy', isEqualTo: userId)
.limit(1)
.get();
@@ -201,10 +228,10 @@ class GroupRepository {
Stream<List<GroupMember>> watchGroupMembers(String groupId) {
return _membersCollection(groupId).snapshots().map(
(snapshot) => snapshot.docs
.map((doc) => GroupMember.fromMap(
doc.data() as Map<String, dynamic>,
doc.id,
))
.map(
(doc) =>
GroupMember.fromMap(doc.data() as Map<String, dynamic>, doc.id),
)
.toList(),
);
}