197 lines
6.8 KiB
Dart
197 lines
6.8 KiB
Dart
import 'dart:convert';
|
|
import 'dart:math';
|
|
|
|
import 'package:SEFER/constant/colors.dart';
|
|
import 'package:SEFER/controller/functions/location_background_controller.dart';
|
|
import 'package:SEFER/views/auth/captin/cards/sms_signup.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.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/controller/functions/secure_storage.dart';
|
|
import 'package:SEFER/main.dart';
|
|
import 'package:SEFER/views/auth/captin/verify_email_captain.dart';
|
|
import 'package:SEFER/views/home/Captin/home_captain/home_captin.dart';
|
|
import 'package:location/location.dart';
|
|
|
|
import '../../firebase/firbase_messge.dart';
|
|
|
|
class LoginDriverController extends GetxController {
|
|
final formKey = GlobalKey<FormState>();
|
|
TextEditingController emailController = TextEditingController();
|
|
TextEditingController phoneController = TextEditingController();
|
|
TextEditingController passwordController = TextEditingController();
|
|
bool isAgreeTerms = false;
|
|
bool isloading = false;
|
|
final FlutterSecureStorage _storage = const FlutterSecureStorage();
|
|
final location = Location();
|
|
void changeAgreeTerm() {
|
|
isAgreeTerms = !isAgreeTerms;
|
|
update();
|
|
}
|
|
|
|
void saveAgreementTerms() {
|
|
box.write(BoxName.agreeTerms, 'agreed');
|
|
update();
|
|
}
|
|
|
|
void saveCountryCode(String countryCode) {
|
|
box.write(BoxName.countryCode, countryCode);
|
|
update();
|
|
}
|
|
|
|
Future<void> getLocationPermission() async {
|
|
Get.put(LocationBackgroundController()).requestLocationPermission();
|
|
|
|
update();
|
|
}
|
|
|
|
void login() async {
|
|
isloading = true;
|
|
update();
|
|
var res = await CRUD().get(link: AppLink.loginCaptin, payload: {
|
|
'email': emailController.text,
|
|
'phone': phoneController.text,
|
|
'password': passwordController.text
|
|
});
|
|
if (res == 'failure') {
|
|
//Failure
|
|
isloading = false;
|
|
update();
|
|
Get.snackbar('Failure', '', backgroundColor: Colors.red);
|
|
} else {
|
|
var jsonDecoeded = jsonDecode(res);
|
|
if (jsonDecoeded.isNotEmpty) {
|
|
if (jsonDecoeded['status'] == 'success') {
|
|
if (jsonDecoeded['data'][0]['verified'] == 1) {
|
|
box.write(BoxName.driverID, jsonDecoeded['data'][0]['id']);
|
|
box.write(BoxName.emailDriver, jsonDecoeded['data'][0]['email']);
|
|
box.write(
|
|
BoxName.nameDriver, jsonDecoeded['data'][0]['first_name']);
|
|
box.write(BoxName.phoneDriver, jsonDecoeded['data'][0]['phone']);
|
|
SecureStorage()
|
|
.saveData(BoxName.passwordDriver, passwordController.text);
|
|
Get.offAll(() => HomeCaptain());
|
|
isloading = false;
|
|
update();
|
|
await CRUD().post(link: AppLink.addTokensDriver, payload: {
|
|
'token': box.read(BoxName.tokenDriver),
|
|
'captain_id': box.read(BoxName.driverID).toString()
|
|
});
|
|
} else {
|
|
isloading = false;
|
|
update();
|
|
Get.defaultDialog(
|
|
title: 'You must Verify email !.'.tr,
|
|
middleText: '',
|
|
backgroundColor: Colors.yellow[300],
|
|
onConfirm: () async {
|
|
int randomNumber = Random().nextInt(100000) + 1;
|
|
await CRUD().post(link: AppLink.sendVerifyEmail, payload: {
|
|
'email': emailController.text,
|
|
'token': randomNumber.toString(),
|
|
});
|
|
Get.to(() => VerifyEmailCaptainPage());
|
|
},
|
|
);
|
|
}
|
|
} else if (jsonDecoeded['status'] == 'Failure') {
|
|
Get.snackbar(jsonDecoeded['status'], jsonDecoeded['data'],
|
|
backgroundColor: Colors.redAccent);
|
|
isloading = false;
|
|
update();
|
|
}
|
|
} else {
|
|
isloading = false;
|
|
update();
|
|
}
|
|
}
|
|
}
|
|
|
|
loginUsingCredentials(String driverID, email) async {
|
|
isloading = true;
|
|
update();
|
|
var res = await CRUD().get(link: AppLink.loginFromGoogleCaptin, payload: {
|
|
'email': email,
|
|
'id': driverID,
|
|
});
|
|
if (res == 'Failure') {
|
|
//Failure
|
|
Get.offAll(() => SmsSignupEgypt());
|
|
isloading = false;
|
|
update();
|
|
// Get.snackbar('Failure', '', backgroundColor: Colors.red);
|
|
} else {
|
|
var jsonDecoeded = jsonDecode(res);
|
|
if (jsonDecoeded.isNotEmpty) {
|
|
if (jsonDecoeded['status'] == 'success' &&
|
|
jsonDecoeded['data'][0]['is_verified'] == 1) {
|
|
box.write(BoxName.emailDriver, jsonDecoeded['data'][0]['email']);
|
|
box.write(BoxName.phoneVerified,
|
|
jsonDecoeded['data'][0]['is_verified'].toString());
|
|
box.write(BoxName.phoneDriver, jsonDecoeded['data'][0]['phone']);
|
|
box.write(
|
|
BoxName.bankCodeDriver, jsonDecoeded['data'][0]['bankCode']);
|
|
box.write(BoxName.accountBankNumberDriver,
|
|
jsonDecoeded['data'][0]['accountBank']);
|
|
box.write(
|
|
BoxName.nameDriver,
|
|
'${jsonDecoeded['data'][0]['first_name']}'
|
|
' ${jsonDecoeded['data'][0]['last_name']}');
|
|
|
|
var token = await CRUD().get(
|
|
link: AppLink.getDriverToken,
|
|
payload: {'captain_id': box.read(BoxName.driverID).toString()});
|
|
|
|
if (token != 'failure') {
|
|
if (jsonDecode(token)['data'][0]['token'] !=
|
|
box.read(BoxName.tokenDriver)) {
|
|
Get.put(FirebaseMessagesController())
|
|
.sendNotificationToAnyWithoutData(
|
|
'token change',
|
|
'change device',
|
|
jsonDecode(token)['data'][0]['token'].toString(),
|
|
);
|
|
await CRUD().post(link: AppLink.addTokensDriver, payload: {
|
|
'token': box.read(BoxName.tokenDriver),
|
|
'captain_id': box.read(BoxName.driverID).toString()
|
|
});
|
|
}
|
|
}
|
|
|
|
Get.off(() => HomeCaptain());
|
|
} else {
|
|
Get.offAll(() => SmsSignupEgypt());
|
|
// Get.snackbar(jsonDecoeded['status'], jsonDecoeded['data'],
|
|
// backgroundColor: Colors.redAccent);
|
|
isloading = false;
|
|
update();
|
|
}
|
|
} else {
|
|
Get.snackbar('failure'.tr, '', backgroundColor: AppColor.redColor);
|
|
isloading = false;
|
|
update();
|
|
}
|
|
}
|
|
}
|
|
|
|
void loginByBoxData() async {
|
|
Get.to(() => HomeCaptain());
|
|
await CRUD().post(link: AppLink.addTokensDriver, payload: {
|
|
'token': box.read(BoxName.tokenDriver).toString(),
|
|
'captain_id': box.read(BoxName.driverID).toString()
|
|
});
|
|
}
|
|
|
|
@override
|
|
void onInit() async {
|
|
super.onInit();
|
|
|
|
// if (box.read(BoxName.driverID).toString() != null) {
|
|
// loginByBoxData();
|
|
// }
|
|
}
|
|
}
|