160 lines
5.9 KiB
Dart
160 lines
5.9 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:SEFER/constant/box_name.dart';
|
|
import 'package:SEFER/constant/colors.dart';
|
|
import 'package:SEFER/constant/links.dart';
|
|
import 'package:SEFER/controller/functions/crud.dart';
|
|
import 'package:SEFER/controller/home/payment/captain_wallet_controller.dart';
|
|
import 'package:SEFER/views/widgets/mydialoug.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_contacts/contact.dart';
|
|
import 'package:flutter_contacts/flutter_contacts.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
import '../../../main.dart';
|
|
import '../../../print.dart';
|
|
import '../../functions/launch.dart';
|
|
import '../../notification/notification_captain_controller.dart';
|
|
|
|
class InviteController extends GetxController {
|
|
final TextEditingController invitePhoneController = TextEditingController();
|
|
List driverInvitationData = [];
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
// fetchDriverStats();
|
|
}
|
|
|
|
void fetchDriverStats() async {
|
|
try {
|
|
var response = await CRUD().get(link: AppLink.getInviteDriver, payload: {
|
|
"driverId": box.read(BoxName.driverID),
|
|
});
|
|
if (response != 'failure') {
|
|
var data = jsonDecode(response);
|
|
driverInvitationData = data['message'];
|
|
update();
|
|
// print('driverInitationData: $driverInitationData');
|
|
}
|
|
} catch (e) {
|
|
print('Error fetching driver stats: $e');
|
|
}
|
|
}
|
|
|
|
Future<void> pickContact() async {
|
|
try {
|
|
print('Requesting contact permission...');
|
|
if (await FlutterContacts.requestPermission(readonly: true)) {
|
|
print('Permission granted. Opening external contact picker...');
|
|
final Contact? contact = await FlutterContacts.openExternalPick();
|
|
if (contact != null) {
|
|
print('Contact picked: ${contact.displayName}');
|
|
if (contact.phones.isNotEmpty) {
|
|
print('Phone number found: ${contact.phones.first.number}');
|
|
invitePhoneController.text = contact.phones.first.number;
|
|
update();
|
|
} else {
|
|
print('Selected contact has no phone number.');
|
|
Get.snackbar('No phone number'.tr,
|
|
'The selected contact does not have a phone number.'.tr);
|
|
}
|
|
} else {
|
|
print('No contact selected or picker was cancelled.');
|
|
Get.snackbar('No contact selected'.tr, 'Please select a contact'.tr);
|
|
}
|
|
} else {
|
|
print('Permission denied by user or system.');
|
|
Get.snackbar('Permission denied'.tr,
|
|
'Contact permission is required to pick a contact'.tr);
|
|
}
|
|
} catch (e) {
|
|
print('Error picking contact: $e');
|
|
print('Stack trace: ${StackTrace.current}');
|
|
Get.snackbar(
|
|
'Error'.tr, 'An error occurred while picking a contact: $e'.tr);
|
|
}
|
|
}
|
|
|
|
void onSelectDriverInvitation(int index) async {
|
|
MyDialog().getDialog(
|
|
driverInvitationData[index]['countOfInvitDriver'] < 100
|
|
? '${'When'.tr} ${driverInvitationData[index]['invitorName']} ${"complete, you can claim your gift".tr} '
|
|
: 'You deserve the gift'.tr,
|
|
'${driverInvitationData[index]['invitorName']} ${driverInvitationData[index]['countOfInvitDriver']} / 100 ${'Trip'.tr}',
|
|
() async {
|
|
if (driverInvitationData[index]['countOfInvitDriver'] < 100) {
|
|
Get.back();
|
|
} else {
|
|
//claim your gift
|
|
if (driverInvitationData[index]['isGiftToken'].toString() == '0') {
|
|
Get.back();
|
|
await Get.find<CaptainWalletController>()
|
|
.addDriverWallet('paymentMethod', '500');
|
|
// add for invitor too
|
|
await Get.find<CaptainWalletController>().addDriverWalletToInvitor(
|
|
'paymentMethod',
|
|
driverInvitationData[index]['driverInviterId'],
|
|
'500');
|
|
await CRUD().post(
|
|
link: AppLink.updateInviteDriver,
|
|
payload: {'id': driverInvitationData[index]['id']});
|
|
NotificationCaptainController().addNotificationCaptain(
|
|
driverInvitationData[index]['driverInviterId'].toString(),
|
|
"You have got a gift for invitation".tr,
|
|
'${"You have 500".tr} ${'LE'}',
|
|
false);
|
|
} else {
|
|
Get.back();
|
|
MyDialog().getDialog("You have got a gift".tr,
|
|
"Share the app with another new driver".tr, () {
|
|
Get.back();
|
|
});
|
|
}
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
void sendInvite() async {
|
|
if (invitePhoneController.text.isEmpty) {
|
|
Get.snackbar('Error', 'Please enter an phone address'.tr);
|
|
return;
|
|
}
|
|
|
|
// try {
|
|
var response = await CRUD().post(link: AppLink.addInviteDriver, payload: {
|
|
"driverId": box.read(BoxName.driverID),
|
|
"inviterDriverPhone": '+2${invitePhoneController.text}'
|
|
});
|
|
Log.print('response: ${response}');
|
|
if (response != 'failure') {
|
|
var d = jsonDecode(response);
|
|
Get.snackbar('Success', 'Invite sent successfully'.tr);
|
|
|
|
String message = '${'*SEFER DRIVER CODE*'.tr}\n\n'
|
|
'${"Use this code in registration".tr}\n'
|
|
'${"To get a gift for both".tr}\n\n'
|
|
'${"The period of this code is 1 hour".tr}\n\n'
|
|
'${'before'.tr} *${d['message']['expirationTime'].toString()}*\n\n'
|
|
'_*${d['message']['inviteCode'].toString()}*_\n\n'
|
|
'${"Install our app:".tr}\n'
|
|
'*Android:* https://play.google.com/store/apps/details?id=com.sefer_driver\n\n\n'
|
|
'*iOS:* https://apps.apple.com/ae/app/sefer-driver/id6502189302';
|
|
|
|
launchCommunication(
|
|
'whatsapp', '+2${invitePhoneController.text}', message);
|
|
|
|
invitePhoneController.clear();
|
|
} else {
|
|
Get.snackbar('Error'.tr, "Invite code already used".tr,
|
|
backgroundColor: AppColor.redColor,
|
|
duration: const Duration(seconds: 4));
|
|
}
|
|
// } catch (e) {
|
|
// print('Error sending invite: $e');
|
|
// Get.snackbar('Error', 'An error occurred'.tr);
|
|
// }
|
|
}
|
|
}
|