chore: استيراد أولي من سيرو (ecfe7568) — بلا أي تعديل
نسخة كاملة من مستودع سيرو عند ecfe7568 لتكون أساس تطبيق «انطلق». نُسخ المتعقَّب في git فقط (12,509 ملفاً / 302 م.ب) بـ git archive، لا `cp -r` — فاستُثنيت تلقائياً مخلفات البناء (build · node_modules · .dart_tool · .gradle · Pods ≈ 10.7 غ.ب) وكل ما يستثنيه .gitignore. هذا الكوميت **بلا أي تعديل عمداً** حتى يكون كل ما يليه فرقاً مقروءاً مقابل سيرو الأصلي. سيرو نفسه لم يُمسّ. ⚠️ لا يبني بعد: `.env` و`lib/env/env.g.dart` غير متعقَّبين في سيرو (وهذا صحيح — أسرار لكل مستأجر). كل تطبيق فلاتر هنا يحتاج .env خاصاً بانطلق ثم توليد env.g.dart عبر build_runner. لا تُنسخ أسرار سيرو. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user