Refactor user and theme management to use BLoC pattern; remove provider classes and integrate new services for user and group functionalities
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
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';
|
||||
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});
|
||||
@@ -12,51 +15,82 @@ class GroupContent extends StatefulWidget {
|
||||
}
|
||||
|
||||
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 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 BlocBuilder<UserBloc, user_state.UserState>(
|
||||
builder: (context, userState) {
|
||||
if (userState is user_state.UserLoading) {
|
||||
return Scaffold(
|
||||
body: Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
|
||||
return StreamBuilder<List<Group>>(
|
||||
stream: GroupService().getGroupsStreamByUser(user.id!), // Filtrer par utilisateur
|
||||
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: [
|
||||
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'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
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}'),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final groups = snapshot.data ?? [];
|
||||
print("Groupes reçus pour l'utilisateur ${user.id}: ${groups.length}");
|
||||
if (userState is! user_state.UserLoaded) {
|
||||
return Scaffold(
|
||||
body: Center(child: Text('Utilisateur non connecté')),
|
||||
);
|
||||
}
|
||||
|
||||
return RefreshIndicator(
|
||||
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 {
|
||||
setState(() {});
|
||||
context.read<GroupBloc>().add(GroupLoadRequested(userId: user.id));
|
||||
},
|
||||
child: SingleChildScrollView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
@@ -69,31 +103,74 @@ class _GroupContentState extends State<GroupContent> {
|
||||
style: TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Theme.of(context).brightness == Brightness.dark ? Colors.white : Colors.black,
|
||||
color: Theme.of(context).brightness == Brightness.dark
|
||||
? Colors.white
|
||||
: Colors.black,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
groups.isEmpty ? _buildEmptyState() : _buildGroupList(groups),
|
||||
|
||||
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: 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(
|
||||
|
||||
Reference in New Issue
Block a user