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:
Dayron
2025-10-14 12:10:42 +02:00
parent c4588a65c0
commit 72ddb58a11
27 changed files with 1864 additions and 791 deletions

View File

@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../providers/user_provider.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../blocs/user/user_bloc.dart';
import '../../blocs/user/user_state.dart' as user_state;
import '../../blocs/user/user_event.dart' as user_event;
import '../../services/auth_service.dart';
class ProfileContent extends StatelessWidget {
@@ -8,14 +10,22 @@ class ProfileContent extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<UserProvider>(
builder: (context, userProvider, child) {
final user = userProvider.currentUser;
return BlocBuilder<UserBloc, user_state.UserState>(
builder: (context, state) {
if (state is user_state.UserLoading) {
return Center(child: CircularProgressIndicator());
}
if (user == null) {
if (state is user_state.UserError) {
return Center(child: Text('Erreur: ${state.message}'));
}
if (state is! user_state.UserLoaded) {
return Center(child: Text('Aucun utilisateur connecté'));
}
final user = state.user;
return Column(
children: [
// Section titre
@@ -62,7 +72,7 @@ class ProfileContent extends StatelessWidget {
// Nom complet
Text(
user.fullName,
'${user.prenom} ${user.nom ?? ''}',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
@@ -96,7 +106,7 @@ class ProfileContent extends StatelessWidget {
title: Text('Changer le mot de passe'),
trailing: Icon(Icons.arrow_forward_ios),
onTap: () {
_showChangePasswordDialog(context);
_showChangePasswordDialog(context, user);
},
),
@@ -107,7 +117,7 @@ class ProfileContent extends StatelessWidget {
title: Text('Supprimer le compte'),
trailing: Icon(Icons.arrow_forward_ios),
onTap: () {
_showDeleteAccountDialog(context);
_showDeleteAccountDialog(context, user);
},
),
],
@@ -116,13 +126,13 @@ class ProfileContent extends StatelessWidget {
);
}
void _showEditProfileDialog(BuildContext context, user) {
void _showEditProfileDialog(BuildContext context, user_state.UserModel user) {
final nomController = TextEditingController(text: user.nom);
final prenomController = TextEditingController(text: user.prenom);
showDialog(
context: context,
builder: (BuildContext context) {
builder: (BuildContext dialogContext) {
return AlertDialog(
title: Text('Modifier le profil'),
content: Column(
@@ -141,37 +151,26 @@ class ProfileContent extends StatelessWidget {
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
onPressed: () => Navigator.of(dialogContext).pop(),
child: Text('Annuler'),
),
TextButton(
onPressed: () async {
if (prenomController.text.trim().isNotEmpty &&
nomController.text.trim().isNotEmpty) {
final updatedUser = user.copyWith(
nom: nomController.text.trim(),
prenom: prenomController.text.trim(),
onPressed: () {
if (prenomController.text.trim().isNotEmpty) {
context.read<UserBloc>().add(
user_event.UserUpdated({
'prenom': prenomController.text.trim(),
'nom': nomController.text.trim(),
}),
);
final success = await Provider.of<UserProvider>(context,
listen: false)
.updateUser(updatedUser);
Navigator.of(context).pop();
if (success) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Profil mis à jour !'),
backgroundColor: Colors.green),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Erreur lors de la mise à jour'),
backgroundColor: Colors.red),
);
}
Navigator.of(dialogContext).pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Profil mis à jour !'),
backgroundColor: Colors.green,
),
);
}
},
child: Text('Sauvegarder'),
@@ -182,7 +181,7 @@ class ProfileContent extends StatelessWidget {
);
}
void _showChangePasswordDialog(BuildContext context) {
void _showChangePasswordDialog(BuildContext context, user_state.UserModel user) {
final currentPasswordController = TextEditingController();
final newPasswordController = TextEditingController();
final confirmPasswordController = TextEditingController();
@@ -190,7 +189,7 @@ class ProfileContent extends StatelessWidget {
showDialog(
context: context,
builder: (BuildContext context) {
builder: (BuildContext dialogContext) {
return AlertDialog(
title: Text('Changer le mot de passe'),
content: Column(
@@ -211,15 +210,13 @@ class ProfileContent extends StatelessWidget {
TextField(
controller: confirmPasswordController,
obscureText: true,
decoration: InputDecoration(
labelText: 'Confirmer le mot de passe',
),
decoration: InputDecoration(labelText: 'Confirmer le mot de passe'),
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
onPressed: () => Navigator.of(dialogContext).pop(),
child: Text('Annuler'),
),
TextButton(
@@ -229,8 +226,9 @@ class ProfileContent extends StatelessWidget {
confirmPasswordController.text.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Tous les champs sont requis'),
backgroundColor: Colors.red),
content: Text('Tous les champs sont requis'),
backgroundColor: Colors.red,
),
);
return;
}
@@ -238,42 +236,33 @@ class ProfileContent extends StatelessWidget {
if (newPasswordController.text != confirmPasswordController.text) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Les mots de passe ne correspondent pas'),
backgroundColor: Colors.red),
);
return;
}
if (newPasswordController.text.length < 8) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content:
Text('Le mot de passe doit contenir au moins 8 caractères'),
backgroundColor: Colors.red),
content: Text('Les mots de passe ne correspondent pas'),
backgroundColor: Colors.red,
),
);
return;
}
try {
final user = Provider.of<UserProvider>(context, listen: false)
.currentUser;
await authService.resetPasswordFromCurrentPassword(
currentPassword: currentPasswordController.text,
newPassword: newPasswordController.text,
email: user!.email,
email: user.email,
);
Navigator.of(context).pop();
Navigator.of(dialogContext).pop();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Mot de passe changé !'),
backgroundColor: Colors.green),
content: Text('Mot de passe changé !'),
backgroundColor: Colors.green,
),
);
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Erreur: Mot de passe actuel incorrect'),
backgroundColor: Colors.red),
content: Text('Erreur: Mot de passe actuel incorrect'),
backgroundColor: Colors.red,
),
);
}
},
@@ -285,13 +274,13 @@ class ProfileContent extends StatelessWidget {
);
}
void _showDeleteAccountDialog(BuildContext context) {
void _showDeleteAccountDialog(BuildContext context, user_state.UserModel user) {
final passwordController = TextEditingController();
final authService = AuthService();
showDialog(
context: context,
builder: (BuildContext context) {
builder: (BuildContext dialogContext) {
return AlertDialog(
title: Text('Supprimer le compte'),
content: Column(
@@ -313,30 +302,19 @@ class ProfileContent extends StatelessWidget {
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
onPressed: () => Navigator.of(dialogContext).pop(),
child: Text('Annuler'),
),
TextButton(
onPressed: () async {
if (passwordController.text.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Mot de passe requis'),
backgroundColor: Colors.red),
);
return;
}
try {
final user = Provider.of<UserProvider>(context, listen: false)
.currentUser;
await authService.deleteAccount(
password: passwordController.text,
email: user!.email,
email: user.email,
);
Navigator.of(context).pop();
Provider.of<UserProvider>(context, listen: false).logout();
Navigator.of(dialogContext).pop();
context.read<UserBloc>().add(user_event.UserLoggedOut());
Navigator.pushNamedAndRemoveUntil(
context,
'/login',
@@ -345,9 +323,9 @@ class ProfileContent extends StatelessWidget {
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content:
Text('Erreur lors de la suppression: Mot de passe incorrect'),
backgroundColor: Colors.red),
content: Text('Erreur: Mot de passe incorrect'),
backgroundColor: Colors.red,
),
);
}
},