import 'package:flutter_bloc/flutter_bloc.dart'; import '../../../../core/resources/data_state.dart'; import '../../../../core/usecase/use_case.dart'; import '../../domain/use_cases/check_otp_usecase.dart'; import '../../domain/use_cases/login_usecase.dart'; import '../../domain/use_cases/logout_usecase.dart'; import '../../domain/use_cases/update_profile_usecase.dart'; import 'auth_event.dart'; import 'auth_state.dart'; import 'login_status.dart'; import 'otp_status.dart'; import 'profile_status.dart'; class AuthBloc extends Bloc { final LoginUseCase loginUseCase; final CheckOtpUseCase checkOtpUseCase; final UpdateProfileUseCase updateProfileUseCase; final LogoutUseCase logoutUseCase; AuthBloc( this.loginUseCase, this.checkOtpUseCase, this.updateProfileUseCase, this.logoutUseCase, ) : super(AuthState.initial()) { on((event, emit) async { emit(state.copyWith(mobile: event.mobile, loginStatus: LoginLoading())); final res = await loginUseCase(event.mobile); if (res is DataSuccess) { emit(state.copyWith(loginStatus: LoginSuccess())); } else { emit(state.copyWith(loginStatus: LoginError(res.error!))); } }); on((event, emit) async { emit(state.copyWith(otpStatus: OtpLoading())); final res = await checkOtpUseCase(OtpParams(state.mobile, event.token)); if (res is DataSuccess) { emit(state.copyWith(otpStatus: OtpSuccess(res.data!.hasName))); } else { emit(state.copyWith(otpStatus: OtpError(res.error!))); } }); on((event, emit) async { emit(state.copyWith(profileStatus: ProfileLoading())); final res = await updateProfileUseCase( ProfileParams(event.firstName, event.avatar)); if (res is DataSuccess) { emit(state.copyWith(profileStatus: ProfileSuccess())); } else { emit(state.copyWith(profileStatus: ProfileError(res.error!))); } }); on((event, emit) async { await logoutUseCase(const NoParams()); emit(AuthState.initial()); }); } }