Update: 2026-07-12 05:40:28
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
// transit_driver_controller.dart — تحكم وضع الباص (جهة السائق)
|
||||
import 'package:get/get.dart';
|
||||
import 'package:intaleq_maps/intaleq_maps.dart' show LatLng;
|
||||
|
||||
import '../functions/location_controller.dart';
|
||||
import 'transit_driver_models.dart';
|
||||
import 'transit_driver_service.dart';
|
||||
|
||||
class TransitDriverController extends GetxController {
|
||||
bool isCheckingBusDriver = true;
|
||||
bool isBusDriver = false;
|
||||
int? driverTransitId;
|
||||
String orgName = '';
|
||||
|
||||
bool isLoadingTrips = false;
|
||||
List<TransitDriverTrip> todayTrips = [];
|
||||
|
||||
TransitDriverTrip? activeTrip;
|
||||
bool isActionInProgress = false;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
checkBusDriverStatus();
|
||||
}
|
||||
|
||||
Future<void> checkBusDriverStatus() async {
|
||||
isCheckingBusDriver = true;
|
||||
update();
|
||||
|
||||
final res = await TransitDriverService.checkIsBusDriver();
|
||||
if (res.success && res.data != null) {
|
||||
isBusDriver = res.data!['is_bus_driver'] == true;
|
||||
if (isBusDriver) {
|
||||
driverTransitId = int.tryParse(res.data!['driver_transit_id'].toString());
|
||||
orgName = res.data!['org_name']?.toString() ?? '';
|
||||
await fetchTodayTrips();
|
||||
}
|
||||
}
|
||||
isCheckingBusDriver = false;
|
||||
update();
|
||||
}
|
||||
|
||||
Future<void> fetchTodayTrips() async {
|
||||
if (driverTransitId == null) return;
|
||||
isLoadingTrips = true;
|
||||
update();
|
||||
|
||||
final res = await TransitDriverService.getTodayTrips(driverTransitId!);
|
||||
if (res.success) {
|
||||
todayTrips = res.data ?? [];
|
||||
final started = todayTrips.where((t) => t.status == 'started');
|
||||
activeTrip = started.isNotEmpty ? started.first : null;
|
||||
|
||||
// إن كانت هناك رحلة قيد التشغيل بالفعل (مثلاً بعد إعادة فتح التطبيق)، فعّل وضع الباص
|
||||
if (activeTrip != null && Get.isRegistered<LocationController>()) {
|
||||
Get.find<LocationController>().setBusMode(
|
||||
enabled: true,
|
||||
tripId: activeTrip!.id,
|
||||
routeId: activeTrip!.routeId,
|
||||
);
|
||||
}
|
||||
}
|
||||
isLoadingTrips = false;
|
||||
update();
|
||||
}
|
||||
|
||||
Future<bool> startTrip(TransitDriverTrip trip) async {
|
||||
if (driverTransitId == null || isActionInProgress) return false;
|
||||
isActionInProgress = true;
|
||||
update();
|
||||
|
||||
LatLng pos = const LatLng(0, 0);
|
||||
if (Get.isRegistered<LocationController>()) {
|
||||
pos = Get.find<LocationController>().myLocation;
|
||||
}
|
||||
|
||||
final res = await TransitDriverService.startTrip(
|
||||
tripId: trip.id,
|
||||
driverTransitId: driverTransitId!,
|
||||
lat: pos.latitude,
|
||||
lng: pos.longitude,
|
||||
);
|
||||
|
||||
if (res.success) {
|
||||
if (Get.isRegistered<LocationController>()) {
|
||||
Get.find<LocationController>().setBusMode(
|
||||
enabled: true,
|
||||
tripId: trip.id,
|
||||
routeId: trip.routeId,
|
||||
);
|
||||
}
|
||||
await fetchTodayTrips();
|
||||
} else {
|
||||
Get.snackbar('مواصلاتي', res.message);
|
||||
}
|
||||
|
||||
isActionInProgress = false;
|
||||
update();
|
||||
return res.success;
|
||||
}
|
||||
|
||||
Future<bool> endTrip(TransitDriverTrip trip) async {
|
||||
if (driverTransitId == null || isActionInProgress) return false;
|
||||
isActionInProgress = true;
|
||||
update();
|
||||
|
||||
final res = await TransitDriverService.endTrip(
|
||||
tripId: trip.id,
|
||||
driverTransitId: driverTransitId!,
|
||||
);
|
||||
|
||||
if (res.success) {
|
||||
if (Get.isRegistered<LocationController>()) {
|
||||
Get.find<LocationController>().setBusMode(enabled: false);
|
||||
}
|
||||
await fetchTodayTrips();
|
||||
} else {
|
||||
Get.snackbar('مواصلاتي', res.message);
|
||||
}
|
||||
|
||||
isActionInProgress = false;
|
||||
update();
|
||||
return res.success;
|
||||
}
|
||||
|
||||
Future<bool> reportDelay(TransitDriverTrip trip, int minutes, {String? reason}) async {
|
||||
if (driverTransitId == null) return false;
|
||||
final res = await TransitDriverService.reportDelay(
|
||||
tripId: trip.id,
|
||||
driverTransitId: driverTransitId!,
|
||||
delayMinutes: minutes,
|
||||
reason: reason,
|
||||
);
|
||||
if (!res.success) Get.snackbar('مواصلاتي', res.message);
|
||||
return res.success;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user