Resolved error for goup page
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
|
||||
<application
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'group_event.dart';
|
||||
import 'group_state.dart';
|
||||
import '../../repositories/group_repository.dart';
|
||||
import '../../data/models/group.dart';
|
||||
|
||||
class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
final GroupRepository _repository;
|
||||
@@ -10,6 +11,7 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
|
||||
GroupBloc(this._repository) : super(GroupInitial()) {
|
||||
on<LoadGroupsByUserId>(_onLoadGroupsByUserId);
|
||||
on<_GroupsUpdated>(_onGroupsUpdated); // NOUVEAU événement interne
|
||||
on<LoadGroupsByTrip>(_onLoadGroupsByTrip);
|
||||
on<CreateGroup>(_onCreateGroup);
|
||||
on<CreateGroupWithMembers>(_onCreateGroupWithMembers);
|
||||
@@ -19,22 +21,58 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
on<DeleteGroup>(_onDeleteGroup);
|
||||
}
|
||||
|
||||
// NOUVEAU : Charger les groupes par userId
|
||||
Future<void> _onLoadGroupsByUserId(
|
||||
LoadGroupsByUserId event,
|
||||
Emitter<GroupState> emit,
|
||||
) async {
|
||||
emit(GroupLoading());
|
||||
print('===== GroupBloc: _onLoadGroupsByUserId START =====');
|
||||
|
||||
await _groupsSubscription?.cancel();
|
||||
try {
|
||||
emit(GroupLoading());
|
||||
print('>>> GroupBloc: État GroupLoading émis');
|
||||
|
||||
_groupsSubscription = _repository.getGroupsByUserId(event.userId).listen(
|
||||
(groups) => emit(GroupsLoaded(groups)),
|
||||
onError: (error) => emit(GroupError(error.toString())),
|
||||
);
|
||||
await _groupsSubscription?.cancel();
|
||||
print('>>> GroupBloc: Ancien subscription annulé');
|
||||
|
||||
_groupsSubscription = _repository.getGroupsByUserId(event.userId).listen(
|
||||
(groups) {
|
||||
print('===== GroupBloc: Stream reçu ${groups.length} groupes =====');
|
||||
// Utiliser un événement interne au lieu d'émettre directement
|
||||
add(_GroupsUpdated(groups));
|
||||
},
|
||||
onError: (error) {
|
||||
print('===== GroupBloc: Erreur stream: $error =====');
|
||||
add(_GroupsUpdated([], error: error.toString()));
|
||||
},
|
||||
);
|
||||
|
||||
print('>>> GroupBloc: Subscription créé avec succès');
|
||||
} catch (e, stackTrace) {
|
||||
print('===== GroupBloc: Exception _onLoadGroupsByUserId =====');
|
||||
print('Exception: $e');
|
||||
print('StackTrace: $stackTrace');
|
||||
emit(GroupError(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// NOUVEAU: Handler pour les mises à jour du stream
|
||||
Future<void> _onGroupsUpdated(
|
||||
_GroupsUpdated event,
|
||||
Emitter<GroupState> emit,
|
||||
) async {
|
||||
print('===== GroupBloc: _onGroupsUpdated =====');
|
||||
print('Groupes reçus: ${event.groups.length}');
|
||||
|
||||
if (event.error != null) {
|
||||
print('>>> Émission GroupError: ${event.error}');
|
||||
emit(GroupError(event.error!));
|
||||
} else {
|
||||
print('>>> Émission GroupsLoaded avec ${event.groups.length} groupes');
|
||||
emit(GroupsLoaded(event.groups));
|
||||
print('>>> GroupsLoaded émis avec succès !');
|
||||
}
|
||||
}
|
||||
|
||||
// Charger les groupes d'un voyage (conservé)
|
||||
Future<void> _onLoadGroupsByTrip(
|
||||
LoadGroupsByTrip event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -52,7 +90,6 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
}
|
||||
}
|
||||
|
||||
// Créer un groupe simple
|
||||
Future<void> _onCreateGroup(
|
||||
CreateGroup event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -69,7 +106,6 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
}
|
||||
}
|
||||
|
||||
// Créer un groupe avec ses membres
|
||||
Future<void> _onCreateGroupWithMembers(
|
||||
CreateGroupWithMembers event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -86,7 +122,6 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
}
|
||||
}
|
||||
|
||||
// Ajouter un membre
|
||||
Future<void> _onAddMemberToGroup(
|
||||
AddMemberToGroup event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -99,7 +134,6 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
}
|
||||
}
|
||||
|
||||
// Supprimer un membre
|
||||
Future<void> _onRemoveMemberFromGroup(
|
||||
RemoveMemberFromGroup event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -112,7 +146,6 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
}
|
||||
}
|
||||
|
||||
// Mettre à jour un groupe
|
||||
Future<void> _onUpdateGroup(
|
||||
UpdateGroup event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -125,7 +158,6 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
}
|
||||
}
|
||||
|
||||
// Supprimer un groupe
|
||||
Future<void> _onDeleteGroup(
|
||||
DeleteGroup event,
|
||||
Emitter<GroupState> emit,
|
||||
@@ -140,7 +172,19 @@ class GroupBloc extends Bloc<GroupEvent, GroupState> {
|
||||
|
||||
@override
|
||||
Future<void> close() {
|
||||
print('===== GroupBloc: close() =====');
|
||||
_groupsSubscription?.cancel();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
||||
// NOUVEAU: Événement interne pour les mises à jour du stream
|
||||
class _GroupsUpdated extends GroupEvent {
|
||||
final List<Group> groups;
|
||||
final String? error;
|
||||
|
||||
const _GroupsUpdated(this.groups, {this.error});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [groups, error];
|
||||
}
|
||||
@@ -18,27 +18,55 @@ class _GroupContentState extends State<GroupContent> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadInitialData();
|
||||
print('===== GroupContent: initState =====');
|
||||
|
||||
// Charger immédiatement sans attendre le prochain frame
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
print('===== PostFrameCallback déclenché =====');
|
||||
_loadInitialData();
|
||||
});
|
||||
}
|
||||
|
||||
void _loadInitialData() {
|
||||
final userState = context.read<UserBloc>().state;
|
||||
if (userState is user_state.UserLoaded) {
|
||||
context.read<GroupBloc>().add(LoadGroupsByUserId(userState.user.id));
|
||||
print('===== _loadInitialData START =====');
|
||||
try {
|
||||
final userState = context.read<UserBloc>().state;
|
||||
print('UserBloc state type: ${userState.runtimeType}');
|
||||
print('UserBloc state: $userState');
|
||||
|
||||
if (userState is user_state.UserLoaded) {
|
||||
final userId = userState.user.id;
|
||||
print('✓ User chargé, ID: $userId');
|
||||
print('>>> Envoi de LoadGroupsByUserId <<<');
|
||||
context.read<GroupBloc>().add(LoadGroupsByUserId(userId));
|
||||
print('>>> LoadGroupsByUserId envoyé <<<');
|
||||
} else {
|
||||
print('✗ UserState n\'est pas UserLoaded');
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
print('===== ERREUR _loadInitialData =====');
|
||||
print('Exception: $e');
|
||||
print('StackTrace: $stackTrace');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
print('===== GroupContent: build =====');
|
||||
|
||||
return BlocBuilder<UserBloc, user_state.UserState>(
|
||||
builder: (context, userState) {
|
||||
print('>>> UserBloc builder - state type: ${userState.runtimeType}');
|
||||
|
||||
if (userState is user_state.UserLoading) {
|
||||
print('État: UserLoading');
|
||||
return const Scaffold(
|
||||
body: Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
|
||||
if (userState is user_state.UserError) {
|
||||
print('État: UserError - ${userState.message}');
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Column(
|
||||
@@ -54,25 +82,31 @@ class _GroupContentState extends State<GroupContent> {
|
||||
}
|
||||
|
||||
if (userState is! user_state.UserLoaded) {
|
||||
print('État: Utilisateur non connecté');
|
||||
return const Scaffold(
|
||||
body: Center(child: Text('Utilisateur non connecté')),
|
||||
);
|
||||
}
|
||||
|
||||
print('✓ État: UserLoaded');
|
||||
final user = userState.user;
|
||||
|
||||
return BlocConsumer<GroupBloc, GroupState>(
|
||||
listener: (context, groupState) {
|
||||
print('===== GroupBloc LISTENER =====');
|
||||
print('State type: ${groupState.runtimeType}');
|
||||
print('State: $groupState');
|
||||
|
||||
if (groupState is GroupOperationSuccess) {
|
||||
print('>>> GroupOperationSuccess: ${groupState.message}');
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(groupState.message),
|
||||
backgroundColor: Colors.green,
|
||||
),
|
||||
);
|
||||
// Recharger les groupes après une opération réussie
|
||||
context.read<GroupBloc>().add(LoadGroupsByUserId(user.id));
|
||||
} else if (groupState is GroupError) {
|
||||
print('>>> GroupError: ${groupState.message}');
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(groupState.message),
|
||||
@@ -82,54 +116,22 @@ class _GroupContentState extends State<GroupContent> {
|
||||
}
|
||||
},
|
||||
builder: (context, groupState) {
|
||||
print('===== GroupBloc BUILDER =====');
|
||||
print('State type: ${groupState.runtimeType}');
|
||||
print('State: $groupState');
|
||||
|
||||
// TEST: Afficher le type exact
|
||||
if (groupState is GroupsLoaded) {
|
||||
print('✓✓✓ GroupsLoaded détecté ! ✓✓✓');
|
||||
print('Nombre de groupes: ${groupState.groups.length}');
|
||||
for (var i = 0; i < groupState.groups.length; i++) {
|
||||
print(' Groupe $i: ${groupState.groups[i].name}');
|
||||
}
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
body: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
context.read<GroupBloc>().add(LoadGroupsByUserId(user.id));
|
||||
},
|
||||
child: SingleChildScrollView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// En-tête
|
||||
Text(
|
||||
'Mes groupes',
|
||||
style: TextStyle(
|
||||
fontSize: 28,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Theme.of(context).brightness == Brightness.dark
|
||||
? Colors.white
|
||||
: Colors.black,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Discutez avec les participants de vos voyages',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Contenu principal
|
||||
if (groupState is GroupLoading)
|
||||
_buildLoadingState()
|
||||
else if (groupState is GroupError)
|
||||
_buildErrorState(groupState.message, user.id)
|
||||
else if (groupState is GroupsLoaded)
|
||||
groupState.groups.isEmpty
|
||||
? _buildEmptyState()
|
||||
: _buildGroupGrid(groupState.groups)
|
||||
else
|
||||
_buildEmptyState(),
|
||||
|
||||
const SizedBox(height: 80),
|
||||
],
|
||||
),
|
||||
),
|
||||
body: SafeArea(
|
||||
child: _buildContent(groupState, user.id),
|
||||
),
|
||||
);
|
||||
},
|
||||
@@ -138,16 +140,174 @@ class _GroupContentState extends State<GroupContent> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLoadingState() {
|
||||
return const Center(
|
||||
Widget _buildContent(GroupState groupState, String userId) {
|
||||
print('===== _buildContent =====');
|
||||
print('State type: ${groupState.runtimeType}');
|
||||
|
||||
if (groupState is GroupLoading) {
|
||||
print('>>> Affichage: Loading');
|
||||
return const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
CircularProgressIndicator(),
|
||||
SizedBox(height: 16),
|
||||
Text('Chargement des groupes...'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (groupState is GroupError) {
|
||||
print('>>> Affichage: Error');
|
||||
return _buildErrorState(groupState.message, userId);
|
||||
}
|
||||
|
||||
if (groupState is GroupsLoaded) {
|
||||
print('>>> Affichage: GroupsLoaded');
|
||||
print('Groupes: ${groupState.groups.length}');
|
||||
|
||||
if (groupState.groups.isEmpty) {
|
||||
print('>>> Affichage: Empty');
|
||||
return _buildEmptyState();
|
||||
}
|
||||
|
||||
print('>>> Affichage: Liste des groupes');
|
||||
return _buildGroupsList(groupState.groups, userId);
|
||||
}
|
||||
|
||||
print('>>> Affichage: Initial/Unknown');
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Text('État inconnu'),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
print('>>> Bouton refresh cliqué');
|
||||
context.read<GroupBloc>().add(LoadGroupsByUserId(userId));
|
||||
},
|
||||
child: const Text('Charger les groupes'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildGroupsList(List<Group> groups, String userId) {
|
||||
print('===== _buildGroupsList =====');
|
||||
print('Nombre de groupes à afficher: ${groups.length}');
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
print('>>> Pull to refresh');
|
||||
context.read<GroupBloc>().add(LoadGroupsByUserId(userId));
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
},
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
const Text(
|
||||
'Mes groupes',
|
||||
style: TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Discutez avec les participants',
|
||||
style: TextStyle(fontSize: 14, color: Colors.grey[600]),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// IMPORTANT: Utiliser un simple Column au lieu de GridView pour tester
|
||||
...groups.map((group) {
|
||||
print('Création widget pour: ${group.name}');
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
child: _buildSimpleGroupCard(group),
|
||||
);
|
||||
}).toList(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSimpleGroupCard(Group group) {
|
||||
print('===== _buildSimpleGroupCard: ${group.name} =====');
|
||||
|
||||
try {
|
||||
final colors = [Colors.blue, Colors.purple, Colors.green, Colors.orange];
|
||||
final color = colors[group.name.hashCode.abs() % colors.length];
|
||||
|
||||
// Membres de manière simple
|
||||
String memberInfo = '${group.members.length} membre(s)';
|
||||
if (group.members.isNotEmpty) {
|
||||
final names = group.members
|
||||
.take(2)
|
||||
.map((m) => m.pseudo.isNotEmpty ? m.pseudo : m.firstName)
|
||||
.join(', ');
|
||||
memberInfo += '\n$names';
|
||||
}
|
||||
|
||||
print('Card créée avec succès');
|
||||
|
||||
return Card(
|
||||
elevation: 2,
|
||||
child: ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: color,
|
||||
child: const Icon(Icons.group, color: Colors.white),
|
||||
),
|
||||
title: Text(
|
||||
group.name,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
subtitle: Text(memberInfo),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: () {
|
||||
print('Tap sur: ${group.name}');
|
||||
_openGroupChat(group);
|
||||
},
|
||||
),
|
||||
);
|
||||
} catch (e, stackTrace) {
|
||||
print('ERREUR dans _buildSimpleGroupCard: $e');
|
||||
print('StackTrace: $stackTrace');
|
||||
return Card(
|
||||
color: Colors.red[100],
|
||||
child: const ListTile(
|
||||
leading: Icon(Icons.error, color: Colors.red),
|
||||
title: Text('Erreur d\'affichage'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildEmptyState() {
|
||||
print('===== _buildEmptyState =====');
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(32),
|
||||
child: CircularProgressIndicator(),
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.forum_outlined, size: 80, color: Colors.grey[400]),
|
||||
const SizedBox(height: 16),
|
||||
const Text('Aucun groupe', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Les groupes sont créés automatiquement',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(fontSize: 14, color: Colors.grey[600]),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildErrorState(String error, String userId) {
|
||||
print('===== _buildErrorState =====');
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
@@ -156,22 +316,13 @@ class _GroupContentState extends State<GroupContent> {
|
||||
children: [
|
||||
const Icon(Icons.error, size: 64, color: Colors.red),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'Erreur lors du chargement des groupes',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
const Text('Erreur', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500)),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
error,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
||||
),
|
||||
Text(error, textAlign: TextAlign.center, style: const TextStyle(fontSize: 12)),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
print('>>> Bouton réessayer cliqué');
|
||||
context.read<GroupBloc>().add(LoadGroupsByUserId(userId));
|
||||
},
|
||||
icon: const Icon(Icons.refresh),
|
||||
@@ -183,175 +334,32 @@ class _GroupContentState extends State<GroupContent> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmptyState() {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.forum_outlined,
|
||||
size: 80,
|
||||
color: Colors.grey[400],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'Aucun groupe',
|
||||
style: TextStyle(
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Les groupes sont créés automatiquement lorsque vous créez ou rejoignez un voyage',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
void _openGroupChat(Group group) {
|
||||
print('===== _openGroupChat: ${group.name} =====');
|
||||
print('Group ID: ${group.id}');
|
||||
print('Group members: ${group.members.length}');
|
||||
|
||||
Widget _buildGroupGrid(List<Group> groups) {
|
||||
return GridView.builder(
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 2,
|
||||
crossAxisSpacing: 12,
|
||||
mainAxisSpacing: 12,
|
||||
childAspectRatio: 0.85,
|
||||
),
|
||||
itemCount: groups.length,
|
||||
itemBuilder: (context, index) => _buildGroupCard(groups[index]),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildGroupCard(Group group) {
|
||||
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
|
||||
final colors = [
|
||||
Colors.blue,
|
||||
Colors.purple,
|
||||
Colors.green,
|
||||
Colors.orange,
|
||||
Colors.teal,
|
||||
Colors.pink,
|
||||
];
|
||||
final color = colors[group.name.hashCode.abs() % colors.length];
|
||||
|
||||
return Card(
|
||||
elevation: 3,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: InkWell(
|
||||
onTap: () => _openGroupChat(group),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [
|
||||
color.withOpacity(0.1),
|
||||
color.withOpacity(0.05),
|
||||
],
|
||||
),
|
||||
try {
|
||||
// Afficher juste un message, pas de navigation pour l'instant
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Chat: ${group.name} (${group.members.length} membres)'),
|
||||
duration: const Duration(seconds: 2),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Avatar du groupe
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withOpacity(0.2),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Icon(
|
||||
Icons.group,
|
||||
color: color,
|
||||
size: 32,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const Spacer(),
|
||||
// TODO: Navigation vers la page de chat
|
||||
// Navigator.push(
|
||||
// context,
|
||||
// MaterialPageRoute(
|
||||
// builder: (context) => GroupChatPage(group: group),
|
||||
// ),
|
||||
// );
|
||||
|
||||
// Nom du groupe
|
||||
Text(
|
||||
group.name,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: isDarkMode ? Colors.white : Colors.black87,
|
||||
),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
|
||||
// Nombre de membres
|
||||
Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.people,
|
||||
size: 16,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
'${group.members.length} membre${group.members.length > 1 ? 's' : ''}',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 4),
|
||||
|
||||
// Afficher les premiers membres
|
||||
if (group.members.isNotEmpty)
|
||||
Text(
|
||||
group.members.take(3).map((m) => m.pseudo).join(', ') +
|
||||
(group.members.length > 3 ? '...' : ''),
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: Colors.grey[500],
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _openGroupChat(Group group) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Ouverture du chat: ${group.name}'),
|
||||
duration: const Duration(seconds: 1),
|
||||
),
|
||||
);
|
||||
|
||||
// TODO: Navigation vers la page de chat
|
||||
// Navigator.push(
|
||||
// context,
|
||||
// MaterialPageRoute(
|
||||
// builder: (context) => GroupChatPage(group: group),
|
||||
// ),
|
||||
// );
|
||||
} catch (e) {
|
||||
print('ERREUR openGroupChat: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -245,7 +245,6 @@ class _HomeContentState extends State<HomeContent> {
|
||||
final color = colors[trip.title.hashCode.abs() % colors.length];
|
||||
|
||||
final isDarkMode = Theme.of(context).brightness == Brightness.dark;
|
||||
final textColor = isDarkMode ? Colors.white : Colors.black;
|
||||
final secondaryTextColor = isDarkMode ? Colors.white70 : Colors.grey[700];
|
||||
final iconColor = isDarkMode ? Colors.white70 : Colors.grey[600];
|
||||
|
||||
|
||||
@@ -11,21 +11,17 @@ class GroupRepository {
|
||||
return _groupsCollection.doc(groupId).collection('members');
|
||||
}
|
||||
|
||||
// Créer un groupe avec ses membres (avec ID du trip)
|
||||
Future<String> createGroupWithMembers({
|
||||
required Group group,
|
||||
required List<GroupMember> members,
|
||||
}) async {
|
||||
try {
|
||||
return await _firestore.runTransaction<String>((transaction) async {
|
||||
// Créer le document avec un ID généré
|
||||
final groupRef = _groupsCollection.doc();
|
||||
|
||||
// Ajouter l'ID dans les données
|
||||
final groupData = group.toMap();
|
||||
transaction.set(groupRef, groupData);
|
||||
|
||||
// Ajouter tous les membres
|
||||
for (var member in members) {
|
||||
final memberRef = groupRef.collection('members').doc(member.userId);
|
||||
transaction.set(memberRef, member.toMap());
|
||||
@@ -38,38 +34,82 @@ class GroupRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// NOUVEAU : Récupérer les groupes où l'utilisateur est membre
|
||||
Stream<List<Group>> getGroupsByUserId(String userId) {
|
||||
return _groupsCollection.snapshots().asyncMap((snapshot) async {
|
||||
List<Group> userGroups = [];
|
||||
print('===== GroupRepository: getGroupsByUserId START =====');
|
||||
print('UserId recherché: $userId');
|
||||
|
||||
for (var groupDoc in snapshot.docs) {
|
||||
try {
|
||||
// Vérifier si l'utilisateur est dans la sous-collection members
|
||||
final memberDoc = await groupDoc.reference
|
||||
.collection('members')
|
||||
.doc(userId)
|
||||
.get();
|
||||
return _groupsCollection
|
||||
.snapshots()
|
||||
.asyncMap((snapshot) async {
|
||||
print('===== GroupRepository: Nouveau snapshot (${DateTime.now()}) =====');
|
||||
print('Nombre de documents: ${snapshot.docs.length}');
|
||||
|
||||
if (memberDoc.exists) {
|
||||
// Charger le groupe avec tous ses membres
|
||||
final group = Group.fromMap(
|
||||
groupDoc.data() as Map<String, dynamic>,
|
||||
groupDoc.id,
|
||||
);
|
||||
final members = await getGroupMembers(groupDoc.id);
|
||||
userGroups.add(group.copyWith(members: members));
|
||||
List<Group> userGroups = [];
|
||||
|
||||
for (var groupDoc in snapshot.docs) {
|
||||
try {
|
||||
final groupId = groupDoc.id;
|
||||
print('--- Vérification groupe: $groupId ---');
|
||||
|
||||
// Vérifier si l'utilisateur est membre
|
||||
final memberDoc = await groupDoc.reference
|
||||
.collection('members')
|
||||
.doc(userId)
|
||||
.get();
|
||||
|
||||
print('Membre existe dans $groupId: ${memberDoc.exists}');
|
||||
|
||||
if (memberDoc.exists) {
|
||||
print('✓ Utilisateur trouvé dans $groupId');
|
||||
|
||||
final groupData = groupDoc.data() as Map<String, dynamic>;
|
||||
final group = Group.fromMap(groupData, groupId);
|
||||
|
||||
final members = await getGroupMembers(groupId);
|
||||
print('${members.length} membres chargés pour $groupId');
|
||||
|
||||
userGroups.add(group.copyWith(members: members));
|
||||
} else {
|
||||
print('✗ Utilisateur NON membre de $groupId');
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
print('ERREUR groupe ${groupDoc.id}: $e');
|
||||
print('StackTrace: $stackTrace');
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('Erreur lors du traitement du groupe ${groupDoc.id}: $e');
|
||||
}
|
||||
}
|
||||
|
||||
return userGroups;
|
||||
});
|
||||
print('===== Retour: ${userGroups.length} groupes =====');
|
||||
return userGroups;
|
||||
})
|
||||
.distinct((prev, next) {
|
||||
// Comparer les listes pour éviter les doublons
|
||||
if (prev.length != next.length) {
|
||||
print('>>> Changement détecté: ${prev.length} -> ${next.length} groupes');
|
||||
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;
|
||||
|
||||
if (!identical) {
|
||||
print('>>> Changement détecté: IDs différents');
|
||||
} else {
|
||||
print('>>> Données identiques, émission ignorée');
|
||||
}
|
||||
|
||||
return identical;
|
||||
})
|
||||
.handleError((error, stackTrace) {
|
||||
print('ERREUR stream: $error');
|
||||
print('StackTrace: $stackTrace');
|
||||
return <Group>[];
|
||||
});
|
||||
}
|
||||
|
||||
// Récupérer un groupe par son ID avec ses membres
|
||||
Future<Group?> getGroupById(String groupId) async {
|
||||
try {
|
||||
final doc = await _groupsCollection.doc(groupId).get();
|
||||
@@ -85,7 +125,6 @@ class GroupRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// Récupérer un groupe par tripId
|
||||
Future<Group?> getGroupByTripId(String tripId) async {
|
||||
try {
|
||||
final querySnapshot = await _groupsCollection
|
||||
@@ -105,23 +144,26 @@ class GroupRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// Récupérer les membres d'un groupe
|
||||
Future<List<GroupMember>> getGroupMembers(String groupId) async {
|
||||
try {
|
||||
print('Chargement membres pour: $groupId');
|
||||
final snapshot = await _membersCollection(groupId).get();
|
||||
print('${snapshot.docs.length} membres trouvés');
|
||||
|
||||
return snapshot.docs
|
||||
.map((doc) => GroupMember.fromMap(
|
||||
doc.data() as Map<String, dynamic>,
|
||||
doc.id,
|
||||
))
|
||||
.map((doc) {
|
||||
return GroupMember.fromMap(
|
||||
doc.data() as Map<String, dynamic>,
|
||||
doc.id,
|
||||
);
|
||||
})
|
||||
.toList();
|
||||
} catch (e) {
|
||||
print('ERREUR getGroupMembers: $e');
|
||||
throw Exception('Erreur lors de la récupération des membres: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// Ajouter un membre
|
||||
Future<void> addMember(String groupId, GroupMember member) async {
|
||||
try {
|
||||
await _membersCollection(groupId).doc(member.userId).set(member.toMap());
|
||||
@@ -134,7 +176,6 @@ class GroupRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// Supprimer un membre
|
||||
Future<void> removeMember(String groupId, String userId) async {
|
||||
try {
|
||||
await _membersCollection(groupId).doc(userId).delete();
|
||||
@@ -147,7 +188,6 @@ class GroupRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// Mettre à jour un groupe
|
||||
Future<void> updateGroup(String groupId, Group group) async {
|
||||
try {
|
||||
await _groupsCollection.doc(groupId).update(
|
||||
@@ -158,7 +198,6 @@ class GroupRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// Supprimer un groupe
|
||||
Future<void> deleteGroup(String groupId) async {
|
||||
try {
|
||||
final membersSnapshot = await _membersCollection(groupId).get();
|
||||
@@ -172,7 +211,6 @@ class GroupRepository {
|
||||
}
|
||||
}
|
||||
|
||||
// Stream des membres en temps réel
|
||||
Stream<List<GroupMember>> watchGroupMembers(String groupId) {
|
||||
return _membersCollection(groupId).snapshots().map(
|
||||
(snapshot) => snapshot.docs
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
import 'package:google_sign_in_platform_interface/google_sign_in_platform_interface.dart';
|
||||
|
||||
class AuthService {
|
||||
|
||||
Reference in New Issue
Block a user