Update: 2026-07-12 05:40:28
This commit is contained in:
@@ -21,6 +21,10 @@ class MapSocketController extends GetxController {
|
||||
int _socketLocationUpdatesCount = 0;
|
||||
Timer? _watchdogTimer;
|
||||
|
||||
// 🚌 مواصلاتي — الخط الحالي المشترَك فيه + مستمع تحديث موقع الباص
|
||||
int? _subscribedTransitRouteId;
|
||||
void Function(Map<String, dynamic> data)? onBusLocationUpdate;
|
||||
|
||||
DateTime? get lastDriverLocationTime => _lastSocketLocationTime;
|
||||
int get socketLocationUpdatesCount => _socketLocationUpdatesCount;
|
||||
|
||||
@@ -61,6 +65,10 @@ class MapSocketController extends GetxController {
|
||||
});
|
||||
Log.print("📡 Re-subscribed to driver location after connect");
|
||||
}
|
||||
if (_subscribedTransitRouteId != null) {
|
||||
socket.emit('subscribe_transit_route', {'route_id': _subscribedTransitRouteId});
|
||||
Log.print("🚌 Re-subscribed to transit route after connect");
|
||||
}
|
||||
update();
|
||||
});
|
||||
|
||||
@@ -89,6 +97,10 @@ class MapSocketController extends GetxController {
|
||||
});
|
||||
Log.print("📡 Re-subscribed to driver location after reconnect");
|
||||
}
|
||||
if (_subscribedTransitRouteId != null) {
|
||||
socket.emit('subscribe_transit_route', {'route_id': _subscribedTransitRouteId});
|
||||
Log.print("🚌 Re-subscribed to transit route after reconnect");
|
||||
}
|
||||
|
||||
if (rideLifecycle.isActiveRideState()) {
|
||||
Log.print("✅ Socket back online — stopping Fast Polling Fallback");
|
||||
@@ -131,6 +143,39 @@ class MapSocketController extends GetxController {
|
||||
socket.on('driver_location_update', (data) {
|
||||
handleDriverLocationUpdate(data);
|
||||
});
|
||||
|
||||
// 🚌 مواصلاتي — بثّ موقع الباص الحي لخط مشترَك فيه
|
||||
socket.on('bus_location_update', (data) {
|
||||
if (data == null) return;
|
||||
try {
|
||||
final map = Map<String, dynamic>.from(data as Map);
|
||||
onBusLocationUpdate?.call(map);
|
||||
} catch (e) {
|
||||
Log.print('Error parsing bus_location_update: $e');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ── مواصلاتي: اشتراك/إلغاء اشتراك ببثّ موقع خط ─────────────
|
||||
// يُستدعى عند فتح/إغلاق شاشة تتبع الباص الحي. يضمن السوكيت متصلاً أولاً.
|
||||
void subscribeToTransitRoute(int routeId) {
|
||||
_subscribedTransitRouteId = routeId;
|
||||
if (!isSocketConnected) {
|
||||
initConnectionWithSocket();
|
||||
// سيُعاد الاشتراك تلقائياً من onConnect إن أضفنا ذلك، لكن نحاول فوراً أيضاً
|
||||
}
|
||||
if (socket.connected) {
|
||||
socket.emit('subscribe_transit_route', {'route_id': routeId});
|
||||
Log.print('🚌 Subscribed to transit route #$routeId');
|
||||
}
|
||||
}
|
||||
|
||||
void unsubscribeFromTransitRoute(int routeId) {
|
||||
if (_subscribedTransitRouteId == routeId) _subscribedTransitRouteId = null;
|
||||
if (isSocketConnected && socket.connected) {
|
||||
socket.emit('unsubscribe_transit_route', {'route_id': routeId});
|
||||
Log.print('🚌 Unsubscribed from transit route #$routeId');
|
||||
}
|
||||
}
|
||||
|
||||
void _startHeartbeat() {
|
||||
|
||||
@@ -1,4 +1,26 @@
|
||||
final Map<String, String> ar_eg = {
|
||||
// ── مواصلاتي (Mawasalati) ──
|
||||
"Mawasalati": "مواصلاتي",
|
||||
"My Memberships": "عضوياتي",
|
||||
"Not enrolled in any institution yet": "لست مشتركاً في أي مؤسسة بعد",
|
||||
"Activate your membership in your university or institution to track your bus live":
|
||||
"فعّل عضويتك في جامعتك أو مؤسستك لمتابعة الباص الخاص بك حياً",
|
||||
"Join a new institution": "انضمام لمؤسسة جديدة",
|
||||
"University, school, or hotel": "جامعة، مدرسة، أو فندق",
|
||||
"Active": "نشطة",
|
||||
"Pending Review": "قيد المراجعة",
|
||||
"Suspended": "موقوفة",
|
||||
"Choose your institution": "اختر مؤسستك",
|
||||
"Search for a university or institution...": "ابحث عن جامعة أو مؤسسة...",
|
||||
"No results": "لا توجد نتائج",
|
||||
"Enter your student/employee ID to activate membership":
|
||||
"أدخل رقمك الجامعي/الوظيفي لتفعيل العضوية",
|
||||
"Student ID": "الرقم الجامعي",
|
||||
"Activate": "تفعيل",
|
||||
"No active routes currently": "لا توجد خطوط نشطة حالياً",
|
||||
"Stops": "محطات",
|
||||
"No bus running on this route right now": "لا يوجد باص يعمل على هذا الخط الآن",
|
||||
"Bus is delayed": "الباص متأخر",
|
||||
" ')[0]).toString()}.\\n\${' I am using": " ')[0]).toString()}.\\n\${' I am using",
|
||||
" I am currently located at ": " أنا حالياً في ",
|
||||
" I am using": " أنا استخدم",
|
||||
|
||||
@@ -1,4 +1,26 @@
|
||||
final Map<String, String> ar_jo = {
|
||||
// ── مواصلاتي (Mawasalati) ──
|
||||
"Mawasalati": "مواصلاتي",
|
||||
"My Memberships": "عضوياتي",
|
||||
"Not enrolled in any institution yet": "لست مشتركاً في أي مؤسسة بعد",
|
||||
"Activate your membership in your university or institution to track your bus live":
|
||||
"فعّل عضويتك في جامعتك أو مؤسستك لمتابعة الباص الخاص بك حياً",
|
||||
"Join a new institution": "انضمام لمؤسسة جديدة",
|
||||
"University, school, or hotel": "جامعة، مدرسة، أو فندق",
|
||||
"Active": "نشطة",
|
||||
"Pending Review": "قيد المراجعة",
|
||||
"Suspended": "موقوفة",
|
||||
"Choose your institution": "اختر مؤسستك",
|
||||
"Search for a university or institution...": "ابحث عن جامعة أو مؤسسة...",
|
||||
"No results": "لا توجد نتائج",
|
||||
"Enter your student/employee ID to activate membership":
|
||||
"أدخل رقمك الجامعي/الوظيفي لتفعيل العضوية",
|
||||
"Student ID": "الرقم الجامعي",
|
||||
"Activate": "تفعيل",
|
||||
"No active routes currently": "لا توجد خطوط نشطة حالياً",
|
||||
"Stops": "محطات",
|
||||
"No bus running on this route right now": "لا يوجد باص يعمل على هذا الخط الآن",
|
||||
"Bus is delayed": "الباص متأخر",
|
||||
" ')[0]).toString()}.\\n\${' I am using": " ')[0]).toString()}.\\n\${' I am using",
|
||||
" I am currently located at ": " أنا حالياً في ",
|
||||
" I am using": " أنا استخدم",
|
||||
|
||||
@@ -1,4 +1,26 @@
|
||||
final Map<String, String> ar_sy = {
|
||||
// ── مواصلاتي (Mawasalati) ──
|
||||
"Mawasalati": "مواصلاتي",
|
||||
"My Memberships": "عضوياتي",
|
||||
"Not enrolled in any institution yet": "لست مشتركاً في أي مؤسسة بعد",
|
||||
"Activate your membership in your university or institution to track your bus live":
|
||||
"فعّل عضويتك في جامعتك أو مؤسستك لمتابعة الباص الخاص بك حياً",
|
||||
"Join a new institution": "انضمام لمؤسسة جديدة",
|
||||
"University, school, or hotel": "جامعة، مدرسة، أو فندق",
|
||||
"Active": "نشطة",
|
||||
"Pending Review": "قيد المراجعة",
|
||||
"Suspended": "موقوفة",
|
||||
"Choose your institution": "اختر مؤسستك",
|
||||
"Search for a university or institution...": "ابحث عن جامعة أو مؤسسة...",
|
||||
"No results": "لا توجد نتائج",
|
||||
"Enter your student/employee ID to activate membership":
|
||||
"أدخل رقمك الجامعي/الوظيفي لتفعيل العضوية",
|
||||
"Student ID": "الرقم الجامعي",
|
||||
"Activate": "تفعيل",
|
||||
"No active routes currently": "لا توجد خطوط نشطة حالياً",
|
||||
"Stops": "محطات",
|
||||
"No bus running on this route right now": "لا يوجد باص يعمل على هذا الخط الآن",
|
||||
"Bus is delayed": "الباص متأخر",
|
||||
" ')[0]).toString()}.\\n\${' I am using": " ')[0]).toString()}.\\n\${' I am using",
|
||||
" I am currently located at ": " أنا حالياً في ",
|
||||
" I am using": " أنا استخدم",
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
// transit_controller.dart — تحكم شاشات مواصلاتي (جهة الراكب)
|
||||
import 'package:get/get.dart';
|
||||
import '../home/map/map_socket_controller.dart';
|
||||
import '../../print.dart';
|
||||
import 'transit_models.dart';
|
||||
import 'transit_service.dart';
|
||||
|
||||
class TransitController extends GetxController {
|
||||
bool isLoadingEnrollments = false;
|
||||
List<TransitEnrollment> myEnrollments = [];
|
||||
|
||||
bool isLoadingOrgs = false;
|
||||
List<TransitOrg> browsableOrgs = [];
|
||||
|
||||
bool isLoadingRoutes = false;
|
||||
List<TransitRouteSummary> currentOrgRoutes = [];
|
||||
int? currentOrgId;
|
||||
|
||||
// ── تتبع الباص الحي ──────────────────────────────────────────
|
||||
int? liveRouteId;
|
||||
int? liveTripId;
|
||||
Map<String, dynamic>? liveTripData;
|
||||
BusPosition? liveBusPosition;
|
||||
String liveError = '';
|
||||
bool isLoadingLiveTrip = false;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
fetchMyEnrollments();
|
||||
}
|
||||
|
||||
Future<void> fetchMyEnrollments() async {
|
||||
isLoadingEnrollments = true;
|
||||
update();
|
||||
final res = await TransitService.getMyEnrollments();
|
||||
if (res.success) myEnrollments = res.data ?? [];
|
||||
isLoadingEnrollments = false;
|
||||
update();
|
||||
}
|
||||
|
||||
Future<void> browseOrgs({String? search}) async {
|
||||
isLoadingOrgs = true;
|
||||
update();
|
||||
final res = await TransitService.browseOrgs(search: search);
|
||||
if (res.success) browsableOrgs = res.data ?? [];
|
||||
isLoadingOrgs = false;
|
||||
update();
|
||||
}
|
||||
|
||||
Future<bool> activateEnrollment(int orgId, String studentId) async {
|
||||
final res = await TransitService.activateEnrollment(
|
||||
orgId: orgId,
|
||||
studentId: studentId,
|
||||
);
|
||||
if (res.success) {
|
||||
await fetchMyEnrollments();
|
||||
return true;
|
||||
}
|
||||
Get.snackbar('مواصلاتي', res.message);
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<void> openOrgRoutes(int orgId) async {
|
||||
currentOrgId = orgId;
|
||||
isLoadingRoutes = true;
|
||||
currentOrgRoutes = [];
|
||||
update();
|
||||
|
||||
final res = await TransitService.getRoutesForOrg(orgId);
|
||||
if (res.success) {
|
||||
currentOrgRoutes = res.data ?? [];
|
||||
} else {
|
||||
Get.snackbar('مواصلاتي', res.message);
|
||||
}
|
||||
isLoadingRoutes = false;
|
||||
update();
|
||||
}
|
||||
|
||||
// ── الخط الحي: تحميل أولي + اشتراك سوكيت ────────────────────
|
||||
Future<void> openLiveRoute(int routeId) async {
|
||||
liveRouteId = routeId;
|
||||
liveTripData = null;
|
||||
liveBusPosition = null;
|
||||
liveError = '';
|
||||
isLoadingLiveTrip = true;
|
||||
update();
|
||||
|
||||
final res = await TransitService.getLiveTrip(routeId: routeId);
|
||||
if (res.success && res.data != null) {
|
||||
liveTripData = res.data;
|
||||
liveTripId = int.tryParse(liveTripData?['trip']?['id']?.toString() ?? '');
|
||||
final pos = liveTripData?['bus_position'];
|
||||
if (pos is Map) liveBusPosition = BusPosition.fromJson(pos);
|
||||
} else {
|
||||
liveError = res.message;
|
||||
}
|
||||
isLoadingLiveTrip = false;
|
||||
update();
|
||||
|
||||
if (Get.isRegistered<MapSocketController>()) {
|
||||
final ms = Get.find<MapSocketController>();
|
||||
ms.onBusLocationUpdate = _handleLiveBusUpdate;
|
||||
ms.subscribeToTransitRoute(routeId);
|
||||
}
|
||||
}
|
||||
|
||||
void _handleLiveBusUpdate(Map<String, dynamic> data) {
|
||||
final incomingRouteId = int.tryParse(data['route_id']?.toString() ?? '');
|
||||
if (incomingRouteId == null || incomingRouteId != liveRouteId) return;
|
||||
|
||||
liveBusPosition = BusPosition(
|
||||
lat: double.tryParse(data['latitude']?.toString() ?? '0') ?? 0,
|
||||
lng: double.tryParse(data['longitude']?.toString() ?? '0') ?? 0,
|
||||
heading: double.tryParse(data['heading']?.toString() ?? '0') ?? 0,
|
||||
speed: double.tryParse(data['speed']?.toString() ?? '0') ?? 0,
|
||||
currentStopSeq: data['current_stop_seq'] == null
|
||||
? null
|
||||
: int.tryParse(data['current_stop_seq'].toString()),
|
||||
);
|
||||
Log.print('🚌 Live bus update: ${liveBusPosition?.lat}, ${liveBusPosition?.lng}');
|
||||
update();
|
||||
}
|
||||
|
||||
void closeLiveRoute() {
|
||||
if (liveRouteId != null && Get.isRegistered<MapSocketController>()) {
|
||||
final ms = Get.find<MapSocketController>();
|
||||
ms.unsubscribeFromTransitRoute(liveRouteId!);
|
||||
ms.onBusLocationUpdate = null;
|
||||
}
|
||||
liveRouteId = null;
|
||||
liveTripId = null;
|
||||
liveTripData = null;
|
||||
liveBusPosition = null;
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
closeLiveRoute();
|
||||
super.onClose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
// transit_models.dart — نماذج بيانات مواصلاتي (جهة الراكب)
|
||||
|
||||
class TransitOrg {
|
||||
final int id;
|
||||
final String type;
|
||||
final String nameAr;
|
||||
final String nameEn;
|
||||
final String? logoUrl;
|
||||
final String city;
|
||||
|
||||
TransitOrg({
|
||||
required this.id,
|
||||
required this.type,
|
||||
required this.nameAr,
|
||||
required this.nameEn,
|
||||
required this.city,
|
||||
this.logoUrl,
|
||||
});
|
||||
|
||||
factory TransitOrg.fromJson(Map<String, dynamic> j) => TransitOrg(
|
||||
id: int.tryParse(j['id'].toString()) ?? 0,
|
||||
type: j['type']?.toString() ?? '',
|
||||
nameAr: j['name_ar']?.toString() ?? '',
|
||||
nameEn: j['name_en']?.toString() ?? '',
|
||||
city: j['city']?.toString() ?? '',
|
||||
logoUrl: j['logo_url']?.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
class TransitEnrollment {
|
||||
final int id;
|
||||
final int orgId;
|
||||
final String orgName;
|
||||
final String status; // pending | active | suspended | expired
|
||||
final String verifyMethod;
|
||||
|
||||
TransitEnrollment({
|
||||
required this.id,
|
||||
required this.orgId,
|
||||
required this.orgName,
|
||||
required this.status,
|
||||
required this.verifyMethod,
|
||||
});
|
||||
|
||||
factory TransitEnrollment.fromActivateResponse(
|
||||
Map<String, dynamic> j, int orgId) =>
|
||||
TransitEnrollment(
|
||||
id: int.tryParse(j['enrollment_id'].toString()) ?? 0,
|
||||
orgId: orgId,
|
||||
orgName: j['org_name']?.toString() ?? '',
|
||||
status: j['status']?.toString() ?? 'pending',
|
||||
verifyMethod: j['verify_method']?.toString() ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
class TransitStop {
|
||||
final int id;
|
||||
final int sequence;
|
||||
final String nameAr;
|
||||
final double lat;
|
||||
final double lng;
|
||||
final int etaOffsetMin;
|
||||
final bool isMajor;
|
||||
|
||||
TransitStop({
|
||||
required this.id,
|
||||
required this.sequence,
|
||||
required this.nameAr,
|
||||
required this.lat,
|
||||
required this.lng,
|
||||
required this.etaOffsetMin,
|
||||
required this.isMajor,
|
||||
});
|
||||
|
||||
factory TransitStop.fromJson(Map<String, dynamic> j) => TransitStop(
|
||||
id: int.tryParse(j['id'].toString()) ?? 0,
|
||||
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: int.tryParse(j['eta_offset_min']?.toString() ?? '0') ?? 0,
|
||||
isMajor: (j['is_major']?.toString() ?? '0') == '1',
|
||||
);
|
||||
}
|
||||
|
||||
class TransitRouteSummary {
|
||||
final int id;
|
||||
final String nameAr;
|
||||
final String status;
|
||||
final int stopCount;
|
||||
|
||||
TransitRouteSummary({
|
||||
required this.id,
|
||||
required this.nameAr,
|
||||
required this.status,
|
||||
required this.stopCount,
|
||||
});
|
||||
|
||||
factory TransitRouteSummary.fromJson(Map<String, dynamic> j) =>
|
||||
TransitRouteSummary(
|
||||
id: int.tryParse(j['id'].toString()) ?? 0,
|
||||
nameAr: j['name_ar']?.toString() ?? '',
|
||||
status: j['status']?.toString() ?? '',
|
||||
stopCount: int.tryParse(j['stop_count']?.toString() ?? '0') ?? 0,
|
||||
);
|
||||
}
|
||||
|
||||
class TransitLiveTrip {
|
||||
final int id;
|
||||
final String status;
|
||||
final int delayMinutes;
|
||||
final int? currentStopSeq;
|
||||
final String routeName;
|
||||
final String? departureTime;
|
||||
final String? driverName;
|
||||
|
||||
TransitLiveTrip({
|
||||
required this.id,
|
||||
required this.status,
|
||||
required this.delayMinutes,
|
||||
required this.routeName,
|
||||
this.currentStopSeq,
|
||||
this.departureTime,
|
||||
this.driverName,
|
||||
});
|
||||
|
||||
factory TransitLiveTrip.fromJson(Map<String, dynamic> j) => TransitLiveTrip(
|
||||
id: int.tryParse(j['id'].toString()) ?? 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(),
|
||||
driverName: j['driver_name']?.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
class BusPosition {
|
||||
final double lat;
|
||||
final double lng;
|
||||
final double heading;
|
||||
final double speed;
|
||||
final int? currentStopSeq;
|
||||
|
||||
BusPosition({
|
||||
required this.lat,
|
||||
required this.lng,
|
||||
this.heading = 0,
|
||||
this.speed = 0,
|
||||
this.currentStopSeq,
|
||||
});
|
||||
|
||||
factory BusPosition.fromJson(Map<dynamic, dynamic> j) => BusPosition(
|
||||
lat: double.tryParse(j['lat']?.toString() ?? '0') ?? 0,
|
||||
lng: double.tryParse(j['lng']?.toString() ?? '0') ?? 0,
|
||||
heading: double.tryParse(j['heading']?.toString() ?? '0') ?? 0,
|
||||
speed: double.tryParse(j['speed']?.toString() ?? '0') ?? 0,
|
||||
currentStopSeq: j['current_stop_seq'] == null
|
||||
? null
|
||||
: int.tryParse(j['current_stop_seq'].toString()),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
// transit_service.dart — طبقة الاتصال بـ backend/transit (جهة الراكب)
|
||||
import '../functions/crud.dart';
|
||||
import '../../constant/links.dart';
|
||||
import 'transit_models.dart';
|
||||
|
||||
class TransitApiResult<T> {
|
||||
final bool success;
|
||||
final T? data;
|
||||
final String message;
|
||||
TransitApiResult(this.success, this.data, this.message);
|
||||
}
|
||||
|
||||
class TransitService {
|
||||
static String get _base => '${AppLink.server}/transit';
|
||||
|
||||
/// تصفح المؤسسات المتاحة للتفعيل (قبل الانضمام)
|
||||
static Future<TransitApiResult<List<TransitOrg>>> browseOrgs({
|
||||
String? country,
|
||||
String? search,
|
||||
}) async {
|
||||
final payload = <String, dynamic>{};
|
||||
if (country != null) payload['country'] = country;
|
||||
if (search != null && search.isNotEmpty) payload['search'] = search;
|
||||
|
||||
final res = await CRUD().post(link: '$_base/org/browse.php', payload: payload);
|
||||
|
||||
if (res is Map && res['status'] == 'success') {
|
||||
final msg = res['message'];
|
||||
final list = (msg is Map && msg['orgs'] is List)
|
||||
? (msg['orgs'] as List)
|
||||
.map((o) => TransitOrg.fromJson(Map<String, dynamic>.from(o)))
|
||||
.toList()
|
||||
: <TransitOrg>[];
|
||||
return TransitApiResult(true, list, 'ok');
|
||||
}
|
||||
return TransitApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
/// تفعيل عضوية الراكب في مؤسسة بالرقم الجامعي/الوظيفي
|
||||
static Future<TransitApiResult<TransitEnrollment>> activateEnrollment({
|
||||
required int orgId,
|
||||
required String studentId,
|
||||
}) async {
|
||||
final res = await CRUD().post(
|
||||
link: '$_base/enrollment/activate.php',
|
||||
payload: {'org_id': orgId.toString(), 'student_id': studentId},
|
||||
);
|
||||
|
||||
if (res is Map && res['status'] == 'success') {
|
||||
final msg = res['message'];
|
||||
if (msg is Map<String, dynamic>) {
|
||||
return TransitApiResult(
|
||||
true,
|
||||
TransitEnrollment.fromActivateResponse(msg, orgId),
|
||||
msg['message']?.toString() ?? 'تم',
|
||||
);
|
||||
}
|
||||
}
|
||||
return TransitApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
/// عضوياتي في كل المؤسسات
|
||||
static Future<TransitApiResult<List<TransitEnrollment>>>
|
||||
getMyEnrollments() async {
|
||||
final res = await CRUD().post(link: '$_base/enrollment/my_enrollments.php');
|
||||
|
||||
if (res is Map && res['status'] == 'success') {
|
||||
final msg = res['message'];
|
||||
final list = (msg is Map && msg['enrollments'] is List)
|
||||
? (msg['enrollments'] as List)
|
||||
.map((e) => TransitEnrollment(
|
||||
id: int.tryParse(e['id'].toString()) ?? 0,
|
||||
orgId: int.tryParse(e['org_id'].toString()) ?? 0,
|
||||
orgName: e['org_name']?.toString() ?? '',
|
||||
status: e['status']?.toString() ?? '',
|
||||
verifyMethod: e['verify_method']?.toString() ?? '',
|
||||
))
|
||||
.toList()
|
||||
: <TransitEnrollment>[];
|
||||
return TransitApiResult(true, list, 'ok');
|
||||
}
|
||||
return TransitApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
/// خطوط مؤسسة معيّنة (يشترط عضوية نشطة)
|
||||
static Future<TransitApiResult<List<TransitRouteSummary>>> getRoutesForOrg(
|
||||
int orgId) async {
|
||||
final res = await CRUD().post(
|
||||
link: '$_base/route/for_org.php',
|
||||
payload: {'org_id': orgId.toString()},
|
||||
);
|
||||
|
||||
if (res is Map && res['status'] == 'success') {
|
||||
final msg = res['message'];
|
||||
final list = (msg is Map && msg['routes'] is List)
|
||||
? (msg['routes'] as List)
|
||||
.map((r) => TransitRouteSummary.fromJson(
|
||||
Map<String, dynamic>.from(r)))
|
||||
.toList()
|
||||
: <TransitRouteSummary>[];
|
||||
return TransitApiResult(true, list, 'ok');
|
||||
}
|
||||
return TransitApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
/// تفاصيل الرحلة الحية لخط (المحطات + آخر موقع معروف)
|
||||
static Future<TransitApiResult<Map<String, dynamic>>> getLiveTrip({
|
||||
int? tripId,
|
||||
int? routeId,
|
||||
}) async {
|
||||
final payload = <String, dynamic>{};
|
||||
if (tripId != null) payload['trip_id'] = tripId.toString();
|
||||
if (routeId != null) payload['route_id'] = routeId.toString();
|
||||
|
||||
final res = await CRUD().post(link: '$_base/trip/live.php', payload: payload);
|
||||
|
||||
if (res is Map && res['status'] == 'success') {
|
||||
final msg = res['message'];
|
||||
if (msg is Map) return TransitApiResult(true, Map<String, dynamic>.from(msg), 'ok');
|
||||
}
|
||||
return TransitApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
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 'حدث خطأ، حاول مجدداً';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user