Files
be_happy_public/lib/presentation/viewmodel/profile_bloc.dart
2026-05-12 12:02:40 +03:00

76 lines
2.3 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<ProfileEvent, ProfileState> {
final GetProfileUseCase getProfileUseCase;
final UploadProfilePhotoUsecase uploadProfilePhotoUsecase;
final UpdateProfileUseCase updateProfileUseCase;
ProfileBloc(this.getProfileUseCase,
this.uploadProfilePhotoUsecase,
this.updateProfileUseCase)
: super(ProfileState.initial()) {
on<ProfileStarted>(_onStarted);
on<ProfileUpdated>(_onUpdated);
on<ProfilePhotoUpdated>(_onPhotoUploaded);
}
Future<void> _onStarted(
ProfileStarted event,
Emitter<ProfileState> 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<void> _onUpdated(
ProfileUpdated event,
Emitter<ProfileState> emit,
) async {
final profile = await getProfileUseCase();
emit(state.copyWith(profile: profile));
}
Future<void> _onPhotoUploaded(
ProfilePhotoUpdated event,
Emitter<ProfileState> 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()}"));
}
}
}