This commit is contained in:
Hamza Aleghwairyeen
2024-03-27 12:01:28 +03:00
parent 0f79b2d86b
commit eb67f93e71
13 changed files with 304 additions and 171 deletions

View File

@@ -0,0 +1,34 @@
import 'package:geolocator/geolocator.dart';
class GeoLocation {
Future<Position> getCurrentLocation() async {
bool serviceEnabled;
LocationPermission permission;
// Check if location services are enabled.
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
// Location services are not enabled, so we request the user to enable it.
return Future.error('Location services are disabled.');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
// Permissions are denied, we cannot fetch the location.
return Future.error('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, we cannot request permissions.
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
}
// When we reach here, permissions are granted and we can fetch the location.
return await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
}
}

View File

@@ -60,6 +60,7 @@
// ''');
// });
// } else {
// await Permission.locationAlways.request();
// print('Location permission not granted');
// }
// }

View File

@@ -1,7 +1,6 @@
import 'dart:async';
import 'package:SEFER/constant/table_names.dart';
import 'package:SEFER/controller/home/captin/map_driver_controller.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';
@@ -10,23 +9,26 @@ 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/main.dart';
import 'package:permission_handler/permission_handler.dart';
import '../home/captin/map_driver_controller.dart';
// LocationController.dart
class LocationController extends GetxController {
LocationData? _currentLocation;
late Location location;
bool isLoading = false;
bool isActive = false;
late LatLng myLocation;
late double heading;
late double accuracy;
late double heading = 0;
late double accuracy = 0;
LatLng? previousLocation;
late double latitude;
late double totalDistance;
late double longitude;
late DateTime time;
late double speed;
late double speedAccuracy;
late double headingAccuracy;
late double speed = 0;
late double speedAccuracy = 0;
late double headingAccuracy = 0;
bool isActive = false;
late LatLng myLocation;
String totalPoints = '0';
LocationData? get currentLocation => _currentLocation;
Timer? _locationTimer;
@@ -34,9 +36,6 @@ class LocationController extends GetxController {
@override
void onInit() async {
super.onInit();
requestLocationPermission();
location = Location();
getLocation();
// startLocationUpdates();
@@ -44,20 +43,6 @@ class LocationController extends GetxController {
totalPoints = Get.put(CaptainWalletController()).totalPoints;
}
Future<void> requestLocationPermission() async {
if (box.read(BoxName.driverID) != null) {
var status = await Permission.locationAlways.status;
if (!status.isGranted) {
await Permission.locationAlways.request();
} else {
var status = await Permission.locationWhenInUse.status;
if (!status.isGranted) {
await Permission.locationWhenInUse.request();
}
}
}
}
Future<void> startLocationUpdates() async {
if (box.read(BoxName.driverID) != null) {
_locationTimer =
@@ -70,32 +55,32 @@ class LocationController extends GetxController {
print('total point is $totalPoints');
await getLocation();
if (box.read(BoxName.driverID) != null) {
await CRUD()
.post(link: AppLink.addCarsLocationByPassenger, payload: {
'driver_id': box.read(BoxName.driverID).toString(),
// if (box.read(BoxName.driverID) != null) {
await CRUD()
.post(link: AppLink.addCarsLocationByPassenger, payload: {
'driver_id': box.read(BoxName.driverID).toString(),
'latitude': myLocation.latitude.toString(),
'longitude': myLocation.longitude.toString(),
'heading': heading.toString(),
'speed': speed.toString(),
'status': box.read(BoxName.statusDriverLocation).toString()
});
if (Get.find<MapDriverController>().rideId == null) {
await sql.insertData({
'driver_id': box.read(BoxName.driverID),
'latitude': myLocation.latitude.toString(),
'longitude': myLocation.longitude.toString(),
'heading': heading.toString(),
'speed': speed.toString(),
'status': box.read(BoxName.statusDriverLocation).toString()
});
if (Get.find<MapDriverController>().rideId == null) {
await sql.insertData({
'driver_id': box.read(BoxName.driverID),
'latitude': myLocation.latitude.toString(),
'longitude': myLocation.longitude.toString(),
'created_at': DateTime.now().toString(),
}, TableName.carLocations);
} else {
await sql.insertData({
'order_id': Get.find<MapDriverController>().rideId,
'created_at': DateTime.now().toString(),
'lat': myLocation.latitude.toString(),
'lng': myLocation.longitude.toString(),
}, TableName.rideLocation);
}
'created_at': DateTime.now().toString(),
}, TableName.carLocations);
} else {
await sql.insertData({
'order_id': Get.find<MapDriverController>().rideId,
'created_at': DateTime.now().toString(),
'lat': myLocation.latitude.toString(),
'lng': myLocation.longitude.toString(),
}, TableName.rideLocation);
}
// }
//
}
@@ -114,32 +99,54 @@ class LocationController extends GetxController {
}
Future<void> getLocation() async {
isLoading = true;
update();
// isLoading = true;
// update();
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
return;
}
}
// Configure location accuracy
// LocationAccuracy desiredAccuracy = LocationAccuracy.high;
// Get the current location
LocationData _locationData = await location.getLocation();
myLocation = LatLng(_locationData.latitude!, _locationData.longitude!);
accuracy = _locationData.accuracy!;
latitude = _locationData.latitude!;
longitude = _locationData.longitude!;
heading = _locationData.heading!;
speed = _locationData.speed!;
speedAccuracy = _locationData.speedAccuracy!;
headingAccuracy = _locationData.headingAccuracy!;
myLocation =
(_locationData.latitude != null && _locationData.longitude != null
? LatLng(_locationData.latitude!, _locationData.longitude!)
: null)!;
// Calculate the distance between the current location and the previous location
if (previousLocation != null) {
// double distance = myLocation.distanceBetween(previousLocation);
// totalDistance += distance;
}
previousLocation = myLocation;
// Print location details
// print('myLocation: $myLocation');
// print('Accuracy: $accuracy');
// print('Latitude: $latitude');
// print('Longitude: $longitude');
// print('heading: $heading');
// print('speed: $speed');
// print('speedAccuracy: $speedAccuracy');
// print('headingAccuracy: $headingAccuracy');
isLoading = false;
// print('myLocation: ${myLocation}');
// print('Accuracy: ${_locationData.accuracy}');
// print('Latitude: ${_locationData.latitude}');
// print('Longitude: ${_locationData.longitude}');
// print('Time: ${_locationData.time}');
// isLoading = false;
update();
}
}

View File

@@ -201,6 +201,7 @@ class HomeCaptainController extends GetxController {
await getlocation();
onButtonSelected();
await getPaymentToday();
await getDriverRate();
getCountRideToday();
getAllPayment();
startPeriodicExecution();
@@ -226,7 +227,7 @@ class HomeCaptainController extends GetxController {
payload: {'driverID': box.read(BoxName.driverID).toString()});
data = jsonDecode(res);
totalMoneyToday = data['message'][0]['todayAmount'];
rating = data['message'][0]['rating'].toString();
update();
}
@@ -241,6 +242,16 @@ class HomeCaptainController extends GetxController {
update();
}
getDriverRate() async {
var res = await CRUD().get(
link: AppLink.getDriveRrate,
payload: {'driver_id': box.read(BoxName.driverID).toString()});
data = jsonDecode(res);
rating = data['message'][0]['rating'].toString();
update();
}
getAllPayment() async {
var res = await CRUD().get(
link: AppLink.getAllPaymentFromRide,

View File

@@ -1016,7 +1016,7 @@ class MapPassengerController extends GetxController {
update();
}
Future<String> getRideStatus(int rideId) async {
Future<String> getRideStatus(String rideId) async {
final response = await CRUD()
.get(link: AppLink.getRideStatus, payload: {'order_id': rideId});
@@ -1026,9 +1026,10 @@ class MapPassengerController extends GetxController {
late String driverCarModel, driverCarMake, driverLicensePlate, driverName;
getUpdatedRideForDriverApply(String rideId) async {
// if (isDriversTokensSend) {
final response =
final res =
await CRUD().get(link: AppLink.getRideOrderID, payload: {'id': rideId});
if (response != 'failure') {
if (res != 'failure') {
var response = jsonDecode(res);
driverId = response['data']['driver_id'];
driverPhone = response['data']['phone'];
driverCarMake = response['data']['make'];