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

60 lines
1.7 KiB
Dart
Raw Permalink 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/entities/user_profile.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import '../../domain/usecase/get_profile_usecase.dart';
import '../../domain/usecase/update_profile_usecase.dart';
import '../event/edit_profile_event.dart';
import '../event/profile_event.dart';
import '../state/edit_profile_state.dart';
import '../state/profile_state.dart';
class EditProfileBloc
extends Bloc<EditProfileEvent, EditProfileState> {
final UpdateProfileUseCase updateProfileUseCase;
final GetProfileUseCase getProfileUseCase;
EditProfileBloc(this.updateProfileUseCase, this.getProfileUseCase)
: super(EditProfileState.initial()) {
on<EditProfileStarted>(_onStarted);
on<EditProfileSubmitted>(_onSubmitted);
}
Future<void> _onSubmitted(
EditProfileSubmitted event,
Emitter<EditProfileState> emit,
) async {
emit(state.copyWith(isSaving: true));
try {
final result = await updateProfileUseCase(event.profile);
if (result != null) {
emit(state.copyWith(isSaving: false, isSuccess: true));
} else {
emit(state.copyWith(isSaving: false, error: "Не удалось обновить профиль"));
}
} catch (e) {
emit(state.copyWith(isSaving: false, error: e.toString()));
}
}
Future<void> _onStarted(
EditProfileStarted event,
Emitter<EditProfileState> emit,
) async {
emit(state.copyWith(isSaving: true));
print("EDIT BLOC STARTED");
try {
final profile = await getProfileUseCase();
emit(state.copyWith(profile: profile, isSaving: false, isSuccess: false));
} catch (e) {
emit(state.copyWith(isSaving: false, error: e.toString()));
}
}
}