98 lines
3.1 KiB
Dart
98 lines
3.1 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:be_happy/domain/usecase/delete_profile_usecase.dart';
|
|
import 'package:be_happy/domain/usecase/upload_profile_photo_usecase.dart';
|
|
import 'package:be_happy/presentation/viewmodel/splash_bloc.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import '../../domain/entities/user_profile.dart';
|
|
import '../../domain/usecase/get_profile_usecase.dart';
|
|
import '../../domain/usecase/logout_usecase.dart';
|
|
import '../../domain/usecase/update_profile_usecase.dart';
|
|
import '../event/profile_event.dart';
|
|
import '../event/spalsh_event.dart';
|
|
import '../state/profile_state.dart';
|
|
|
|
class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
|
final GetProfileUseCase getProfileUseCase;
|
|
final UploadProfilePhotoUsecase uploadProfilePhotoUsecase;
|
|
final UpdateProfileUseCase updateProfileUseCase;
|
|
final DeleteProfileUsecase deleteProfileUsecase;
|
|
final LogoutUseCase logoutUseCase;
|
|
final SplashBloc splashBloc;
|
|
|
|
|
|
ProfileBloc(this.getProfileUseCase,
|
|
this.uploadProfilePhotoUsecase,
|
|
this.updateProfileUseCase,
|
|
this.deleteProfileUsecase,
|
|
this.logoutUseCase,
|
|
this.splashBloc)
|
|
: super(ProfileState.initial()) {
|
|
on<ProfileStarted>(_onStarted);
|
|
on<ProfileUpdated>(_onUpdated);
|
|
on<ProfilePhotoUpdated>(_onPhotoUploaded);
|
|
on<DeleteProfilePressed>(_onDeleteProfilePressed);
|
|
}
|
|
|
|
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) {
|
|
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()}"));
|
|
}
|
|
}
|
|
|
|
FutureOr<void> _onDeleteProfilePressed(DeleteProfilePressed event, Emitter<ProfileState> emit) async {
|
|
try {
|
|
|
|
await deleteProfileUsecase();
|
|
splashBloc.add(AuthLogoutRequested());
|
|
} catch (e) {
|
|
emit(state.copyWith(isLoading: false, error: "Ошибка удаления профиля: ${e.toString()}"));
|
|
}
|
|
}
|
|
}
|