import 'package:be_happy/domain/usecase/upload_profile_photo_usecase.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import '../../domain/entities/user_profile.dart'; import '../../domain/usecase/get_profile_usecase.dart'; import '../../domain/usecase/update_profile_usecase.dart'; import '../event/profile_event.dart'; import '../state/profile_state.dart'; class ProfileBloc extends Bloc { final GetProfileUseCase getProfileUseCase; final UploadProfilePhotoUsecase uploadProfilePhotoUsecase; final UpdateProfileUseCase updateProfileUseCase; ProfileBloc(this.getProfileUseCase, this.uploadProfilePhotoUsecase, this.updateProfileUseCase) : super(ProfileState.initial()) { on(_onStarted); on(_onUpdated); on(_onPhotoUploaded); } Future _onStarted( ProfileStarted event, Emitter emit, ) async { emit(state.copyWith(isLoading: true)); print("_onStarted method was start"); try { final profile = await getProfileUseCase(); print(profile.name); emit(state.copyWith(isLoading: false, profile: profile)); } catch (e) { emit(state.copyWith(isLoading: false, error: "STARTED: ${e.toString()}")); } } Future _onUpdated( ProfileUpdated event, Emitter emit, ) async { final profile = await getProfileUseCase(); emit(state.copyWith(profile: profile)); } Future _onPhotoUploaded( ProfilePhotoUpdated event, Emitter emit, ) async { emit(state.copyWith(isLoading: true)); try { final avatarId = await uploadProfilePhotoUsecase(event.imageFile); if (avatarId != null) { // Отправляем PATCH запрос с новым avatarId await updateProfileUseCase(UserProfile( name: '', birthDate: '', phone: '', email: '', avatarId: avatarId, )); } final updatedProfile = await getProfileUseCase(); emit(state.copyWith(isLoading: false, profile: updatedProfile, error: null)); } catch (e) { emit(state.copyWith(isLoading: false, error: "Ошибка загрузки: ${e.toString()}")); } } }