This commit is contained in:
Hamza-Ayed
2024-09-30 09:37:26 +03:00
parent f5b7307f86
commit 659f178737
21 changed files with 416 additions and 151 deletions

View File

@@ -160,83 +160,124 @@ class RegisterCaptainController extends GetxController {
// isLoading = false;
// update();
// }
DateTime? lastOtpSentTime; // Store the last OTP sent time
int otpResendInterval = 300; // 5 minutes in seconds
// Main function to handle OTP sending
sendOtpMessage() async {
SmsEgyptController smsEgyptController = Get.put(SmsEgyptController());
isLoading = true;
update();
int randomNumber = Random().nextInt(100000) + 1;
isLoading = true;
update();
// Get the current time and the last OTP time (if it exists)
DateTime currentTime = DateTime.now();
DateTime? lastOtpTime = box.read(BoxName.lastOtpTime);
// Check if the last OTP was sent within 5 minutes (300 seconds)
if (lastOtpTime != null &&
currentTime.difference(lastOtpTime).inSeconds < 300) {
Get.snackbar(
'Please wait'.tr, 'You can send another OTP after 5 minutes.'.tr,
backgroundColor: AppColor.redColor);
isLoading = false;
if (_isOtpResendAllowed()) {
isLoading = true;
update();
return;
}
if (formKey3.currentState!.validate()) {
if (box.read(BoxName.countryCode) == 'Egypt') {
if (isValidEgyptianPhoneNumber(phoneController.text)) {
var responseCheker = await CRUD()
.post(link: AppLink.checkPhoneNumberISVerfiedDriver, payload: {
'phone_number': '+2${phoneController.text}',
});
if (formKey3.currentState!.validate()) {
String countryCode = box.read(BoxName.countryCode);
String phoneNumber = phoneController.text;
if (responseCheker != 'failure') {
var d = jsonDecode(responseCheker);
if (d['message'][0]['is_verified'].toString() == '1') {
Get.snackbar('Phone number is verified before'.tr, '',
backgroundColor: AppColor.greenColor);
box.write(BoxName.phoneVerified, '1');
box.write(BoxName.phone, '+2${phoneController.text}');
await Get.put(LoginDriverController()).loginUsingCredentials(
box.read(BoxName.driverID).toString(),
box.read(BoxName.emailDriver).toString(),
);
} else {
await _sendOtp(randomNumber, smsEgyptController);
}
} else {
await _sendOtp(randomNumber, smsEgyptController);
}
if (countryCode == 'Egypt' && isValidEgyptianPhoneNumber(phoneNumber)) {
await _checkAndSendOtp(phoneNumber);
} else {
Get.snackbar('Phone Number wrong'.tr, '',
backgroundColor: AppColor.redColor);
_showErrorMessage('Phone Number is not Egypt phone '.tr);
}
}
isLoading = false;
update();
} else {
_showCooldownMessage();
}
}
// Check if the resend OTP request is allowed (5 minutes cooldown)
bool _isOtpResendAllowed() {
if (lastOtpSentTime == null) return true;
final int elapsedTime =
DateTime.now().difference(lastOtpSentTime!).inSeconds;
return elapsedTime >= otpResendInterval;
}
// Show message when user tries to resend OTP too soon
void _showCooldownMessage() {
int remainingTime = otpResendInterval -
DateTime.now().difference(lastOtpSentTime!).inSeconds;
Get.snackbar(
'Please wait ${remainingTime ~/ 60}:${(remainingTime % 60).toString().padLeft(2, '0')} minutes before requesting again',
'',
backgroundColor: AppColor.redColor,
);
}
// Check if the phone number has been verified, and send OTP if not verified
_checkAndSendOtp(String phoneNumber) async {
var responseChecker = await CRUD().post(
link: AppLink.checkPhoneNumberISVerfiedDriver,
payload: {
'phone_number': '+2$phoneNumber',
},
);
if (responseChecker != 'failure') {
var responseData = jsonDecode(responseChecker);
if (_isPhoneVerified(responseData)) {
_handleAlreadyVerified();
} else {
await _sendOtpAndSms(phoneNumber);
}
} else {
await _sendOtpAndSms(phoneNumber);
}
}
// Check if the phone number is already verified
bool _isPhoneVerified(dynamic responseData) {
return responseData['message'][0]['is_verified'].toString() == '1';
}
// Handle case where phone number is already verified
_handleAlreadyVerified() {
Get.snackbar(
'Phone number is already verified'.tr,
'',
backgroundColor: AppColor.greenColor,
);
box.write(BoxName.phoneVerified, '1');
box.write(BoxName.phone, '+2${phoneController.text}');
Get.put(LoginDriverController()).loginUsingCredentials(
box.read(BoxName.driverID).toString(),
box.read(BoxName.emailDriver).toString(),
);
}
// Send OTP and SMS
_sendOtpAndSms(String phoneNumber) async {
SmsEgyptController smsEgyptController = Get.put(SmsEgyptController());
int randomNumber = Random().nextInt(100000) + 1;
await CRUD().post(
link: AppLink.sendVerifyOtpMessage,
payload: {
'phone_number': '+2$phoneNumber',
'token_code': randomNumber.toString(),
'driverId': box.read(BoxName.driverID),
'email': box.read(BoxName.emailDriver),
},
);
// Get.snackbar('', '');
// await smsEgyptController.sendSmsEgypt(phoneNumber, randomNumber.toString());
lastOtpSentTime = DateTime.now(); // Update the last OTP sent time
isSent = true;
isLoading = false;
update();
}
_sendOtp(int randomNumber, SmsEgyptController smsEgyptController) async {
await CRUD().post(link: AppLink.sendVerifyOtpMessage, payload: {
'phone_number': '+2${phoneController.text}',
'token_code': randomNumber.toString(),
"driverId": box.read(BoxName.driverID),
"email": box.read(BoxName.emailDriver),
});
await smsEgyptController.sendSmsEgypt(
phoneController.text.toString(), randomNumber.toString());
// Save the current time as the last OTP time
box.write(BoxName.lastOtpTime, DateTime.now());
isSent = true;
isLoading = false;
update();
// Show error message in case of invalid phone number
void _showErrorMessage(String message) {
Get.snackbar(
message.tr,
'',
backgroundColor: AppColor.redColor,
);
}
verifySMSCode() async {