293 lines
8.5 KiB
Dart
293 lines
8.5 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:SEFER/constant/links.dart';
|
|
import 'package:SEFER/main.dart';
|
|
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
|
import 'dart:math' as math;
|
|
import '../../../constant/box_name.dart';
|
|
import '../../functions/audio_controller.dart';
|
|
import '../../functions/crud.dart';
|
|
import '../../functions/location_controller.dart';
|
|
import 'home_captain_controller.dart';
|
|
|
|
class OrderRequestController extends GetxController {
|
|
double progress = 0;
|
|
double progressSpeed = 0;
|
|
int duration = 11;
|
|
int durationSpeed = 20;
|
|
int remainingTime = 0;
|
|
int remainingTimeSpeed = 0;
|
|
String countRefuse = '0';
|
|
bool applied = false;
|
|
final locationController = Get.put(LocationController());
|
|
BitmapDescriptor startIcon = BitmapDescriptor.defaultMarker;
|
|
BitmapDescriptor endIcon = BitmapDescriptor.defaultMarker;
|
|
final arguments = Get.arguments;
|
|
var myList;
|
|
late int hours;
|
|
late int minutes;
|
|
|
|
@override
|
|
void onInit() {
|
|
// AudioController audioController = Get.put(AudioController());
|
|
|
|
// audioController.playAudio();
|
|
// getRefusedOrderByCaptain();
|
|
initilizeOrderPage();
|
|
addCustomStartIcon();
|
|
addCustomEndIcon();
|
|
|
|
// calculateConsumptionFuel();
|
|
update();
|
|
super.onInit();
|
|
}
|
|
|
|
late LatLngBounds bounds;
|
|
late List<LatLng> pointsDirection;
|
|
late String body;
|
|
late double latPassengerLocation;
|
|
late double lngPassengerLocation;
|
|
late double lngPassengerDestination;
|
|
late double latPassengerDestination;
|
|
initilizeOrderPage() {
|
|
final myListString = arguments['myListString'];
|
|
// final myList = arguments['DriverList'];
|
|
if (arguments['DriverList'] == null || arguments['DriverList'].isEmpty) {
|
|
myList = jsonDecode(myListString);
|
|
} else {
|
|
myList = arguments['DriverList'];
|
|
}
|
|
|
|
body = arguments['body'];
|
|
Duration durationToAdd = Duration(seconds: int.parse(myList[4]));
|
|
hours = durationToAdd.inHours;
|
|
minutes = (durationToAdd.inMinutes % 60).round();
|
|
startTimerSpeed(myList[6].toString(), body.toString());
|
|
var coords = myList[0].split(',');
|
|
var coordDestination = myList[1].split(',');
|
|
|
|
// Parse to double
|
|
latPassengerLocation = double.parse(coords[0]);
|
|
lngPassengerLocation = double.parse(coords[1]);
|
|
latPassengerDestination = double.parse(coordDestination[0]);
|
|
lngPassengerDestination = double.parse(coordDestination[1]);
|
|
|
|
pointsDirection = [
|
|
LatLng(latPassengerLocation, lngPassengerLocation),
|
|
LatLng(latPassengerDestination, lngPassengerDestination)
|
|
]; // Calculate the midpoint between the two points
|
|
// Calculate the minimum and maximum latitude and longitude values
|
|
double minLatitude =
|
|
math.min(pointsDirection[0].latitude, pointsDirection[1].latitude);
|
|
double maxLatitude =
|
|
math.max(pointsDirection[0].latitude, pointsDirection[1].latitude);
|
|
double minLongitude =
|
|
math.min(pointsDirection[0].longitude, pointsDirection[1].longitude);
|
|
double maxLongitude =
|
|
math.max(pointsDirection[0].longitude, pointsDirection[1].longitude);
|
|
// Create a bounding box using the calculated values
|
|
bounds = LatLngBounds(
|
|
southwest: LatLng(minLatitude, minLongitude),
|
|
northeast: LatLng(maxLatitude, maxLongitude),
|
|
);
|
|
update();
|
|
}
|
|
|
|
getRideDEtailsForBackgroundOrder() async {
|
|
await CRUD().get(link: AppLink.getRidesDetails, payload: {
|
|
'id': box.read(BoxName.myList)[2].toString(),
|
|
});
|
|
}
|
|
|
|
void addCustomStartIcon() async {
|
|
// Create the marker with the resized image
|
|
|
|
ImageConfiguration config = ImageConfiguration(
|
|
size: const Size(30, 30), devicePixelRatio: Get.pixelRatio);
|
|
BitmapDescriptor.asset(
|
|
config,
|
|
'assets/images/A.png',
|
|
).then((value) {
|
|
startIcon = value;
|
|
update();
|
|
});
|
|
}
|
|
|
|
void addCustomEndIcon() {
|
|
ImageConfiguration config = ImageConfiguration(
|
|
size: const Size(30, 30), devicePixelRatio: Get.pixelRatio);
|
|
BitmapDescriptor.asset(
|
|
config,
|
|
'assets/images/b.png',
|
|
).then((value) {
|
|
endIcon = value;
|
|
update();
|
|
});
|
|
}
|
|
|
|
void changeApplied() {
|
|
applied = true;
|
|
update();
|
|
}
|
|
|
|
double mpg = 0;
|
|
calculateConsumptionFuel() {
|
|
mpg = Get.find<HomeCaptainController>().fuelPrice /
|
|
12; //todo in register car add mpg in box
|
|
}
|
|
|
|
bool _timerActive = false;
|
|
|
|
Future<void> startTimer(String driverID, String orderID) async {
|
|
_timerActive = true;
|
|
for (int i = 0; i <= duration && _timerActive; i++) {
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
progress = i / duration;
|
|
remainingTime = duration - i;
|
|
|
|
update();
|
|
}
|
|
if (remainingTime == 0 && _timerActive) {
|
|
if (applied == false) {
|
|
endTimer();
|
|
refuseOrder(orderID);
|
|
}
|
|
}
|
|
}
|
|
|
|
void endTimer() {
|
|
_timerActive = false;
|
|
}
|
|
|
|
void startTimerSpeed(String driverID, orderID) async {
|
|
for (int i = 0; i <= durationSpeed; i++) {
|
|
await Future.delayed(const Duration(seconds: 1));
|
|
progressSpeed = i / durationSpeed;
|
|
remainingTimeSpeed = durationSpeed - i;
|
|
|
|
update();
|
|
}
|
|
if (remainingTimeSpeed == 0) {
|
|
if (applied == false) {
|
|
Get.back();
|
|
// refuseOrder(box.read(BoxName.driverID), orderID);
|
|
}
|
|
}
|
|
}
|
|
|
|
void refuseOrder(
|
|
orderID,
|
|
) async {
|
|
await CRUD().postFromDialogue(link: AppLink.addDriverOrder, payload: {
|
|
//TODO need review
|
|
'driver_id': box.read(BoxName.driverID),
|
|
// box.read(BoxName.driverID).toString(),
|
|
'order_id': orderID,
|
|
'status': 'Refused'
|
|
});
|
|
await CRUD().post(link: AppLink.updateRides, payload: {
|
|
'id': orderID,
|
|
// 'rideTimeStart': DateTime.now().toString(),
|
|
'status': 'Refused',
|
|
'driver_id': box.read(BoxName.driverID),
|
|
});
|
|
CRUD().post(
|
|
link: '${AppLink.seferAlexandriaServer}/rides/update.php',
|
|
payload: {
|
|
'id': orderID,
|
|
// 'rideTimeStart': DateTime.now().toString(),
|
|
'status': 'Refused',
|
|
'driver_id': box.read(BoxName.driverID),
|
|
});
|
|
CRUD().post(link: '${AppLink.seferGizaServer}/rides/update.php', payload: {
|
|
'id': orderID,
|
|
// 'rideTimeStart': DateTime.now().toString(),
|
|
'status': 'Refused',
|
|
'driver_id': box.read(BoxName.driverID),
|
|
});
|
|
|
|
// applied = true;
|
|
// if (box.read(BoxName.gender).toString() != 'Female') {
|
|
// sql.insertData({
|
|
// 'order_id': orderID,
|
|
// 'created_at': DateTime.now().toString(),
|
|
// 'driver_id': box.read(BoxName.driverID).toString(),
|
|
// }, TableName.driverOrdersRefuse);
|
|
// getRefusedOrderByCaptain();
|
|
// }
|
|
update();
|
|
// Get.back();
|
|
// Get.offAll(HomeCaptain());
|
|
}
|
|
|
|
addRideToNotificationDriverString(
|
|
orderID,
|
|
String startLocation,
|
|
String endLocation,
|
|
String date,
|
|
String time,
|
|
String price,
|
|
String passengerId,
|
|
String status,
|
|
String carType,
|
|
String passengerRate,
|
|
String priceForPassenger,
|
|
String distance,
|
|
String duration,
|
|
) async {
|
|
await CRUD().post(link: AppLink.addWaitingRide, payload: {
|
|
'id': orderID,
|
|
'start_location': startLocation,
|
|
'end_location': endLocation,
|
|
'date': date,
|
|
'time': time,
|
|
'price': price,
|
|
'passenger_id': passengerId,
|
|
'status': status,
|
|
'carType': carType,
|
|
'passengerRate': passengerRate,
|
|
'price_for_passenger': priceForPassenger,
|
|
'distance': distance,
|
|
'duration': duration,
|
|
});
|
|
CRUD().post(
|
|
link:
|
|
'${AppLink.seferAlexandriaServer}/notificationCaptain/addWaitingRide.php',
|
|
payload: {
|
|
'id': orderID,
|
|
'start_location': startLocation,
|
|
'end_location': endLocation,
|
|
'date': date,
|
|
'time': time,
|
|
'price': price,
|
|
'passenger_id': passengerId,
|
|
'status': status,
|
|
'carType': carType,
|
|
'passengerRate': passengerRate,
|
|
'price_for_passenger': priceForPassenger,
|
|
'distance': distance,
|
|
'duration': duration,
|
|
});
|
|
CRUD().post(
|
|
link:
|
|
'${AppLink.seferGizaServer}/notificationCaptain/addWaitingRide.php',
|
|
payload: {
|
|
'id': orderID,
|
|
'start_location': startLocation,
|
|
'end_location': endLocation,
|
|
'date': date,
|
|
'time': time,
|
|
'price': price,
|
|
'passenger_id': passengerId,
|
|
'status': status,
|
|
'carType': carType,
|
|
'passengerRate': passengerRate,
|
|
'price_for_passenger': priceForPassenger,
|
|
'distance': distance,
|
|
'duration': duration,
|
|
});
|
|
}
|
|
}
|