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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
// transit_driver_models.dart — نماذج بيانات مواصلاتي (جهة سائق الباص)
|
||||
|
||||
class TransitStopInfo {
|
||||
final int sequence;
|
||||
final String nameAr;
|
||||
final double lat;
|
||||
final double lng;
|
||||
final int? etaOffsetMin;
|
||||
|
||||
TransitStopInfo({
|
||||
required this.sequence,
|
||||
required this.nameAr,
|
||||
required this.lat,
|
||||
required this.lng,
|
||||
this.etaOffsetMin,
|
||||
});
|
||||
|
||||
factory TransitStopInfo.fromJson(Map<String, dynamic> j) => TransitStopInfo(
|
||||
sequence: int.tryParse(j['sequence'].toString()) ?? 0,
|
||||
nameAr: j['name_ar']?.toString() ?? '',
|
||||
lat: double.tryParse(j['latitude']?.toString() ?? '0') ?? 0,
|
||||
lng: double.tryParse(j['longitude']?.toString() ?? '0') ?? 0,
|
||||
etaOffsetMin: j['eta_offset_min'] == null
|
||||
? null
|
||||
: int.tryParse(j['eta_offset_min'].toString()),
|
||||
);
|
||||
}
|
||||
|
||||
class TransitDriverTrip {
|
||||
final int id;
|
||||
final int routeId;
|
||||
final String status; // scheduled | started | completed | cancelled | no_show
|
||||
final int delayMinutes;
|
||||
final int? currentStopSeq;
|
||||
final String routeName;
|
||||
final String? departureTime;
|
||||
final String? vehiclePlate;
|
||||
final int? capacity;
|
||||
final List<TransitStopInfo> stops;
|
||||
|
||||
TransitDriverTrip({
|
||||
required this.id,
|
||||
required this.routeId,
|
||||
required this.status,
|
||||
required this.delayMinutes,
|
||||
required this.routeName,
|
||||
this.currentStopSeq,
|
||||
this.departureTime,
|
||||
this.vehiclePlate,
|
||||
this.capacity,
|
||||
this.stops = const [],
|
||||
});
|
||||
|
||||
factory TransitDriverTrip.fromJson(Map<String, dynamic> j) => TransitDriverTrip(
|
||||
id: int.tryParse(j['id'].toString()) ?? 0,
|
||||
routeId: int.tryParse(j['route_id']?.toString() ?? '0') ?? 0,
|
||||
status: j['status']?.toString() ?? '',
|
||||
delayMinutes: int.tryParse(j['delay_minutes']?.toString() ?? '0') ?? 0,
|
||||
currentStopSeq: j['current_stop_seq'] == null
|
||||
? null
|
||||
: int.tryParse(j['current_stop_seq'].toString()),
|
||||
routeName: j['route_name']?.toString() ?? '',
|
||||
departureTime: j['departure_time']?.toString(),
|
||||
vehiclePlate: j['vehicle_plate']?.toString(),
|
||||
capacity: j['capacity'] == null ? null : int.tryParse(j['capacity'].toString()),
|
||||
stops: (j['stops'] is List)
|
||||
? (j['stops'] as List)
|
||||
.map((s) => TransitStopInfo.fromJson(Map<String, dynamic>.from(s)))
|
||||
.toList()
|
||||
: const [],
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
// transit_driver_service.dart — طبقة الاتصال بـ backend/transit (جهة سائق الباص)
|
||||
import '../functions/crud.dart';
|
||||
import '../../constant/links.dart';
|
||||
import 'transit_driver_models.dart';
|
||||
|
||||
class TransitApiResult<T> {
|
||||
final bool success;
|
||||
final T? data;
|
||||
final String message;
|
||||
TransitApiResult(this.success, this.data, this.message);
|
||||
}
|
||||
|
||||
class TransitDriverService {
|
||||
static String get _base => '${AppLink.server}/transit';
|
||||
|
||||
static String _errMsg(dynamic res) {
|
||||
if (res == 'no_internet') return 'تحقق من اتصالك بالإنترنت';
|
||||
if (res == 'token_expired') return 'انتهت الجلسة، حاول مجدداً';
|
||||
if (res is Map && res['message'] is String) return res['message'];
|
||||
return 'حدث خطأ، حاول مجدداً';
|
||||
}
|
||||
|
||||
/// هل هذا الحساب سائق باص، وما معرّفه في مواصلاتي؟
|
||||
static Future<TransitApiResult<Map<String, dynamic>>> checkIsBusDriver() async {
|
||||
final res = await CRUD().post(link: '$_base/driver/me.php');
|
||||
if (res is Map && res['status'] == 'success' && res['message'] is Map) {
|
||||
return TransitApiResult(true, Map<String, dynamic>.from(res['message']), 'ok');
|
||||
}
|
||||
return TransitApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
/// رحلات اليوم (تُنشأ تلقائياً من الجداول عند أول استدعاء)
|
||||
static Future<TransitApiResult<List<TransitDriverTrip>>> getTodayTrips(
|
||||
int driverTransitId) async {
|
||||
final res = await CRUD().post(
|
||||
link: '$_base/trip/today.php',
|
||||
payload: {'driver_transit_id': driverTransitId.toString()},
|
||||
);
|
||||
if (res is Map && res['status'] == 'success' && res['message'] is Map) {
|
||||
final msg = res['message'] as Map;
|
||||
final list = (msg['trips'] is List)
|
||||
? (msg['trips'] as List)
|
||||
.map((t) => TransitDriverTrip.fromJson(Map<String, dynamic>.from(t)))
|
||||
.toList()
|
||||
: <TransitDriverTrip>[];
|
||||
return TransitApiResult(true, list, 'ok');
|
||||
}
|
||||
return TransitApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
static Future<TransitApiResult<Map<String, dynamic>>> startTrip({
|
||||
required int tripId,
|
||||
required int driverTransitId,
|
||||
required double lat,
|
||||
required double lng,
|
||||
}) async {
|
||||
final res = await CRUD().post(
|
||||
link: '$_base/trip/start.php',
|
||||
payload: {
|
||||
'trip_id': tripId.toString(),
|
||||
'driver_transit_id': driverTransitId.toString(),
|
||||
'lat': lat.toString(),
|
||||
'lng': lng.toString(),
|
||||
},
|
||||
);
|
||||
if (res is Map && res['status'] == 'success') {
|
||||
return TransitApiResult(true, Map<String, dynamic>.from(res['message'] ?? {}), 'ok');
|
||||
}
|
||||
return TransitApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
static Future<TransitApiResult<Map<String, dynamic>>> endTrip({
|
||||
required int tripId,
|
||||
required int driverTransitId,
|
||||
}) async {
|
||||
final res = await CRUD().post(
|
||||
link: '$_base/trip/end.php',
|
||||
payload: {
|
||||
'trip_id': tripId.toString(),
|
||||
'driver_transit_id': driverTransitId.toString(),
|
||||
},
|
||||
);
|
||||
if (res is Map && res['status'] == 'success') {
|
||||
return TransitApiResult(true, Map<String, dynamic>.from(res['message'] ?? {}), 'ok');
|
||||
}
|
||||
return TransitApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
static Future<TransitApiResult<void>> reportDelay({
|
||||
required int tripId,
|
||||
required int driverTransitId,
|
||||
required int delayMinutes,
|
||||
String? reason,
|
||||
}) async {
|
||||
final res = await CRUD().post(
|
||||
link: '$_base/trip/delay.php',
|
||||
payload: {
|
||||
'trip_id': tripId.toString(),
|
||||
'driver_transit_id': driverTransitId.toString(),
|
||||
'delay_minutes': delayMinutes.toString(),
|
||||
if (reason != null) 'reason': reason,
|
||||
},
|
||||
);
|
||||
if (res is Map && res['status'] == 'success') {
|
||||
return TransitApiResult(true, null, 'ok');
|
||||
}
|
||||
return TransitApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user