162 lines
5.5 KiB
Dart
162 lines
5.5 KiB
Dart
/// BLoC pour la gestion du profil utilisateur
|
|
library profile_bloc;
|
|
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:injectable/injectable.dart';
|
|
import 'package:dio/dio.dart';
|
|
import '../../domain/usecases/get_profile.dart';
|
|
import '../../domain/usecases/update_profile.dart';
|
|
import '../../domain/repositories/profile_repository.dart';
|
|
import '../../../members/data/models/membre_complete_model.dart';
|
|
|
|
part 'profile_event.dart';
|
|
part 'profile_state.dart';
|
|
|
|
/// BLoC pour la gestion du profil (Clean Architecture)
|
|
@injectable
|
|
class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
|
final GetProfile _getProfile;
|
|
final UpdateProfile _updateProfile;
|
|
final IProfileRepository _repository; // Pour méthodes non-couvertes (getProfileByEmail)
|
|
|
|
ProfileBloc(
|
|
this._getProfile,
|
|
this._updateProfile,
|
|
this._repository,
|
|
) : super(const ProfileInitial()) {
|
|
on<LoadMe>(_onLoadMe);
|
|
on<LoadMyProfile>(_onLoadMyProfile);
|
|
on<UpdateMyProfile>(_onUpdateMyProfile);
|
|
on<ChangePassword>(_onChangePassword);
|
|
on<DeleteAccount>(_onDeleteAccount);
|
|
}
|
|
|
|
/// Charge le profil du membre connecté
|
|
Future<void> _onLoadMe(
|
|
LoadMe event,
|
|
Emitter<ProfileState> emit,
|
|
) async {
|
|
try {
|
|
emit(const ProfileLoading());
|
|
final membre = await _getProfile();
|
|
if (membre != null) {
|
|
emit(ProfileLoaded(membre));
|
|
} else {
|
|
emit(const ProfileNotFound());
|
|
}
|
|
} on DioException catch (e) {
|
|
if (e.type == DioExceptionType.cancel) return;
|
|
emit(ProfileError(_networkErrorMessage(e)));
|
|
} catch (e) {
|
|
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
|
emit(ProfileError('Erreur lors du chargement du profil : $e'));
|
|
}
|
|
}
|
|
|
|
/// Charge le profil par email (recherche)
|
|
/// Note: Cette méthode utilise directement le repository car elle n'a pas de use case dédié
|
|
Future<void> _onLoadMyProfile(
|
|
LoadMyProfile event,
|
|
Emitter<ProfileState> emit,
|
|
) async {
|
|
try {
|
|
emit(const ProfileLoading());
|
|
final membre = await _repository.getProfileByEmail(event.email);
|
|
if (membre != null) {
|
|
emit(ProfileLoaded(membre));
|
|
} else {
|
|
emit(const ProfileNotFound());
|
|
}
|
|
} on DioException catch (e) {
|
|
if (e.type == DioExceptionType.cancel) return;
|
|
emit(ProfileError(_networkErrorMessage(e)));
|
|
} catch (e) {
|
|
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
|
emit(ProfileError('Erreur lors du chargement du profil : $e'));
|
|
}
|
|
}
|
|
|
|
/// Met à jour le profil
|
|
Future<void> _onUpdateMyProfile(
|
|
UpdateMyProfile event,
|
|
Emitter<ProfileState> emit,
|
|
) async {
|
|
final currentState = state;
|
|
try {
|
|
if (currentState is ProfileLoaded) {
|
|
emit(ProfileUpdating(currentState.membre));
|
|
}
|
|
final updated = await _updateProfile(event.membreId, event.membre);
|
|
emit(ProfileUpdated(updated));
|
|
} on DioException catch (e) {
|
|
if (e.type == DioExceptionType.cancel) return;
|
|
if (currentState is ProfileLoaded) {
|
|
emit(ProfileLoaded(currentState.membre));
|
|
}
|
|
emit(ProfileError(_networkErrorMessage(e)));
|
|
} catch (e) {
|
|
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
|
emit(ProfileError('Erreur lors de la mise à jour du profil : $e'));
|
|
}
|
|
}
|
|
|
|
/// Change le mot de passe via Keycloak
|
|
Future<void> _onChangePassword(
|
|
ChangePassword event,
|
|
Emitter<ProfileState> emit,
|
|
) async {
|
|
final previousState = state;
|
|
try {
|
|
emit(const PasswordChanging());
|
|
await _repository.changePassword(event.membreId, event.oldPassword, event.newPassword);
|
|
emit(const PasswordChanged());
|
|
if (previousState is ProfileLoaded) emit(previousState);
|
|
} on DioException catch (e) {
|
|
if (e.type == DioExceptionType.cancel) return;
|
|
emit(ProfileError(_networkErrorMessage(e)));
|
|
if (previousState is ProfileLoaded) emit(previousState);
|
|
} catch (e) {
|
|
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
|
emit(ProfileError(e.toString().replaceFirst('Exception: ', '')));
|
|
if (previousState is ProfileLoaded) emit(previousState);
|
|
}
|
|
}
|
|
|
|
/// Supprime le compte (soft delete)
|
|
Future<void> _onDeleteAccount(
|
|
DeleteAccount event,
|
|
Emitter<ProfileState> emit,
|
|
) async {
|
|
try {
|
|
emit(const AccountDeleting());
|
|
await _repository.deleteAccount(event.membreId);
|
|
emit(const AccountDeleted());
|
|
} on DioException catch (e) {
|
|
if (e.type == DioExceptionType.cancel) return;
|
|
emit(ProfileError(_networkErrorMessage(e)));
|
|
} catch (e) {
|
|
if (e is DioException && e.type == DioExceptionType.cancel) return;
|
|
emit(ProfileError(e.toString().replaceFirst('Exception: ', '')));
|
|
}
|
|
}
|
|
|
|
String _networkErrorMessage(DioException e) {
|
|
switch (e.type) {
|
|
case DioExceptionType.connectionTimeout:
|
|
case DioExceptionType.sendTimeout:
|
|
case DioExceptionType.receiveTimeout:
|
|
return 'Délai de connexion dépassé.';
|
|
case DioExceptionType.badResponse:
|
|
final code = e.response?.statusCode;
|
|
if (code == 401) return 'Non autorisé. Veuillez vous reconnecter.';
|
|
if (code == 403) return 'Accès refusé.';
|
|
if (code == 404) return 'Profil non trouvé.';
|
|
if (code != null && code >= 500) return 'Erreur serveur.';
|
|
return 'Erreur de communication avec le serveur.';
|
|
default:
|
|
return 'Erreur réseau. Vérifiez votre connexion.';
|
|
}
|
|
}
|
|
}
|