This commit is contained in:
Hamza-Ayed
2023-08-13 23:03:58 +03:00
parent 8b46545fbc
commit 28f4f8635d
24 changed files with 742 additions and 70 deletions

View File

@@ -1,11 +1,13 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:get/get.dart';
import 'package:ride/constant/box_name.dart';
import 'package:ride/constant/links.dart';
import 'package:ride/controller/functions/crud.dart';
import 'package:ride/main.dart';
import 'package:ride/views/home/map_page.dart';
class LoginController extends GetxController {
final formKey = GlobalKey<FormState>();
@@ -13,6 +15,8 @@ class LoginController extends GetxController {
TextEditingController phoneController = TextEditingController();
TextEditingController passwordController = TextEditingController();
bool isAgreeTerms = false;
bool isloading = false;
final FlutterSecureStorage _storage = FlutterSecureStorage();
void changeAgreeTerm() {
isAgreeTerms = !isAgreeTerms;
@@ -25,26 +29,60 @@ class LoginController extends GetxController {
update();
}
void saveData(String key, value) async {
await _storage.write(key: key, value: value);
}
Future<String?> readData(String boxName) async {
final String? value = await _storage.read(key: boxName);
return value;
}
void login() async {
Map<String, dynamic> res = await CRUD().post(link: AppLink.login, payload: {
isloading = true;
update();
var res = await CRUD().get(link: AppLink.login, payload: {
'email': emailController.text,
'phone': phoneController.text,
'password': passwordController.text
});
if (res.isNotEmpty) {
if (res['status'] == 'success1') {
print(res);
} else {
print('noooooooooooooooo');
var jsonDecoeded = jsonDecode(res);
// print(jsonDecoeded);
if (jsonDecoeded.isNotEmpty) {
if (jsonDecoeded['status'] == 'success') {
box.write(BoxName.pasengerID, jsonDecoeded['data'][0]['id']);
box.write(BoxName.email, jsonDecoeded['data'][0]['email']);
box.write(BoxName.phone, jsonDecoeded['data'][0]['phone']);
Get.offAll(() => MapPage());
isloading = false;
update();
print(box.read(BoxName.pasengerID).toString());
await CRUD().post(link: AppLink.addTokens, payload: {
'token': box.read(BoxName.tokenFCM.toString()),
'passengerID': box.read(BoxName.pasengerID).toString()
}).then((value) => print('cccc'));
} else if (jsonDecoeded['status'] == 'Failure') {
Get.snackbar(jsonDecoeded['status'], jsonDecoeded['data'],
backgroundColor: Colors.redAccent);
isloading = false;
update();
}
} else {
print('res is null');
}
}
goToMapPage() {
if (box.read(BoxName.email) != null) {
Get.offAll(() => const MapPage());
}
}
@override
void onInit() {
super.onInit();
goToMapPage();
}
}

View File

@@ -1,5 +1,13 @@
import 'dart:convert';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ride/constant/links.dart';
import 'package:ride/controller/functions/crud.dart';
import 'package:ride/views/home/map_page.dart';
import '../../views/auth/verify_email_page.dart';
class RegisterController extends GetxController {
final formKey = GlobalKey<FormState>();
@@ -9,8 +17,11 @@ class RegisterController extends GetxController {
TextEditingController emailController = TextEditingController();
TextEditingController phoneController = TextEditingController();
TextEditingController passwordController = TextEditingController();
TextEditingController siteController = TextEditingController();
TextEditingController verfyCode = TextEditingController();
String birthDate = 'Birth Date'.tr;
String gender = 'Male'.tr;
@override
void onInit() {
super.onInit();
@@ -40,9 +51,41 @@ class RegisterController extends GetxController {
);
}
void register() {
if (formKey.currentState!.validate()) {
// Do something with the data
void changeGender(String value) {
gender = value;
update();
}
sendverfications() async {
var res = await CRUD().post(link: AppLink.verifyEmail, payload: {
'email': emailController.text,
'token': verfyCode.text,
});
var jsondecod = jsonDecode(res);
if (jsondecod['status'] == 'success') {
Get.offAll(() => const MapPage());
}
}
void register() async {
if (formKey.currentState!.validate()) {
await CRUD().post(link: AppLink.signUp, payload: {
'first_name': firstNameController.text,
'last_name': lastNameController.text,
'email': emailController.text,
'phone': phoneController.text,
'password': passwordController.text,
'gender': gender,
'site': siteController.text,
'birthdate': birthDate,
}).then((value) => print(value));
}
int randomNumber = Random().nextInt(100000) + 1;
await CRUD().post(link: AppLink.sendVerifyEmail, payload: {
'email': emailController.text,
'token': randomNumber.toString(),
}).then((value) => print(value));
Get.to(() => const VerifyEmailPage());
}
}

View File

@@ -0,0 +1,39 @@
import 'dart:convert';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
import '../../constant/box_name.dart';
import '../../constant/credential.dart';
import '../../constant/links.dart';
import '../../main.dart';
class TokenController extends GetxController {
bool isloading = false;
Future addToken() async {
isloading = true;
update();
var res = await http.post(
Uri.parse(AppLink.addTokens),
headers: {
'Authorization':
'Basic ${base64Encode(utf8.encode(AppCredintials.basicAuthCredentials))}',
},
body: {
'token': box.read(BoxName.tokenFCM.toString()),
'passengerID': box.read(BoxName.pasengerID).toString()
},
);
isloading = false;
update();
print(res.request);
print(res.body);
var jsonToken = jsonDecode(res.body);
// print(jsonToken);
if (jsonToken['status'] == 'The token has been updated successfully.') {
Get.snackbar('token updated', 'message');
}
}
}

View File

@@ -0,0 +1,16 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ride/constant/links.dart';
import 'package:ride/controller/functions/crud.dart';
class VerifyEmailController extends GetxController {
TextEditingController verfyCode = TextEditingController();
@override
void onInit() async {
super.onInit();
}
sendverfications() async {
await CRUD().post(link: AppLink.sendVerifyEmail);
}
}

View File

@@ -0,0 +1,249 @@
import 'dart:convert';
import 'dart:io';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
import '../../constant/box_name.dart';
import '../../constant/credential.dart';
import '../../constant/links.dart';
import '../../main.dart';
class FirebasMessagesController extends GetxController {
final fcmToken = FirebaseMessaging.instance;
List<String> tokens = [];
List dataTokens = [];
NotificationSettings? notificationSettings;
@override
void onInit() async {
super.onInit();
// getNotificationSettings();
getToken();
getTokens();
}
Future<void> getNotificationSettings() async {
// Get the current notification settings.
notificationSettings =
await FirebaseMessaging.instance.getNotificationSettings();
print('------noti======');
print(notificationSettings!.authorizationStatus.toString());
update();
}
void requestPermission() async {
// Check if the platform is Android
if (Platform.isAndroid) {
// Request permission for Android
fcmToken.requestPermission().then((value) {
if (value!.authorizationStatus == AuthorizationStatus.denied) {
print('dddddddddddddddddddddd');
}
});
} else {
// Request permission for iOS
NotificationSettings settings = await fcmToken.requestPermission(
alert: true,
announcement: true,
badge: true,
carPlay: true,
criticalAlert: true,
provisional: false,
sound: true,
);
print('User granted permission: ${settings.authorizationStatus}');
fcmToken.setForegroundNotificationPresentationOptions(
alert: true, badge: true, sound: true);
}
}
Future getTokens() async {
var res = await http.post(
Uri.parse(AppLink.getTokens),
headers: {
'Authorization':
'Basic ${base64Encode(utf8.encode(AppCredintials.basicAuthCredentials))}',
},
body: {},
);
// print(res.body);
var jsonResponse = jsonDecode(res.body);
// print(jsonResponse);
if (jsonResponse['status'] == 'success') {
dataTokens = jsonResponse['data'];
for (var i = 0; i < dataTokens.length; i++) {
tokens.add(jsonResponse['data'][i]['token']);
}
box.write(BoxName.tokens, tokens);
print(box.read(BoxName.tokens));
} else {
Get.defaultDialog(title: "Warning", middleText: "Server Error");
}
}
void getToken() async {
// final SharedPreferences prefs = await SharedPreferences.getInstance();
await fcmToken.getToken().then(
(token) {
// print(token);/
box.write(BoxName.tokenFCM, token);
},
);
FirebaseMessaging.onMessage.listen((event) {
print(
'onMessage: ${event.notification!.title} ${event.notification!.body}');
if (Get.currentRoute == '/') {
// If the app is in the foreground, show a dialog to the user
if (event.notification!.title.toString().contains('reef')) {
// Get.snackbar('reeffff', 'message');
} else if (event.notification!.title.toString().contains('انذار')) {
// Get.to(() => const ProfilePage());
} else if (event.notification!.title
.toString()
.contains('تعديل طلبيات')) {}
} else if (event.notification!.title.toString().contains('Deleted')) {
// LogOutController().removeKeys(prefs);
} else {
// If the app is in the background or terminated, show a system tray message
RemoteNotification? notification = event.notification;
AndroidNotification? android = event.notification?.android;
// if (notification != null && android != null) {
if (notification != null && android != null) {
print(
'onMessageOpenedApp: ${notification.title} ${notification.body}');
if (event.notification!.title.toString().contains('reef')) {
// Get.snackbar('reef', '');
// SendGpsNow().getSiteNotification();
} else if (event.notification!.title.toString().contains('انذار')) {
// Get.to(() => const ProfilePage());
} else if (event.notification!.title
.toString()
.contains('تعديل طلبيات')) {
// Get.to(() => const OrdersDriverPage());
} else if (event.notification!.title.toString().contains('Deleted')) {
// LogOutController().removeKeys(prefs);
}
}
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage event) {
print(
'onMessageOpenedApp: ${event.notification!.title} ${event.notification!.body}');
RemoteNotification? notification = event.notification;
if (notification != null) {
print('onMessageOpenedApp: ${notification.title} ${notification.body}');
if (event.notification!.title.toString().contains('reef')) {
// Get.snackbar('reef', '');
// SendGpsNow().getSiteNotification();
} else if (event.notification!.title.toString().contains('انذار')) {
// Get.to(() => const ProfilePage());
} else if (event.notification!.title
.toString()
.contains('تعديل طلبيات')) {
// Get.to(() => const OrdersDriverPage());
} else if (event.notification!.title.toString().contains('Deleted')) {
// LogOutController().removeKeys(prefs);
}
}
});
}
void sendNotificationAll(String title, body) async {
// Get the token you want to subtract.
String token = box.read(BoxName.tokenFCM);
tokens = box.read(BoxName.tokens);
// Subtract the token from the list of tokens.
tokens.remove(token);
// Save the list of tokens back to the box.
// box.write(BoxName.tokens, tokens);
tokens = box.read(BoxName.tokens);
// print(tokens);
for (var i = 0; i < tokens.length; i++) {
http
.post(Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=${AppCredintials.serverAPI}'
},
body: jsonEncode({
'notification': <String, dynamic>{
'title': title,
'body': body,
'sound': 'true'
},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': tokens[i],
}))
.whenComplete(() {})
.catchError((e) {
print('sendNotification() error: $e');
});
}
}
void sendNotificationToManager(String title, body) async {
// tokens = box.read(BoxName.tokenManager);
for (var i = 0; i < tokens.length; i++) {
http
.post(Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=${AppCredintials.serverAPI}'
},
body: jsonEncode({
'notification': <String, dynamic>{
'title': title,
'body': body,
'sound': 'true'
},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': tokens[i],
}))
.whenComplete(() {})
.catchError((e) {
print('sendNotification() error: $e');
});
}
}
void sendNotificationDriverId(String title, body, token) async {
http
.post(Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=${AppCredintials.serverAPI}'
},
body: jsonEncode({
'notification': <String, dynamic>{
'title': title,
'body': body,
'sound': 'true'
},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': token,
}))
.whenComplete(() {})
.catchError((e) {
print('sendNotification() error: $e');
});
}
}

View File

@@ -25,9 +25,10 @@ class CRUD {
if (response.statusCode == 200) {
var jsonData = jsonDecode(response.body);
if (jsonData['status'] == 'success') {
print(jsonData['message']);
return jsonData['message'];
print(jsonData['data']);
return response.body;
}
return response.body;
}
}
@@ -47,7 +48,7 @@ class CRUD {
'Basic ${base64Encode(utf8.encode(AppCredintials.basicAuthCredentials))}',
},
);
print(response.request);
if (response.statusCode == 200) {
var jsonData = jsonDecode(response.body);
if (jsonData['status'] == 'success') {
@@ -57,12 +58,9 @@ class CRUD {
);
return response.body;
} else if (jsonData['status'] == 'success1') {
print(jsonData['data']);
return jsonData;
}
}
return (response.body);
// return (response.body);
}
Future<dynamic> getGoogleApi({

View File

@@ -0,0 +1,16 @@
import 'package:location/location.dart';
import 'package:get/get.dart';
class LocationPermissions {
late Location location;
Future locationPermissions() async {
location = Location();
var permissionStatus = await location.requestPermission();
if (permissionStatus == PermissionStatus.denied) {
// The user denied the location permission.
Get.defaultDialog(title: 'GPS Required Allow !.'.tr, middleText: '');
return null;
}
}
}

View File

@@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
@@ -167,8 +169,10 @@ class MapController extends GetxController {
}
Future getCarsLocationByPassenger() async {
List data =
var res =
await CRUD().get(link: AppLink.getCarsLocationByPassenger, payload: {});
List data = jsonDecode(res);
print(data);
for (var i = 0; i < data.length; i++) {
carsLocationByPassenger.add(LatLng(double.parse(data[i]['latitude']),
double.parse(data[i]['longitude'])));