320 lines
11 KiB
Dart
320 lines
11 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:Tripz/constant/info.dart';
|
|
import 'package:Tripz/controller/firebase/firbase_messge.dart';
|
|
import 'package:Tripz/controller/functions/add_error.dart';
|
|
import 'package:Tripz/views/auth/login_page.dart';
|
|
import 'package:Tripz/views/auth/sms_verfy_page.dart';
|
|
import 'package:Tripz/views/widgets/mydialoug.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:Tripz/constant/box_name.dart';
|
|
import 'package:Tripz/constant/links.dart';
|
|
import 'package:Tripz/controller/functions/crud.dart';
|
|
import 'package:Tripz/main.dart';
|
|
import 'package:Tripz/views/home/map_page_passenger.dart';
|
|
import 'package:location/location.dart';
|
|
|
|
import '../../print.dart';
|
|
import '../functions/encrypt_decrypt.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 = int.parse(d['message'][0]['isTest'].toString());
|
|
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.toString().contains('@')
|
|
? encryptionHelper.encryptData(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) {
|
|
var d = jsonDecoeded['data'][0];
|
|
if (jsonDecoeded['status'] == 'success' &&
|
|
d['verified'].toString() == '1') {
|
|
//
|
|
|
|
box.write(BoxName.isVerified, '1');
|
|
box.write(BoxName.email, d['email']);
|
|
box.write(BoxName.phone, d['phone']);
|
|
box.write(BoxName.isTest, '1');
|
|
box.write(BoxName.package, d['package']);
|
|
box.write(BoxName.promo, d['promo']);
|
|
box.write(BoxName.discount, d['discount']);
|
|
box.write(BoxName.validity, d['validity']);
|
|
box.write(BoxName.isInstall, d['isInstall'] ?? 'none');
|
|
box.write(BoxName.isGiftToken, d['isGiftToken'] ?? 'none');
|
|
d['inviteCode'] != null
|
|
? box.write(
|
|
BoxName.inviteCode,
|
|
encryptionHelper.decryptData(d['inviteCode'].toString()) ??
|
|
'none')
|
|
: null;
|
|
|
|
var token = await CRUD().get(link: AppLink.getTokens, payload: {
|
|
'passengerID': box.read(BoxName.passengerID).toString()
|
|
});
|
|
if (token != 'failure') {
|
|
if (encryptionHelper.decryptData(
|
|
jsonDecode(token)['data'][0]['token'].toString()) !=
|
|
box.read(BoxName.tokenFCM)) {
|
|
MyDialog().getDialog('change device'.tr, 'token change'.tr,
|
|
() async {
|
|
List<Future> updatetoken = [
|
|
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.tripzAlexandriaServer}/ride/firebase/add.php",
|
|
payload: {
|
|
'token': box.read(BoxName.tokenFCM),
|
|
'passengerID': box.read(BoxName.passengerID).toString()
|
|
}),
|
|
CRUD().post(
|
|
link: "${AppLink.tripzGizaServer}/ride/firebase/add.php",
|
|
payload: {
|
|
'token': box.read(BoxName.tokenFCM),
|
|
'passengerID': box.read(BoxName.passengerID).toString()
|
|
}),
|
|
];
|
|
// cameras = await availableCameras();
|
|
await Future.wait(updatetoken);
|
|
Get.put(FirebaseMessagesController())
|
|
.sendNotificationToDriverMAP(
|
|
'token change'.tr,
|
|
'change device'.tr,
|
|
encryptionHelper.decryptData(
|
|
jsonDecode(token)['data'][0]['token'].toString()),
|
|
[],
|
|
'cancel.wav',
|
|
);
|
|
});
|
|
|
|
Future.delayed(const Duration(seconds: 1));
|
|
|
|
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());
|
|
},
|
|
);
|
|
} else {
|
|
print('same');
|
|
}
|
|
} // Logging to check if inviteCode is written correctly
|
|
|
|
if (d['inviteCode'] != 'none' &&
|
|
d['inviteCode'] != null &&
|
|
// box.read(BoxName.inviteCode).toString() != 'none' &&
|
|
box.read(BoxName.isInstall).toString() != '1') {
|
|
await CRUD()
|
|
.post(link: AppLink.updatePassengersInvitation, payload: {
|
|
"inviteCode": encryptionHelper
|
|
.encryptData(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: () {
|
|
try {
|
|
CRUD().post(link: AppLink.addPassengersPromo, payload: {
|
|
"promoCode":
|
|
'S-${encryptionHelper.decryptData(box.read(BoxName.name)).toString().split(' ')[0]}',
|
|
"amount": '25',
|
|
"passengerID": box.read(BoxName.passengerID).toString(),
|
|
"description": 'promo first'
|
|
});
|
|
} catch (e) {
|
|
addError(e.toString(),
|
|
'passenger Invitation Used dialogu as promo line 185 login_controller');
|
|
} finally {
|
|
// Continue with the rest of your flow, regardless of errors
|
|
// For example, navigate to the next page
|
|
Get.offAll(() => const MapPagePassenger());
|
|
}
|
|
},
|
|
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 login() async {
|
|
isloading = true;
|
|
update();
|
|
var res =
|
|
await CRUD().get(link: AppLink.loginFromGooglePassenger, payload: {
|
|
'email': encryptionHelper.encryptData(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'].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.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();
|
|
}
|
|
}
|