Update codebase

This commit is contained in:
Hamza-Ayed
2026-08-05 14:12:41 +03:00
parent 58928cd258
commit 95e2e4f35d
51 changed files with 3141 additions and 390 deletions
@@ -6,6 +6,7 @@ import '../../../core/api/api_exception.dart';
import '../../../core/location/location_service.dart';
import '../../../core/realtime/realtime_service.dart';
import '../data/models/geo_point.dart';
import '../data/maps_repository.dart';
import '../data/models/trip.dart';
import '../data/trip_repository.dart';
import 'duty_state.dart';
@@ -16,14 +17,17 @@ class DutyCubit extends Cubit<DutyState> {
required TripRepository trips,
required LocationService location,
required RealtimeService realtime,
required MapsRepository maps,
}) : _trips = trips,
_location = location,
_realtime = realtime,
_maps = maps,
super(const DutyState());
final TripRepository _trips;
final LocationService _location;
final RealtimeService _realtime;
final MapsRepository _maps;
StreamSubscription<RealtimeEvent>? _events;
Timer? _offerTimer;
@@ -100,6 +104,7 @@ class DutyCubit extends Cubit<DutyState> {
final heading = position.heading as double?;
emit(state.copyWith(myLocation: GeoPoint(lat, lng)));
_refreshNavRoute(GeoPoint(lat, lng));
// مساران عمداً: REST يغذّي المطابقة في Redis، والسوكت يغذّي خريطة
// الراكب الحيّة. سقوط أحدهما لا يُعمي الآخر.
@@ -202,6 +207,53 @@ class DutyCubit extends Cubit<DutyState> {
busy: false,
phase: DutyPhase.active,
));
// الهدف يتبدّل مع الحالة (الراكب ← الوجهة)، فالمسار يُعاد فوراً عند كل
// انتقال بدل انتظار نبضة الموقع التالية.
_lastNavFrom = null;
final me = state.myLocation;
if (me != null) _refreshNavRoute(me);
}
// ── مسار القيادة ───────────────────────────────────────────────────────
/// أقلّ إزاحة تستدعي إعادة الحساب (~50 متراً) — نفس منطق الراكب: يلتقط
/// الانعطاف الفعلي ويتجاهل رجفة GPS.
static const double _redrawThreshold = 0.0005;
GeoPoint? _lastNavFrom;
bool _routing = false;
void _refreshNavRoute(GeoPoint from) {
final target = state.navTarget;
if (target == null) {
if (state.navRoute != null) emit(state.copyWith(clearNavRoute: true));
_lastNavFrom = null;
return;
}
if (_routing) return;
final last = _lastNavFrom;
final moved = last == null ||
(from.lat - last.lat).abs() > _redrawThreshold ||
(from.lng - last.lng).abs() > _redrawThreshold;
if (!moved) return;
_lastNavFrom = from;
unawaited(_route(from, target));
}
Future<void> _route(GeoPoint from, GeoPoint to) async {
_routing = true;
try {
final route = await _maps.route(from, to);
if (!isClosed) emit(state.copyWith(navRoute: route));
} on ApiException {
// مسارٌ متعذّر لا يوقف الرحلة — السائق يرى النقطة ولو بلا خط،
// وتُعاد المحاولة مع الحركة التالية.
} finally {
_routing = false;
}
}
void _onRealtime(RealtimeEvent event) {