176 lines
5.6 KiB
Dart
176 lines
5.6 KiB
Dart
// transit_controller.dart — تحكم شاشات مواصلاتي (جهة الراكب)
|
|
import 'package:firebase_messaging/firebase_messaging.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();
|
|
// اشترك في topic المؤسسة لاستقبال إشعارات الانطلاق والتأخير
|
|
try {
|
|
await FirebaseMessaging.instance.subscribeToTopic('transit_org_$orgId');
|
|
Log.print('Subscribed to transit_org_$orgId ✅');
|
|
} catch (_) {}
|
|
return true;
|
|
}
|
|
Get.snackbar('مواصلاتي', res.message);
|
|
return false;
|
|
}
|
|
|
|
// اشتراك/إلغاء اشتراك في topic الخط المحدد
|
|
Future<void> subscribeToRouteTopic(int routeId) async {
|
|
try {
|
|
await FirebaseMessaging.instance.subscribeToTopic('transit_route_$routeId');
|
|
Log.print('Subscribed to transit_route_$routeId ✅');
|
|
} catch (_) {}
|
|
}
|
|
|
|
Future<void> unsubscribeFromRouteTopic(int routeId) async {
|
|
try {
|
|
await FirebaseMessaging.instance.unsubscribeFromTopic('transit_route_$routeId');
|
|
} catch (_) {}
|
|
}
|
|
|
|
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.onTransitError = _handleTransitError;
|
|
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 _handleTransitError(String errorCode) {
|
|
if (errorCode == 'NOT_ENROLLED') {
|
|
liveError = 'غير مشترك في هذا الخط. فعّل عضويتك أولاً.';
|
|
} else if (errorCode == 'ROUTE_NOT_FOUND') {
|
|
liveError = 'الخط غير متاح حالياً.';
|
|
} else {
|
|
liveError = 'خطأ في الاتصال بالخط: $errorCode';
|
|
}
|
|
update();
|
|
}
|
|
|
|
void closeLiveRoute() {
|
|
if (liveRouteId != null && Get.isRegistered<MapSocketController>()) {
|
|
final ms = Get.find<MapSocketController>();
|
|
ms.unsubscribeFromTransitRoute(liveRouteId!);
|
|
ms.onBusLocationUpdate = null;
|
|
ms.onTransitError = null;
|
|
}
|
|
liveRouteId = null;
|
|
liveTripId = null;
|
|
liveTripData = null;
|
|
liveBusPosition = null;
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
closeLiveRoute();
|
|
super.onClose();
|
|
}
|
|
}
|