132 lines
3.7 KiB
Dart
132 lines
3.7 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:get/get.dart';
|
|
import 'package:sefer_driver/print.dart';
|
|
import 'package:sefer_driver/views/home/Captin/home_captain/home_captin.dart';
|
|
|
|
import '../../../constant/box_name.dart';
|
|
import '../../../constant/links.dart';
|
|
import '../../../main.dart';
|
|
import '../../firebase/firbase_messge.dart';
|
|
import '../../firebase/notification_service.dart';
|
|
import '../../functions/crud.dart';
|
|
|
|
class OtpVerificationController extends GetxController {
|
|
final String phone;
|
|
final String deviceToken;
|
|
final String token;
|
|
final otpCode = ''.obs;
|
|
final isLoading = false.obs;
|
|
final isVerifying = false.obs;
|
|
var canResend = false.obs;
|
|
var countdown = 120.obs;
|
|
Timer? _timer;
|
|
|
|
OtpVerificationController({
|
|
required this.phone,
|
|
required this.deviceToken,
|
|
required this.token,
|
|
});
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
sendOtp(); // ترسل تلقائيًا عند فتح الصفحة
|
|
startCountdown();
|
|
}
|
|
|
|
void startCountdown() {
|
|
canResend.value = false;
|
|
countdown.value = 120;
|
|
_timer?.cancel();
|
|
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
|
if (countdown.value > 0) {
|
|
countdown.value--;
|
|
} else {
|
|
canResend.value = true;
|
|
timer.cancel();
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> sendOtp() async {
|
|
isLoading.value = true;
|
|
try {
|
|
final response = await CRUD().post(
|
|
link:
|
|
'${AppLink.server}/auth/token_passenger/driver/send_otp_driver.php',
|
|
payload: {
|
|
'receiver': phone,
|
|
// 'device_token': deviceToken,
|
|
},
|
|
);
|
|
|
|
if (response != 'failure') {
|
|
// بإمكانك عرض رسالة نجاح هنا
|
|
} else {
|
|
// Get.snackbar('Error', 'Failed to send OTP');
|
|
}
|
|
} catch (e) {
|
|
Get.snackbar('Error', e.toString());
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
Future<void> verifyOtp(String ptoken) async {
|
|
isVerifying.value = true;
|
|
var finger = await storage.read(key: BoxName.fingerPrint);
|
|
try {
|
|
final response = await CRUD().post(
|
|
link:
|
|
'${AppLink.server}/auth/token_passenger/driver/verify_otp_driver.php',
|
|
payload: {
|
|
'phone_number': phone,
|
|
'otp': otpCode.value,
|
|
'token': box.read(BoxName.tokenDriver).toString(),
|
|
'fingerPrint': finger.toString(),
|
|
},
|
|
);
|
|
|
|
if (response != 'failure') {
|
|
Log.print('response: ${response}');
|
|
Get.back(); // توجه إلى الصفحة التالية
|
|
await CRUD().post(
|
|
link: '${AppLink.paymentServer}/auth/token/update_driver_auth.php',
|
|
payload: {
|
|
'token': box.read(BoxName.tokenDriver).toString(),
|
|
'fingerPrint': finger.toString(),
|
|
'captain_id': box.read(BoxName.driverID).toString(),
|
|
});
|
|
final fcm = Get.isRegistered<FirebaseMessagesController>()
|
|
? Get.find<FirebaseMessagesController>()
|
|
: Get.put(FirebaseMessagesController());
|
|
|
|
// await fcm.sendNotificationToDriverMAP(
|
|
// 'token change',
|
|
// 'change device'.tr,
|
|
// ptoken.toString(),
|
|
// [],
|
|
// 'cancel.wav',
|
|
// );
|
|
await NotificationService.sendNotification(
|
|
target: ptoken.toString(),
|
|
title: 'token change',
|
|
body: 'token change'.tr,
|
|
isTopic: false, // Important: this is a token
|
|
tone: 'cancel',
|
|
driverList: [],
|
|
);
|
|
|
|
Get.offAll(() => HomeCaptain());
|
|
} else {
|
|
Get.snackbar('Verification Failed', 'OTP is incorrect or expired');
|
|
}
|
|
} catch (e) {
|
|
Get.snackbar('Error', e.toString());
|
|
} finally {
|
|
isVerifying.value = false;
|
|
}
|
|
}
|
|
}
|