feat: refactor code
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
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());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
abstract class AuthEvent {}
|
||||
|
||||
class LoginOtpEvent extends AuthEvent {
|
||||
final String mobile;
|
||||
LoginOtpEvent(this.mobile);
|
||||
}
|
||||
|
||||
class CheckOtpEvent extends AuthEvent {
|
||||
final String token;
|
||||
CheckOtpEvent(this.token);
|
||||
}
|
||||
|
||||
class UpdateProfileEvent extends AuthEvent {
|
||||
final String firstName;
|
||||
final String avatar;
|
||||
UpdateProfileEvent(this.firstName, this.avatar);
|
||||
}
|
||||
|
||||
class LogoutEvent extends AuthEvent {}
|
||||
@@ -0,0 +1,37 @@
|
||||
import 'login_status.dart';
|
||||
import 'otp_status.dart';
|
||||
import 'profile_status.dart';
|
||||
|
||||
class AuthState {
|
||||
final String mobile; // شمارهی در حال احراز (برای صفحهی کد)
|
||||
final LoginStatus loginStatus;
|
||||
final OtpStatus otpStatus;
|
||||
final ProfileStatus profileStatus;
|
||||
|
||||
AuthState({
|
||||
required this.mobile,
|
||||
required this.loginStatus,
|
||||
required this.otpStatus,
|
||||
required this.profileStatus,
|
||||
});
|
||||
|
||||
factory AuthState.initial() => AuthState(
|
||||
mobile: '',
|
||||
loginStatus: LoginInitial(),
|
||||
otpStatus: OtpInitial(),
|
||||
profileStatus: ProfileInitial(),
|
||||
);
|
||||
|
||||
AuthState copyWith({
|
||||
String? mobile,
|
||||
LoginStatus? loginStatus,
|
||||
OtpStatus? otpStatus,
|
||||
ProfileStatus? profileStatus,
|
||||
}) =>
|
||||
AuthState(
|
||||
mobile: mobile ?? this.mobile,
|
||||
loginStatus: loginStatus ?? this.loginStatus,
|
||||
otpStatus: otpStatus ?? this.otpStatus,
|
||||
profileStatus: profileStatus ?? this.profileStatus,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
abstract class LoginStatus {}
|
||||
|
||||
class LoginInitial extends LoginStatus {}
|
||||
|
||||
class LoginLoading extends LoginStatus {}
|
||||
|
||||
class LoginSuccess extends LoginStatus {}
|
||||
|
||||
class LoginError extends LoginStatus {
|
||||
final String message;
|
||||
LoginError(this.message);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
abstract class OtpStatus {}
|
||||
|
||||
class OtpInitial extends OtpStatus {}
|
||||
|
||||
class OtpLoading extends OtpStatus {}
|
||||
|
||||
class OtpSuccess extends OtpStatus {
|
||||
final bool hasName; // اگر نام نداشته باشد، باید به صفحهی انتخاب نام برود
|
||||
OtpSuccess(this.hasName);
|
||||
}
|
||||
|
||||
class OtpError extends OtpStatus {
|
||||
final String message;
|
||||
OtpError(this.message);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
abstract class ProfileStatus {}
|
||||
|
||||
class ProfileInitial extends ProfileStatus {}
|
||||
|
||||
class ProfileLoading extends ProfileStatus {}
|
||||
|
||||
class ProfileSuccess extends ProfileStatus {}
|
||||
|
||||
class ProfileError extends ProfileStatus {
|
||||
final String message;
|
||||
ProfileError(this.message);
|
||||
}
|
||||
Reference in New Issue
Block a user