Modifs MVVM
This commit is contained in:
@ -1,46 +0,0 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../models/user_model.dart';
|
||||
|
||||
class LocalAuthProvider with ChangeNotifier {
|
||||
UserModel? _currentUser;
|
||||
|
||||
UserModel? get currentUser => _currentUser;
|
||||
String? get role => _currentUser?.role;
|
||||
|
||||
void setUser(UserModel user) {
|
||||
_currentUser = user;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void clearUser() {
|
||||
_currentUser = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<UserCredential> signInWithEmailAndPassword(
|
||||
String email, String password) async {
|
||||
try {
|
||||
UserCredential userCredential = await FirebaseAuth.instance
|
||||
.signInWithEmailAndPassword(email: email, password: password);
|
||||
|
||||
DocumentSnapshot userDoc = await FirebaseFirestore.instance
|
||||
.collection('users')
|
||||
.doc(userCredential.user!.uid)
|
||||
.get();
|
||||
|
||||
if (userDoc.exists) {
|
||||
setUser(UserModel.fromMap(
|
||||
userDoc.data() as Map<String, dynamic>, userDoc.id));
|
||||
} else {
|
||||
throw FirebaseAuthException(
|
||||
code: 'user-not-found',
|
||||
message: "Aucune donnée utilisateur trouvée.");
|
||||
}
|
||||
return userCredential;
|
||||
} on FirebaseAuthException catch (e) {
|
||||
throw FirebaseAuthException(code: e.code, message: e.message);
|
||||
}
|
||||
}
|
||||
}
|
108
em2rp/lib/providers/local_user_provider.dart
Normal file
108
em2rp/lib/providers/local_user_provider.dart
Normal file
@ -0,0 +1,108 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import '../models/user_model.dart';
|
||||
import '../utils/firebase_storage_manager.dart';
|
||||
|
||||
class LocalUserProvider with ChangeNotifier {
|
||||
UserModel? _currentUser;
|
||||
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
||||
final FirebaseStorageManager _storageManager = FirebaseStorageManager();
|
||||
|
||||
UserModel? get currentUser => _currentUser;
|
||||
String? get uid => _currentUser?.uid;
|
||||
String? get firstName => _currentUser?.firstName;
|
||||
String? get lastName => _currentUser?.lastName;
|
||||
String? get role => _currentUser?.role ?? 'USER';
|
||||
String? get profilePhotoUrl => _currentUser?.profilePhotoUrl;
|
||||
String? get email => _currentUser?.email;
|
||||
String? get phoneNumber => _currentUser?.phoneNumber;
|
||||
|
||||
/// Charge les données de l'utilisateur actuel
|
||||
Future<void> loadUserData() async {
|
||||
if (_auth.currentUser == null) return;
|
||||
DocumentSnapshot userDoc =
|
||||
await _firestore.collection('users').doc(_auth.currentUser!.uid).get();
|
||||
if (userDoc.exists) {
|
||||
setUser(UserModel.fromMap(
|
||||
userDoc.data() as Map<String, dynamic>, userDoc.id));
|
||||
}
|
||||
}
|
||||
|
||||
/// Met à jour l'utilisateur
|
||||
void setUser(UserModel user) {
|
||||
_currentUser = user;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Efface les données utilisateur
|
||||
void clearUser() {
|
||||
_currentUser = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Mise à jour des informations utilisateur
|
||||
Future<void> updateUserData(
|
||||
{String? firstName, String? lastName, String? phoneNumber}) async {
|
||||
if (_currentUser == null) return;
|
||||
try {
|
||||
await _firestore.collection('users').doc(_currentUser!.uid).set({
|
||||
'firstName': firstName ?? _currentUser!.firstName,
|
||||
'lastName': lastName ?? _currentUser!.lastName,
|
||||
'phone': phoneNumber ?? _currentUser!.phoneNumber,
|
||||
}, SetOptions(merge: true));
|
||||
|
||||
_currentUser = _currentUser!.copyWith(
|
||||
firstName: firstName ?? _currentUser!.firstName,
|
||||
lastName: lastName ?? _currentUser!.lastName,
|
||||
phoneNumber: phoneNumber ?? _currentUser!.phoneNumber,
|
||||
);
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
debugPrint('Erreur mise à jour utilisateur : $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Changement de photo de profil
|
||||
Future<void> changeProfilePicture(XFile image) async {
|
||||
if (_currentUser == null) return;
|
||||
try {
|
||||
String? newProfilePhotoUrl = await _storageManager.sendProfilePicture(
|
||||
imageFile: image,
|
||||
uid: _currentUser!.uid,
|
||||
);
|
||||
if (newProfilePhotoUrl != null) {
|
||||
_firestore
|
||||
.collection('users')
|
||||
.doc(_currentUser!.uid)
|
||||
.update({'profilePhotoUrl': newProfilePhotoUrl});
|
||||
_currentUser =
|
||||
_currentUser!.copyWith(profilePhotoUrl: newProfilePhotoUrl);
|
||||
notifyListeners();
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('Erreur mise à jour photo de profil : $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Connexion
|
||||
Future<UserCredential> signInWithEmailAndPassword(
|
||||
String email, String password) async {
|
||||
try {
|
||||
UserCredential userCredential = await _auth.signInWithEmailAndPassword(
|
||||
email: email, password: password);
|
||||
await loadUserData();
|
||||
return userCredential;
|
||||
} catch (e) {
|
||||
throw FirebaseAuthException(code: 'login-failed', message: e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/// Déconnexion
|
||||
Future<void> signOut() async {
|
||||
await _auth.signOut();
|
||||
clearUser();
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class UserProvider extends ChangeNotifier {
|
||||
String? _uid;
|
||||
String? _firstName;
|
||||
String? _lastName;
|
||||
String? _role;
|
||||
String? _profilePictureUrl;
|
||||
String? _email;
|
||||
String? _phoneNumber;
|
||||
|
||||
String? get uid => _uid;
|
||||
String? get firstName => _firstName;
|
||||
String? get lastName => _lastName;
|
||||
String? get role => _role;
|
||||
String? get profilePhotoUrl => _profilePictureUrl;
|
||||
String? get email => _email;
|
||||
String? get phoneNumber => _phoneNumber;
|
||||
|
||||
void setUserData(Map<String, dynamic> userData, String uid) {
|
||||
_uid = uid;
|
||||
_firstName = userData['firstName'];
|
||||
_lastName = userData['lastName'];
|
||||
_role = userData['role'] ?? 'USER';
|
||||
if (userData['profilePhotoUrl'] != "") {
|
||||
_profilePictureUrl = userData['profilePhotoUrl'];
|
||||
}
|
||||
_email = userData['email'];
|
||||
_phoneNumber = userData['phoneNumber'];
|
||||
notifyListeners(); // Notify listeners that state has changed
|
||||
}
|
||||
|
||||
void clearUserData() {
|
||||
_uid = null;
|
||||
_firstName = null;
|
||||
_lastName = null;
|
||||
_role = null;
|
||||
_profilePictureUrl = null;
|
||||
_email = null;
|
||||
_phoneNumber = null;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void setUserFirstName(String text) {
|
||||
_firstName = text;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void setUserLastName(String text) {
|
||||
_lastName = text;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void setUserPhoneNumber(String text) {
|
||||
_phoneNumber = text;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
44
em2rp/lib/providers/users_provider.dart
Normal file
44
em2rp/lib/providers/users_provider.dart
Normal file
@ -0,0 +1,44 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../models/user_model.dart';
|
||||
import '../services/user_service.dart';
|
||||
|
||||
class UsersProvider with ChangeNotifier {
|
||||
final UserService _userService;
|
||||
List<UserModel> _users = [];
|
||||
bool _isLoading = false;
|
||||
|
||||
List<UserModel> get users => _users;
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
UsersProvider(this._userService);
|
||||
|
||||
/// Récupération de tous les utilisateurs
|
||||
Future<void> fetchUsers() async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
try {
|
||||
_users = await _userService.fetchUsers();
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// Mise à jour d'un utilisateur
|
||||
Future<void> updateUser(UserModel user) async {
|
||||
await _userService.updateUser(user);
|
||||
await fetchUsers();
|
||||
}
|
||||
|
||||
/// Suppression d'un utilisateur
|
||||
Future<void> deleteUser(String uid) async {
|
||||
await _userService.deleteUser(uid);
|
||||
await fetchUsers();
|
||||
}
|
||||
|
||||
/// Réinitialisation du mot de passe
|
||||
Future<void> resetPassword(String email) async {
|
||||
await _userService.resetPassword(email);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user