Files
front-hokm/lib/feature/auth/presentation/bloc/auth_bloc.dart
T
2026-06-17 12:57:28 +03:30

64 lines
2.1 KiB
Dart

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<AuthEvent, AuthState> {
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<LoginOtpEvent>((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<CheckOtpEvent>((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<UpdateProfileEvent>((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<LogoutEvent>((event, emit) async {
await logoutUseCase(const NoParams());
emit(AuthState.initial());
});
}
}