- Config Spec-Kit pour Spec-Driven Development - CONSTITUTION.md + .specify/memory/constitution.md - Commandes Cursor /speckit.*, règles projet - Mission: associations + mutuelles d'épargne et de financement - .gitignore: versionner config spec-kit unionflow Made-with: Cursor
78 lines
2.5 KiB
Dart
78 lines
2.5 KiB
Dart
library profile_bloc;
|
|
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:dio/dio.dart';
|
|
import '../../../data/repositories/profile_repository.dart';
|
|
import '../../../members/data/models/membre_complete_model.dart';
|
|
|
|
part 'profile_event.dart';
|
|
part 'profile_state.dart';
|
|
|
|
class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
|
final ProfileRepository _repository;
|
|
|
|
ProfileBloc(this._repository) : super(const ProfileInitial()) {
|
|
on<LoadMyProfile>(_onLoadMyProfile);
|
|
on<UpdateMyProfile>(_onUpdateMyProfile);
|
|
}
|
|
|
|
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) {
|
|
emit(ProfileError(_networkErrorMessage(e)));
|
|
} catch (e) {
|
|
emit(ProfileError('Erreur lors du chargement du profil : $e'));
|
|
}
|
|
}
|
|
|
|
Future<void> _onUpdateMyProfile(
|
|
UpdateMyProfile event,
|
|
Emitter<ProfileState> emit,
|
|
) async {
|
|
final currentState = state;
|
|
try {
|
|
if (currentState is ProfileLoaded) {
|
|
emit(ProfileUpdating(currentState.membre));
|
|
}
|
|
final updated = await _repository.updateProfile(event.membreId, event.membre);
|
|
emit(ProfileUpdated(updated));
|
|
} on DioException catch (e) {
|
|
if (currentState is ProfileLoaded) {
|
|
emit(ProfileLoaded(currentState.membre));
|
|
}
|
|
emit(ProfileError(_networkErrorMessage(e)));
|
|
} catch (e) {
|
|
emit(ProfileError('Erreur lors de la mise à jour du profil : $e'));
|
|
}
|
|
}
|
|
|
|
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.';
|
|
}
|
|
}
|
|
}
|