feat: sending otp and resend otp done
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
import 'package:food_delivery_client/feature/auth/domain/usecases/verify_otp_code_login_usecase.dart';
|
||||
import 'package:food_delivery_client/feature/auth/domain/usecases/verify_otp_code_register_usecase.dart';
|
||||
import 'package:food_delivery_client/feature/auth/domain/usecases/verify_phone_login_usecase.dart';
|
||||
import 'package:food_delivery_client/feature/auth/domain/usecases/verify_phone_register_usecase.dart';
|
||||
import 'package:food_delivery_client/feature/common/presentation/widgets/w_toastification.dart';
|
||||
import 'package:food_delivery_client/food_delivery_client.dart';
|
||||
|
||||
part 'verify_otp_event.dart';
|
||||
|
||||
part 'verify_otp_state.dart';
|
||||
|
||||
part 'verify_otp_bloc.freezed.dart';
|
||||
|
||||
@injectable
|
||||
class VerifyOtpBloc extends Bloc<VerifyOtpEvent, VerifyOtpState> {
|
||||
final VerifyOtpCodeRegisterUseCase _registerUseCase;
|
||||
final VerifyOtpCodeForgotPasswordUseCase _passwordUseCase;
|
||||
final VerifyPhoneRegisterUseCase _phoneRegisterUseCase;
|
||||
final VerifyPhoneNumberLoginUseCase _phoneNumberLoginUseCase;
|
||||
|
||||
VerifyOtpBloc(
|
||||
this._registerUseCase,
|
||||
this._passwordUseCase,
|
||||
this._phoneRegisterUseCase,
|
||||
this._phoneNumberLoginUseCase,
|
||||
) : super(const VerifyOtpState()) {
|
||||
Timer? timer1;
|
||||
|
||||
on<_CancelTimer>((event, emit) {
|
||||
timer1?.cancel();
|
||||
});
|
||||
|
||||
on<_Started>((event, emit) {
|
||||
int seconds = state.time;
|
||||
emit(state.copyWith(time: seconds));
|
||||
timer1 = Timer.periodic(TimeDelayConst.duration1, (timer) {
|
||||
if (seconds == 0) {
|
||||
timer.cancel();
|
||||
} else {
|
||||
seconds--;
|
||||
add(VerifyOtpEvent.ticked(seconds));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
on<_Ticked>(_onTicked);
|
||||
on<_VerifyOtpReset>(_onVerifyOtpReset);
|
||||
on<_VerifyOtpRegister>(_onVerifyOtpRegister);
|
||||
on<_ResendRegister>(_onResendVerifyPhoneRegister);
|
||||
on<_ResendForgot>(_onResendVerifyPhoneForgot);
|
||||
}
|
||||
|
||||
void _onTicked(_Ticked event, Emitter<VerifyOtpState> emit) {
|
||||
emit(state.copyWith(time: event.seconds));
|
||||
}
|
||||
|
||||
Future<void> _onVerifyOtpReset(
|
||||
_VerifyOtpReset event,
|
||||
Emitter<VerifyOtpState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: RequestStatus.loading));
|
||||
final response = await _passwordUseCase.call(event.params);
|
||||
|
||||
response.fold(
|
||||
(l) {
|
||||
showErrorToast(l.errorMessage);
|
||||
emit(state.copyWith(status: RequestStatus.error));
|
||||
},
|
||||
(r) {
|
||||
emit(state.copyWith(status: RequestStatus.loaded));
|
||||
add(VerifyOtpEvent.cancelTimer());
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onVerifyOtpRegister(
|
||||
_VerifyOtpRegister event,
|
||||
Emitter<VerifyOtpState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: RequestStatus.loading));
|
||||
final response = await _registerUseCase.call(event.params);
|
||||
|
||||
response.fold(
|
||||
(l) {
|
||||
showErrorToast(l.errorMessage);
|
||||
emit(state.copyWith(status: RequestStatus.error));
|
||||
},
|
||||
(r) {
|
||||
emit(state.copyWith(status: RequestStatus.loaded));
|
||||
add(VerifyOtpEvent.cancelTimer());
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onResendVerifyPhoneRegister(
|
||||
_ResendRegister event,
|
||||
Emitter<VerifyOtpState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(resendStatus: RequestStatus.loading));
|
||||
final response = await _phoneRegisterUseCase.call(
|
||||
VerifyPhoneNumberParams(phoneNumber: event.phoneNUmber),
|
||||
);
|
||||
|
||||
response.fold(
|
||||
(l) {
|
||||
showErrorToast(l.errorMessage);
|
||||
emit(state.copyWith(resendStatus: RequestStatus.error));
|
||||
},
|
||||
(r) {
|
||||
emit(state.copyWith(resendStatus: RequestStatus.loaded));
|
||||
add(VerifyOtpEvent.ticked(120));
|
||||
add(VerifyOtpEvent.started());
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onResendVerifyPhoneForgot(
|
||||
_ResendForgot event,
|
||||
Emitter<VerifyOtpState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(resendStatus: RequestStatus.loading));
|
||||
final response = await _phoneNumberLoginUseCase.call(
|
||||
VerifyPhoneNumberParams(phoneNumber: event.phoneNUmber),
|
||||
);
|
||||
|
||||
response.fold(
|
||||
(l) {
|
||||
showErrorToast(l.errorMessage);
|
||||
emit(state.copyWith(resendStatus: RequestStatus.error));
|
||||
},
|
||||
(r) {
|
||||
emit(state.copyWith(resendStatus: RequestStatus.loaded));
|
||||
add(VerifyOtpEvent.ticked(120));
|
||||
add(VerifyOtpEvent.started());
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
part of 'verify_otp_bloc.dart';
|
||||
|
||||
@freezed
|
||||
class VerifyOtpEvent with _$VerifyOtpEvent {
|
||||
const factory VerifyOtpEvent.started() = _Started;
|
||||
const factory VerifyOtpEvent.cancelTimer() = _CancelTimer;
|
||||
const factory VerifyOtpEvent.resendRegister(String phoneNUmber) =
|
||||
_ResendRegister;
|
||||
|
||||
const factory VerifyOtpEvent.resendForgot(String phoneNUmber) = _ResendForgot;
|
||||
|
||||
const factory VerifyOtpEvent.ticked(int seconds) = _Ticked;
|
||||
|
||||
const factory VerifyOtpEvent.verifyOtpReset(VerifyOtpCodeParams params) =
|
||||
_VerifyOtpReset;
|
||||
|
||||
const factory VerifyOtpEvent.verifyOtpRegister(VerifyOtpCodeParams params) =
|
||||
_VerifyOtpRegister;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
part of 'verify_otp_bloc.dart';
|
||||
|
||||
@freezed
|
||||
abstract class VerifyOtpState with _$VerifyOtpState {
|
||||
const factory VerifyOtpState({
|
||||
@Default(RequestStatus.initial) RequestStatus status,
|
||||
@Default(RequestStatus.initial) RequestStatus resendStatus,
|
||||
@Default(120) int time,
|
||||
}) = _VerifyOtpState;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import 'package:food_delivery_client/feature/auth/domain/usecases/verify_phone_login_usecase.dart';
|
||||
import 'package:food_delivery_client/feature/auth/domain/usecases/verify_phone_register_usecase.dart';
|
||||
import 'package:food_delivery_client/feature/common/presentation/widgets/w_toastification.dart';
|
||||
import 'package:food_delivery_client/food_delivery_client.dart';
|
||||
|
||||
part 'verify_phone_event.dart';
|
||||
|
||||
part 'verify_phone_state.dart';
|
||||
|
||||
part 'verify_phone_bloc.freezed.dart';
|
||||
|
||||
@injectable
|
||||
class VerifyPhoneBloc extends Bloc<VerifyPhoneEvent, VerifyPhoneState> {
|
||||
final VerifyPhoneNumberLoginUseCase _loginUseCase;
|
||||
final VerifyPhoneRegisterUseCase _registerUseCase;
|
||||
|
||||
VerifyPhoneBloc(this._loginUseCase, this._registerUseCase)
|
||||
: super(const VerifyPhoneState()) {
|
||||
on<_VerifyPhoneRegister>(_onVerifyPhoneNumberRegister);
|
||||
on<_VerifyPhoneReset>(_onVerifyPhoneReset);
|
||||
}
|
||||
|
||||
Future<void> _onVerifyPhoneNumberRegister(
|
||||
_VerifyPhoneRegister event,
|
||||
Emitter<VerifyPhoneState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: RequestStatus.loading));
|
||||
final response = await _registerUseCase.call(event.params);
|
||||
|
||||
response.fold(
|
||||
(l) {
|
||||
showErrorToast(l.errorMessage);
|
||||
emit(state.copyWith(status: RequestStatus.error));
|
||||
},
|
||||
(r) {
|
||||
showSuccessToast(r.message);
|
||||
emit(state.copyWith(status: RequestStatus.loaded));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _onVerifyPhoneReset(
|
||||
_VerifyPhoneReset event,
|
||||
Emitter<VerifyPhoneState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: RequestStatus.loading));
|
||||
final response = await _loginUseCase.call(event.params);
|
||||
|
||||
response.fold(
|
||||
(l) {
|
||||
showErrorToast(l.errorMessage);
|
||||
emit(state.copyWith(status: RequestStatus.error));
|
||||
},
|
||||
(r) {
|
||||
showSuccessToast(r.message);
|
||||
emit(state.copyWith(status: RequestStatus.loaded));
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
part of 'verify_phone_bloc.dart';
|
||||
|
||||
@freezed
|
||||
class VerifyPhoneEvent with _$VerifyPhoneEvent {
|
||||
const factory VerifyPhoneEvent.started() = _Started;
|
||||
|
||||
const factory VerifyPhoneEvent.verifyPhoneRegister(
|
||||
VerifyPhoneNumberParams params,
|
||||
) = _VerifyPhoneRegister;
|
||||
|
||||
const factory VerifyPhoneEvent.verifyPhoneReset(
|
||||
VerifyPhoneNumberParams params,
|
||||
) = _VerifyPhoneReset;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
part of 'verify_phone_bloc.dart';
|
||||
|
||||
@freezed
|
||||
abstract class VerifyPhoneState with _$VerifyPhoneState {
|
||||
const factory VerifyPhoneState({
|
||||
@Default(RequestStatus.initial) RequestStatus status,
|
||||
}) = _VerifyPhoneState;
|
||||
}
|
||||
Reference in New Issue
Block a user