This commit is contained in:
Hamza-Ayed
2023-10-06 12:02:34 +03:00
parent edc3a4348d
commit 54e948e93b
14 changed files with 505 additions and 75 deletions

View File

@@ -2,7 +2,9 @@ import 'package:get/get.dart';
import 'package:ride/constant/box_name.dart';
import 'dart:async';
import '../../../constant/links.dart';
import '../../../main.dart';
import '../../functions/crud.dart';
import '../../functions/location_controller.dart';
class HomeCaptainController extends GetxController {
@@ -50,6 +52,15 @@ class HomeCaptainController extends GetxController {
return totalDuration;
}
@override
void onInit() async {
await CRUD().post(link: AppLink.addTokensDriver, payload: {
'token': box.read(BoxName.tokenDriver),
'captain_id': box.read(BoxName.driverID).toString()
}).then((value) => print('cccc'));
super.onInit();
}
@override
void dispose() {
activeTimer?.cancel();

View File

@@ -4,8 +4,11 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_polyline_algorithm/google_polyline_algorithm.dart';
import 'package:ride/constant/box_name.dart';
import 'package:ride/constant/colors.dart';
import 'package:ride/controller/firebase/firbase_messge.dart';
import 'package:ride/controller/functions/location_controller.dart';
import 'package:ride/main.dart';
import '../../../constant/credential.dart';
import '../../../constant/links.dart';
@@ -19,6 +22,70 @@ class MapDirection extends GetxController {
final List<LatLng> polylineCoordinates = [];
List<Polyline> polyLines = [];
Set<Marker> markers = {};
late String passengerLocation;
late String duration;
late String distance;
late String name;
late String phone;
late String rideId;
late String tokenPassenger;
late String durationToPassenger;
late String walletChecked;
late String direction;
bool isPassengerInfoWindow = false;
bool isBtnRideBegin = false;
double passengerInfoWindow = Get.height * .32;
double progress = 0;
double progressToPassenger = 0;
bool isRideBegin = false;
int progressTimerToShowPassengerInfoWindowFromDriver = 25;
int remainingTimeToShowPassengerInfoWindowFromDriver = 25;
int remainingTimeToPassenger = 60;
bool isDriverNearPassengerStart = false;
GoogleMapController? mapController;
late LatLng myLocation;
void onMapCreated(GoogleMapController controller) {
LocationController locationController = Get.find<LocationController>();
myLocation = locationController.myLocation;
mapController = controller;
controller.getVisibleRegion();
controller.animateCamera(
CameraUpdate.newLatLng(myLocation),
);
update();
// Set up a timer or interval to trigger the marker update every 3 seconds.
Timer.periodic(const Duration(seconds: 3), (_) {
updateMarker();
});
}
void checkIsDriverNearPassenger() async {
if (isDriverNearPassengerStart) {
Timer.periodic(const Duration(seconds: 3), (timer) {
String driverLat =
Get.find<LocationController>().myLocation.latitude.toString();
String driverLng =
Get.find<LocationController>().myLocation.longitude.toString();
// Replace "passengerLat" and "passengerLng" with the actual passenger's location
String passengerLat = passengerLocation; // Set the passenger's latitude
String passengerLng = ""; // Set the passenger's longitude
// double distance = calculateDistance(
// double.parse(driverLat),
// double.parse(driverLng),
// double.parse(passengerLat),
// double.parse(passengerLng),
// );
// if (distance < 50) {
// print("Distance to passenger: $distance meters");
// }
});
}
}
void clearPolyline() {
polyLines = [];
@@ -26,29 +93,87 @@ class MapDirection extends GetxController {
update();
}
void changeRideToBegin() {
isRideBegin = true;
passengerInfoWindow = Get.height * .22;
update();
}
void startTimerToShowPassengerInfoWindowFromDriver() async {
for (int i = 0;
i <= progressTimerToShowPassengerInfoWindowFromDriver;
i++) {
await Future.delayed(const Duration(seconds: 1));
progress = i / progressTimerToShowPassengerInfoWindowFromDriver;
remainingTimeToShowPassengerInfoWindowFromDriver =
progressTimerToShowPassengerInfoWindowFromDriver - i;
if (remainingTimeToShowPassengerInfoWindowFromDriver == 0) {
isPassengerInfoWindow = true;
print(isPassengerInfoWindow);
update();
startTimerToShowDriverToPassengerDuration();
}
print(isPassengerInfoWindow);
print(remainingTimeToShowPassengerInfoWindowFromDriver);
update();
}
}
void startTimerToShowDriverToPassengerDuration() async {
for (int i = 0; i <= int.parse(durationToPassenger); i++) {
await Future.delayed(const Duration(seconds: 1));
progressToPassenger = i / int.parse(durationToPassenger);
remainingTimeToPassenger = int.parse(durationToPassenger) - i;
if (remainingTimeToPassenger == 0) {
isBtnRideBegin = true;
print(isBtnRideBegin);
update();
}
print(isBtnRideBegin);
print(remainingTimeToPassenger);
update();
}
}
beginRideFromDriver() async {
changeRideToBegin();
await CRUD().post(link: AppLink.updateRides, payload: {
'id': rideId,
'rideTimeStart': DateTime.now().toString(),
'status': 'Applied'
});
FirebaseMessagesController().sendNotificationToAnyWithoutData('RideIsBegin',
box.read(BoxName.tokenDriver).toString(), tokenPassenger);
}
void updateMarker() {
// Remove the existing marker with the ID `MyLocation`.
markers.remove(MarkerId('MyLocation'.tr));
// Add a new marker with the ID `MyLocation` at the current location of the user.
LocationController locationController = Get.find<LocationController>();
markers.add(Marker(
markerId: MarkerId('MyLocation'.tr),
position: locationController.myLocation,
draggable: true,
icon: carIcon,
// infoWindow: const InfoWindow(
// title: 'Time',
// ),
));
myLocation = locationController.myLocation;
markers.add(
Marker(
markerId: MarkerId('MyLocation'.tr),
position: locationController.myLocation,
draggable: true,
icon: carIcon,
// infoWindow: const InfoWindow(
// title: 'Time',
// ),
),
);
// Update the `markers` set and call the `update()` method on the controller to notify the view that the marker position has changed.
update();
// Reload the marker after 3 seconds.
Future.delayed(const Duration(seconds: 3), () {
updateMarker();
});
// No recursive call here. The marker update will be triggered externally.
// Optionally, you can animate the camera to the new location after updating the marker.
mapController!.animateCamera(
CameraUpdate.newLatLng(locationController.myLocation),
);
}
void addCustomCarIcon() {
@@ -106,17 +231,34 @@ class MapDirection extends GetxController {
}
}
void changePassengerInfoWindow() {
isPassengerInfoWindow = !isPassengerInfoWindow;
passengerInfoWindow = isPassengerInfoWindow == true ? 200 : 0;
update();
}
@override
void onInit() {
// Get the passenger location from the arguments.
String passengerLocation = Get.arguments['passengerLocation'];
passengerLocation = Get.arguments['passengerLocation'];
duration = Get.arguments['Duration'];
distance = Get.arguments['Distance'];
name = Get.arguments['name'];
phone = Get.arguments['phone'];
walletChecked = Get.arguments['WalletChecked'];
tokenPassenger = Get.arguments['tokenPassenger'];
direction = Get.arguments['direction'];
durationToPassenger = Get.arguments['DurationToPassenger'];
rideId = Get.arguments['rideId'];
String lat = Get.find<LocationController>().myLocation.latitude.toString();
String lng = Get.find<LocationController>().myLocation.longitude.toString();
String origin = '$lat,$lng';
// Set the origin and destination coordinates for the Google Maps directions request.
getMap(origin, passengerLocation);
addCustomCarIcon();
updateMarker();
// updateMarker();
startTimerToShowPassengerInfoWindowFromDriver();
// checkIsDriverNearPassenger();
super.onInit();
}
}

View File

@@ -50,12 +50,12 @@ class MapController extends GetxController {
bool isCancelRidePageShown = false;
bool isCashConfirmPageShown = false;
bool isPaymentMethodPageShown = false;
bool isPassengerInfoWindow = false;
bool rideConfirm = false;
bool isMainBottomMenuMap = true;
double heightBottomSheetShown = 0;
double cashConfirmPageShown = 250;
double passengerInfoWindow = 250;
double widthMapTypeAndTraffic = 50;
double paymentPageShown = Get.height * .6;
late LatLng southwest;
@@ -92,12 +92,6 @@ class MapController extends GetxController {
update();
}
void changePassengerInfoWindow() {
isPassengerInfoWindow = !isPassengerInfoWindow;
passengerInfoWindow = isPassengerInfoWindow == true ? 200 : 0;
update();
}
void changePaymentMethodPageShown() {
isPaymentMethodPageShown = !isPaymentMethodPageShown;
paymentPageShown = isPaymentMethodPageShown == true ? Get.height * .6 : 0;
@@ -203,8 +197,6 @@ class MapController extends GetxController {
// List<String> body = [
rideId = jsonDecode(value)['message'];
List<String> body = [
// '${data[0]['start_address']}',
// '${data[0]['end_address']}',
'${data[0]["start_location"]['lat']},${data[0]["start_location"]['lng']}',
'${data[0]["end_location"]['lat']},${data[0]["end_location"]['lng']}',
totalPassenger.toString(),
@@ -220,6 +212,8 @@ class MapController extends GetxController {
distanceByPassenger.toString(),
paymentController.isWalletChecked.toString(),
dataCarsLocationByPassenger['message'][carsOrder]['token'].toString(),
duration1.toString(),
rideId,
];
FirebaseMessagesController().sendNotificationToDriverMAP(
'Order',