318 lines
11 KiB
Dart
318 lines
11 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:SEFER/constant/info.dart';
|
|
import 'package:SEFER/controller/firebase/firbase_messge.dart';
|
|
import 'package:SEFER/views/auth/login_page.dart';
|
|
import 'package:SEFER/views/auth/sms_verfy_page.dart';
|
|
import 'package:SEFER/views/widgets/my_dialog.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:SEFER/constant/box_name.dart';
|
|
import 'package:SEFER/constant/links.dart';
|
|
import 'package:SEFER/controller/functions/crud.dart';
|
|
import 'package:SEFER/main.dart';
|
|
import 'package:SEFER/views/home/map_page_passenger.dart';
|
|
import 'package:location/location.dart';
|
|
|
|
import '../functions/package_info.dart';
|
|
|
|
class LoginController extends GetxController {
|
|
final formKey = GlobalKey<FormState>();
|
|
final formKeyAdmin = GlobalKey<FormState>();
|
|
TextEditingController emailController = TextEditingController();
|
|
TextEditingController phoneController = TextEditingController();
|
|
TextEditingController passwordController = TextEditingController();
|
|
TextEditingController adminPasswordController = TextEditingController();
|
|
TextEditingController adminNameController = TextEditingController();
|
|
bool isAgreeTerms = false;
|
|
bool isloading = false;
|
|
late int isTest = 1;
|
|
void changeAgreeTerm() {
|
|
isAgreeTerms = !isAgreeTerms;
|
|
update();
|
|
}
|
|
|
|
@override
|
|
void onInit() async {
|
|
box.read(BoxName.isTest) == null ||
|
|
box.read(BoxName.isTest).toString() == '0'
|
|
? await getAppTester()
|
|
: null;
|
|
|
|
super.onInit();
|
|
}
|
|
|
|
getAppTester() async {
|
|
var res = await CRUD().get(
|
|
link: AppLink.getTesterApp,
|
|
payload: {'appPlatform': AppInformation.appName});
|
|
if (res != 'failure') {
|
|
var d = jsonDecode(res);
|
|
|
|
isTest = d['message'][0]['isTest'];
|
|
update();
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
updateAppTester(String appPlatform) async {
|
|
await CRUD().post(
|
|
link: AppLink.updateTesterApp, payload: {'appPlatform': appPlatform});
|
|
}
|
|
|
|
void saveAgreementTerms() {
|
|
box.write(BoxName.agreeTerms, 'agreed');
|
|
update();
|
|
}
|
|
|
|
void saveCountryCode(String countryCode) {
|
|
box.write(BoxName.countryCode, countryCode);
|
|
update();
|
|
}
|
|
|
|
loginUsingCredentials(String passengerID, email) async {
|
|
isloading = true;
|
|
update();
|
|
var res =
|
|
await CRUD().get(link: AppLink.loginFromGooglePassenger, payload: {
|
|
'email': email,
|
|
'id': passengerID,
|
|
"platform": Platform.isAndroid ? 'android' : 'ios',
|
|
"appName": AppInformation.appName,
|
|
});
|
|
if (res == 'Failure') {
|
|
Get.offAll(SmsSignupEgypt());
|
|
isloading = false;
|
|
update();
|
|
// Get.snackbar("User does not exist.".tr, '', backgroundColor: Colors.red);
|
|
} else {
|
|
var jsonDecoeded = jsonDecode(res);
|
|
if (jsonDecoeded.isNotEmpty) {
|
|
if (jsonDecoeded['status'] == 'success' &&
|
|
jsonDecoeded['data'][0]['verified'].toString() == '1') {
|
|
//
|
|
box.write(BoxName.isVerified, '1');
|
|
box.write(BoxName.email, jsonDecoeded['data'][0]['email']);
|
|
box.write(BoxName.phone, jsonDecoeded['data'][0]['phone']);
|
|
box.write(BoxName.isTest, '1');
|
|
box.write(BoxName.package, jsonDecoeded['data'][0]['package']);
|
|
box.write(BoxName.promo, jsonDecoeded['data'][0]['promo']);
|
|
box.write(BoxName.discount, jsonDecoeded['data'][0]['discount']);
|
|
box.write(BoxName.validity, jsonDecoeded['data'][0]['validity']);
|
|
box.write(BoxName.isInstall,
|
|
jsonDecoeded['data'][0]['isInstall'] ?? 'none');
|
|
box.write(BoxName.isGiftToken,
|
|
jsonDecoeded['data'][0]['isGiftToken'] ?? 'none');
|
|
box.write(BoxName.inviteCode,
|
|
jsonDecoeded['data'][0]['inviteCode'] ?? 'none');
|
|
|
|
var token = await CRUD().get(link: AppLink.getTokens, payload: {
|
|
'passengerID': box.read(BoxName.passengerID).toString()
|
|
});
|
|
if (token != 'failure') {
|
|
if (jsonDecode(token)['data'][0]['token'] !=
|
|
box.read(BoxName.tokenFCM)) {
|
|
Get.put(FirebaseMessagesController())
|
|
.sendNotificationToAnyWithoutData(
|
|
'token change'.tr,
|
|
'change device'.tr,
|
|
jsonDecode(token)['data'][0]['token'].toString(),
|
|
'cancel.wav',
|
|
);
|
|
Future.delayed(const Duration(seconds: 1));
|
|
await CRUD().post(
|
|
link: "${AppLink.server}/ride/firebase/add.php",
|
|
payload: {
|
|
'token': box.read(BoxName.tokenFCM),
|
|
'passengerID': box.read(BoxName.passengerID).toString()
|
|
});
|
|
CRUD().post(
|
|
link:
|
|
"${AppLink.seferAlexandriaServer}/ride/firebase/add.php",
|
|
payload: {
|
|
'token': box.read(BoxName.tokenFCM),
|
|
'passengerID': box.read(BoxName.passengerID).toString()
|
|
});
|
|
CRUD().post(
|
|
link: "${AppLink.seferGizaServer}/ride/firebase/add.php",
|
|
payload: {
|
|
'token': box.read(BoxName.tokenFCM),
|
|
'passengerID': box.read(BoxName.passengerID).toString()
|
|
});
|
|
Get.defaultDialog(
|
|
title: 'Device Change Detected'.tr,
|
|
middleText:
|
|
'You can only use one device at a time. This device will now be set as your active device.'
|
|
.tr,
|
|
textConfirm: 'OK'.tr,
|
|
confirmTextColor: Colors.white,
|
|
onConfirm: () {
|
|
Get.back();
|
|
Get.offAll(() => const MapPagePassenger());
|
|
},
|
|
);
|
|
}
|
|
} // Logging to check if inviteCode is written correctly
|
|
print("Invite Code in Box: ${box.read(BoxName.inviteCode)}");
|
|
print("Is Install: ${box.read(BoxName.isInstall)}");
|
|
|
|
if (box.read(BoxName.inviteCode).toString() != 'none' &&
|
|
box.read(BoxName.isInstall).toString() != '1') {
|
|
await CRUD()
|
|
.post(link: AppLink.updatePassengersInvitation, payload: {
|
|
"inviteCode": box.read(BoxName.inviteCode).toString(),
|
|
"passengerID": box.read(BoxName.passengerID).toString(),
|
|
});
|
|
Get.defaultDialog(
|
|
title: 'Invitation Used'
|
|
.tr, // Automatically translates based on the current locale
|
|
middleText: "Your invite code was successfully applied!"
|
|
.tr, // Automatically translates based on the current locale
|
|
onConfirm: () {
|
|
CRUD().post(link: AppLink.addPassengersPromo, payload: {
|
|
"promoCode":
|
|
'S-${box.read(BoxName.name).toString().split(' ')[0]}',
|
|
"amount": '25',
|
|
"passengerID": box.read(BoxName.passengerID).toString(),
|
|
"description": 'promo first'
|
|
});
|
|
Get.offAll(() =>
|
|
const MapPagePassenger()); // Navigate to MapPagePassenger after confirmation
|
|
},
|
|
textConfirm: "OK".tr, // Confirm button text
|
|
);
|
|
} else {
|
|
Get.offAll(() => const MapPagePassenger());
|
|
}
|
|
} else {
|
|
Get.offAll(() => SmsSignupEgypt());
|
|
// Get.snackbar(jsonDecoeded['status'], jsonDecoeded['data'],
|
|
// backgroundColor: Colors.redAccent);
|
|
isloading = false;
|
|
update();
|
|
}
|
|
} else {
|
|
isloading = false;
|
|
update();
|
|
}
|
|
}
|
|
}
|
|
|
|
// void adminDashboardOpen() async {
|
|
// if (formKeyAdmin.currentState!.validate()) {
|
|
// await DeviceInfoPlus.getDeviceInfo();
|
|
// if (Platform.isAndroid) {
|
|
// // var res = await CRUD().get(link: AppLink.getAdminUser, payload: {
|
|
// // // 'device_number': DeviceInfoPlus.deviceData['serialNumber'].toString(),
|
|
// // });
|
|
// // var d = jsonDecode(res);
|
|
// // // if (DeviceInfoPlus.deviceData['serialNumber'] ==
|
|
// // d['message']['device_number']) {
|
|
// Get.back();
|
|
// Get.to(() => const AdminHomePage());
|
|
// // }
|
|
// }
|
|
// if (Platform.isIOS) {
|
|
// // var res = await CRUD().get(link: AppLink.getAdminUser, payload: {
|
|
// // 'device_number': DeviceInfoPlus.deviceData['identifierForVendor'].toString(),
|
|
// // });
|
|
// // var d = jsonDecode(res);
|
|
// // if (DeviceInfoPlus.deviceData['serialNumber'] ==
|
|
// // d['message']['device_number']) {
|
|
// Get.back();
|
|
// Get.to(() => const AdminHomePage());
|
|
// // }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
void login() async {
|
|
isloading = true;
|
|
update();
|
|
var res = await CRUD().get(
|
|
link: AppLink.loginFromGooglePassenger,
|
|
payload: {
|
|
'email': emailController.text,
|
|
'id': passwordController.text
|
|
});
|
|
|
|
isloading = false;
|
|
update();
|
|
if (res == 'Failure') {
|
|
//Failure
|
|
Get.offAll(() => LoginPage());
|
|
isloading = false;
|
|
update();
|
|
// Get.snackbar("User does not exist.".tr, '', backgroundColor: Colors.red);
|
|
} else {
|
|
var jsonDecoeded = jsonDecode(res);
|
|
if (jsonDecoeded.isNotEmpty) {
|
|
if (jsonDecoeded['status'] == 'success' &&
|
|
jsonDecoeded['data'][0]['verified'] == 1) {
|
|
//
|
|
box.write(BoxName.isVerified, '1');
|
|
box.write(BoxName.email, jsonDecoeded['data'][0]['email']);
|
|
box.write(BoxName.phone, jsonDecoeded['data'][0]['phone']);
|
|
box.write(BoxName.passengerID, passwordController.text);
|
|
// var token = await CRUD().get(link: AppLink.getTokens, payload: {
|
|
// 'passengerID': box.read(BoxName.passengerID).toString()
|
|
// });
|
|
await updateAppTester(AppInformation.appName);
|
|
|
|
Get.offAll(() => const MapPagePassenger());
|
|
} else {
|
|
// Get.offAll(() => SmsSignupEgypt());
|
|
// Get.snackbar(jsonDecoeded['status'], jsonDecoeded['data'],
|
|
// backgroundColor: Colors.redAccent);
|
|
isloading = false;
|
|
update();
|
|
}
|
|
} else {
|
|
isloading = false;
|
|
update();
|
|
}
|
|
}
|
|
}
|
|
|
|
goToMapPage() {
|
|
if (box.read(BoxName.email) != null) {
|
|
Get.offAll(() => const MapPagePassenger());
|
|
}
|
|
}
|
|
|
|
final location = Location();
|
|
|
|
// late PermissionStatus permissionGranted = PermissionStatus.denied;
|
|
Future<void> getLocationPermission() async {
|
|
bool serviceEnabled;
|
|
PermissionStatus permissionGranted;
|
|
|
|
// Check if location services are enabled
|
|
serviceEnabled = await location.serviceEnabled();
|
|
if (!serviceEnabled) {
|
|
serviceEnabled = await location.requestService();
|
|
if (!serviceEnabled) {
|
|
// Location services are still not enabled, handle the error
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Check if the app has permission to access location
|
|
permissionGranted = await location.hasPermission();
|
|
if (permissionGranted == PermissionStatus.denied) {
|
|
permissionGranted = await location.requestPermission();
|
|
if (permissionGranted != PermissionStatus.granted) {
|
|
// Location permission is still not granted, handle the error
|
|
permissionGranted = await location.requestPermission();
|
|
return;
|
|
}
|
|
}
|
|
if (permissionGranted.toString() == 'PermissionStatus.granted') {
|
|
box.write(BoxName.locationPermission, 'true');
|
|
}
|
|
update();
|
|
}
|
|
}
|