206 lines
6.5 KiB
Dart
206 lines
6.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:travel_mate/data/models/group.dart';
|
|
import '../../blocs/user/user_bloc.dart';
|
|
import '../../blocs/user/user_state.dart' as user_state;
|
|
import '../../blocs/group/group_bloc.dart';
|
|
import '../../blocs/group/group_state.dart';
|
|
import '../../blocs/group/group_event.dart';
|
|
|
|
class GroupContent extends StatefulWidget {
|
|
const GroupContent({super.key});
|
|
|
|
@override
|
|
State<GroupContent> createState() => _GroupContentState();
|
|
}
|
|
|
|
class _GroupContentState extends State<GroupContent> {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadGroupsIfUserLoaded();
|
|
}
|
|
|
|
void _loadGroupsIfUserLoaded() {
|
|
final userState = context.read<UserBloc>().state;
|
|
if (userState is user_state.UserLoaded) {
|
|
context.read<GroupBloc>().add(GroupLoadRequested(userId: userState.user.id));
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return BlocBuilder<UserBloc, user_state.UserState>(
|
|
builder: (context, userState) {
|
|
if (userState is user_state.UserLoading) {
|
|
return Scaffold(
|
|
body: Center(child: CircularProgressIndicator()),
|
|
);
|
|
}
|
|
|
|
if (userState is user_state.UserError) {
|
|
return Scaffold(
|
|
body: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(Icons.error, size: 64, color: Colors.red),
|
|
SizedBox(height: 16),
|
|
Text('Erreur: ${userState.message}'),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
if (userState is! user_state.UserLoaded) {
|
|
return Scaffold(
|
|
body: Center(child: Text('Utilisateur non connecté')),
|
|
);
|
|
}
|
|
|
|
final user = userState.user;
|
|
|
|
// Charger les groupes si ce n'est pas déjà fait
|
|
if (context.read<GroupBloc>().state is GroupInitial) {
|
|
context.read<GroupBloc>().add(GroupLoadRequested(userId: user.id));
|
|
}
|
|
|
|
return BlocConsumer<GroupBloc, GroupState>(
|
|
listener: (context, groupState) {
|
|
if (groupState is GroupOperationSuccess) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(groupState.message),
|
|
backgroundColor: Colors.green,
|
|
),
|
|
);
|
|
// Recharger les groupes
|
|
context.read<GroupBloc>().add(GroupLoadRequested(userId: user.id));
|
|
} else if (groupState is GroupError) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(groupState.message),
|
|
backgroundColor: Colors.red,
|
|
),
|
|
);
|
|
}
|
|
},
|
|
builder: (context, groupState) {
|
|
return Scaffold(
|
|
body: RefreshIndicator(
|
|
onRefresh: () async {
|
|
context.read<GroupBloc>().add(GroupLoadRequested(userId: user.id));
|
|
},
|
|
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),
|
|
|
|
if (groupState is GroupLoading)
|
|
_buildLoadingState()
|
|
else if (groupState is GroupError)
|
|
_buildErrorState(groupState.message, user.id)
|
|
else if (groupState is GroupLoaded)
|
|
groupState.groups.isEmpty
|
|
? _buildEmptyState()
|
|
: _buildGroupList(groupState.groups)
|
|
else
|
|
_buildEmptyState(),
|
|
|
|
const SizedBox(height: 80),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _buildLoadingState() {
|
|
return const Center(
|
|
child: Padding(
|
|
padding: EdgeInsets.all(16.0),
|
|
child: CircularProgressIndicator(),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildErrorState(String error, String userId) {
|
|
return Center(
|
|
child: Padding(
|
|
padding: EdgeInsets.all(32),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Icon(Icons.error, size: 64, color: Colors.red),
|
|
const SizedBox(height: 16),
|
|
const Text('Erreur lors du chargement des groupes.'),
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
error,
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(fontSize: 12, color: Colors.grey),
|
|
),
|
|
const SizedBox(height: 16),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
context.read<GroupBloc>().add(GroupLoadRequested(userId: userId));
|
|
},
|
|
child: const Text('Réessayer'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
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
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|