Update: 2026-07-12 18:53:59
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// transit_driver_controller.dart — تحكم وضع الباص (جهة السائق)
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:intaleq_maps/intaleq_maps.dart' show LatLng;
|
||||
|
||||
@@ -18,6 +19,13 @@ class TransitDriverController extends GetxController {
|
||||
TransitDriverTrip? activeTrip;
|
||||
bool isActionInProgress = false;
|
||||
|
||||
// ── حالة التفعيل ──────────────────────────────────────────────
|
||||
bool isActivating = false;
|
||||
String activationError = '';
|
||||
|
||||
// ── جيوفينس المحطات — آخر محطة تم الإعلان عنها ──────────────
|
||||
int _lastAnnouncedStopSeq = 0;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
@@ -41,18 +49,35 @@ class TransitDriverController extends GetxController {
|
||||
update();
|
||||
}
|
||||
|
||||
/// تفعيل الحساب بتوكن الدعوة
|
||||
Future<void> activateWithToken(String token) async {
|
||||
if (token.trim().isEmpty) return;
|
||||
isActivating = true;
|
||||
activationError = '';
|
||||
update();
|
||||
|
||||
final res = await TransitDriverService.activateByInviteToken(token.trim());
|
||||
if (res.success) {
|
||||
// أعد فحص الحالة بعد التفعيل
|
||||
await checkBusDriverStatus();
|
||||
} else {
|
||||
activationError = res.message;
|
||||
}
|
||||
isActivating = false;
|
||||
update();
|
||||
}
|
||||
|
||||
Future<void> fetchTodayTrips() async {
|
||||
if (driverTransitId == null) return;
|
||||
isLoadingTrips = true;
|
||||
update();
|
||||
|
||||
final res = await TransitDriverService.getTodayTrips(driverTransitId!);
|
||||
final res = await TransitDriverService.getTodayTrips();
|
||||
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,
|
||||
@@ -66,7 +91,7 @@ class TransitDriverController extends GetxController {
|
||||
}
|
||||
|
||||
Future<bool> startTrip(TransitDriverTrip trip) async {
|
||||
if (driverTransitId == null || isActionInProgress) return false;
|
||||
if (isActionInProgress) return false;
|
||||
isActionInProgress = true;
|
||||
update();
|
||||
|
||||
@@ -77,12 +102,12 @@ class TransitDriverController extends GetxController {
|
||||
|
||||
final res = await TransitDriverService.startTrip(
|
||||
tripId: trip.id,
|
||||
driverTransitId: driverTransitId!,
|
||||
lat: pos.latitude,
|
||||
lng: pos.longitude,
|
||||
);
|
||||
|
||||
if (res.success) {
|
||||
_lastAnnouncedStopSeq = 0;
|
||||
if (Get.isRegistered<LocationController>()) {
|
||||
Get.find<LocationController>().setBusMode(
|
||||
enabled: true,
|
||||
@@ -101,16 +126,14 @@ class TransitDriverController extends GetxController {
|
||||
}
|
||||
|
||||
Future<bool> endTrip(TransitDriverTrip trip) async {
|
||||
if (driverTransitId == null || isActionInProgress) return false;
|
||||
if (isActionInProgress) return false;
|
||||
isActionInProgress = true;
|
||||
update();
|
||||
|
||||
final res = await TransitDriverService.endTrip(
|
||||
tripId: trip.id,
|
||||
driverTransitId: driverTransitId!,
|
||||
);
|
||||
final res = await TransitDriverService.endTrip(tripId: trip.id);
|
||||
|
||||
if (res.success) {
|
||||
_lastAnnouncedStopSeq = 0;
|
||||
if (Get.isRegistered<LocationController>()) {
|
||||
Get.find<LocationController>().setBusMode(enabled: false);
|
||||
}
|
||||
@@ -124,15 +147,57 @@ class TransitDriverController extends GetxController {
|
||||
return res.success;
|
||||
}
|
||||
|
||||
Future<bool> reportDelay(TransitDriverTrip trip, int minutes, {String? reason}) async {
|
||||
if (driverTransitId == null) return false;
|
||||
Future<bool> reportDelay(TransitDriverTrip trip, int minutes,
|
||||
{String? reason}) async {
|
||||
final res = await TransitDriverService.reportDelay(
|
||||
tripId: trip.id,
|
||||
driverTransitId: driverTransitId!,
|
||||
delayMinutes: minutes,
|
||||
reason: reason,
|
||||
);
|
||||
if (!res.success) Get.snackbar('مواصلاتي', res.message);
|
||||
return res.success;
|
||||
}
|
||||
|
||||
// ── كشف الوصول للمحطات (جيوفينس) ────────────────────────────
|
||||
// تُستدعى من LocationController عند كل تحديث للموقع أثناء وضع الباص
|
||||
void onLocationUpdate(LatLng pos) {
|
||||
if (activeTrip == null || activeTrip!.stops.isEmpty) return;
|
||||
|
||||
final stops = activeTrip!.stops;
|
||||
int? nextSeq;
|
||||
|
||||
// الاتجاه للمحطة التالية بعد آخر معلنة
|
||||
for (final stop in stops) {
|
||||
if (stop.sequence <= _lastAnnouncedStopSeq) continue;
|
||||
final dist = Geolocator.distanceBetween(
|
||||
pos.latitude, pos.longitude,
|
||||
stop.latitude, stop.longitude,
|
||||
);
|
||||
if (dist <= stop.geofenceRadius) {
|
||||
nextSeq = stop.sequence;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (nextSeq != null && nextSeq > _lastAnnouncedStopSeq) {
|
||||
_lastAnnouncedStopSeq = nextSeq;
|
||||
// أرسل التسلسل الحالي عبر السوكيت
|
||||
if (Get.isRegistered<LocationController>()) {
|
||||
final lc = Get.find<LocationController>();
|
||||
lc.emitBusLocationToSocket(pos, 0, 0, currentStopSeq: nextSeq);
|
||||
}
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
// عودة بالمحطة الحالية للعرض
|
||||
TransitStop? get currentStop {
|
||||
if (activeTrip == null || _lastAnnouncedStopSeq == 0) return null;
|
||||
try {
|
||||
return activeTrip!.stops
|
||||
.firstWhere((s) => s.sequence == _lastAnnouncedStopSeq);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user