feat: Implement group management features with Firestore integration and loading states
This commit is contained in:
@@ -1,27 +1,126 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:travel_mate/models/group.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../../providers/user_provider.dart';
|
||||
import '../../services/group_service.dart';
|
||||
|
||||
class GroupContent extends StatelessWidget {
|
||||
class GroupContent extends StatefulWidget {
|
||||
const GroupContent({super.key});
|
||||
|
||||
@override
|
||||
State<GroupContent> createState() => _GroupContentState();
|
||||
}
|
||||
|
||||
class _GroupContentState extends State<GroupContent> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Consumer<UserProvider>(
|
||||
builder: (context, userProvider, child) {
|
||||
final user = userProvider.currentUser;
|
||||
if (user == null || user.id == null) {
|
||||
return const Center(
|
||||
child: Text('Utilisateur non connecté'),
|
||||
);
|
||||
}
|
||||
|
||||
return StreamBuilder<List<Group>>(
|
||||
stream: GroupService().getGroupsStream(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return _buildLoadingState();
|
||||
} else if (snapshot.hasError) {
|
||||
print('Erreur du stream: ${snapshot.error}');
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.group, size: 64, color: Colors.blue),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'Chat de Groupe',
|
||||
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
|
||||
),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
'Communiquez avec vos compagnons de voyage',
|
||||
style: TextStyle(fontSize: 16, color: Colors.grey),
|
||||
const Text('Erreur lors du chargement des groupes.'),
|
||||
const SizedBox(height: 8),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
// Forcer le rechargement du stream
|
||||
});
|
||||
},
|
||||
child: const Text('Réessayer'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final groups = snapshot.data ?? [];
|
||||
print("Groupes reçus: ${groups.length}");
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
setState() {};
|
||||
},
|
||||
child: SingleChildScrollView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'Vos Groupes',
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Theme.of(context).brightness == Brightness.dark ? Colors.white : Colors.black,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
groups.isEmpty ? _buildEmptyState() : _buildGroupList(groups),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLoadingState() {
|
||||
return const Center(
|
||||
child: Padding(padding: EdgeInsets.all(16.0),
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmptyState() {
|
||||
return const Center(
|
||||
child: Text(
|
||||
'Aucun groupe disponible. Créez ou rejoignez un voyage pour commencer à discuter!',
|
||||
style: TextStyle(fontSize: 16, color: Colors.grey),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildGroupList(List<Group> groups) {
|
||||
return Column(
|
||||
children: groups.map((group) => _buildGroupCard(group)).toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildGroupCard(Group group) {
|
||||
return Card(
|
||||
margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
|
||||
child: ListTile(
|
||||
leading: CircleAvatar(
|
||||
child: Text(group.name.isNotEmpty ? group.name[0] : '?'),
|
||||
),
|
||||
title: Text(group.name),
|
||||
subtitle: Text('${group.members.length} membres'),
|
||||
onTap: () {
|
||||
// Logique pour ouvrir le chat de groupe
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
28
lib/models/group.dart
Normal file
28
lib/models/group.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
import 'package:travel_mate/models/user.dart';
|
||||
|
||||
class Group {
|
||||
final String? id;
|
||||
final String name;
|
||||
final List<User> members;
|
||||
|
||||
Group({
|
||||
this.id,
|
||||
required this.name,
|
||||
required this.members,
|
||||
});
|
||||
|
||||
factory Group.fromMap(Map<String, dynamic> data, String documentId) {
|
||||
return Group(
|
||||
id: documentId,
|
||||
name: data['name'] ?? '',
|
||||
members: List<User>.from(data['members']?.map((member) => User.fromMap(member)) ?? []),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'name': name,
|
||||
'members': members.map((member) => member.toMap()).toList(),
|
||||
};
|
||||
}
|
||||
}
|
||||
13
lib/models/message.dart
Normal file
13
lib/models/message.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
class Message {
|
||||
final String text;
|
||||
final DateTime timestamp;
|
||||
final String senderId;
|
||||
final String senderName;
|
||||
|
||||
Message({
|
||||
required this.text,
|
||||
required this.timestamp,
|
||||
required this.senderId,
|
||||
required this.senderName,
|
||||
});
|
||||
}
|
||||
26
lib/services/group_service.dart
Normal file
26
lib/services/group_service.dart
Normal file
@@ -0,0 +1,26 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:travel_mate/models/group.dart';
|
||||
|
||||
class GroupService {
|
||||
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
||||
|
||||
Stream<List<Group>> getGroupsStream() {
|
||||
return _firestore.collection('groups').snapshots().map((snapshot) {
|
||||
return snapshot.docs.map((doc) {
|
||||
return Group.fromMap(doc.data(), doc.id);
|
||||
}).toList();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> createGroup(Group group) async {
|
||||
await _firestore.collection('groups').add(group.toMap());
|
||||
}
|
||||
|
||||
Future<void> updateGroup(Group group) async {
|
||||
await _firestore.collection('groups').doc(group.id).update(group.toMap());
|
||||
}
|
||||
|
||||
Future<void> deleteGroup(String groupId) async {
|
||||
await _firestore.collection('groups').doc(groupId).delete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user