feat: implement core architecture, design system, localization, and trip features for both driver and rider apps.

This commit is contained in:
Hamza-Ayed
2026-07-19 16:21:58 +03:00
parent 959e2fa758
commit c4dfeb4c6f
207 changed files with 14017 additions and 5567 deletions
@@ -0,0 +1,91 @@
import 'dart:async';
import 'package:geolocator/geolocator.dart';
/// خدمة موقع السائق — تتبع الموقع في الخلفية وإرساله للسيرفر.
///
/// تتبع 3 ثوانٍ عند التنشيط، ثم إرسال كل دقيقتين (docs/23 §10).
/// تراعي حالة البطارية وتتوقف عند انخفاضها.
class DriverLocationService {
final Future<void> Function(double lat, double lng, {double? heading, double? speed})
_onLocationUpdate;
StreamSubscription<Position>? _positionSub;
Timer? _heartbeatTimer;
bool _active = false;
DriverLocationService(this._onLocationUpdate);
bool get isActive => _active;
/// بدء التتبع.
Future<void> start() async {
if (_active) return;
// تحقق من صلاحية الموقع.
var permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied ||
permission == LocationPermission.deniedForever) {
return;
}
}
_active = true;
// أرسل الموقع فوراً.
_sendCurrentLocation();
// استمع لتحديثات الموقع كل 3 ثوانٍ.
_positionSub = Geolocator.getPositionStream(
locationSettings: const LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 15, // تحديث عند تغيير 15 متر
),
).listen((pos) {
_onLocationUpdate(
pos.latitude,
pos.longitude,
heading: pos.heading,
speed: pos.speed,
);
});
// دفعة كل دقيقتين كحد أقصى.
_heartbeatTimer = Timer.periodic(const Duration(minutes: 2), (_) {
_sendCurrentLocation();
});
}
/// إيقاف التتبع.
void stop() {
_active = false;
_positionSub?.cancel();
_positionSub = null;
_heartbeatTimer?.cancel();
_heartbeatTimer = null;
}
/// إرسال الموقع الحالي مرة واحدة.
Future<void> _sendCurrentLocation() async {
try {
final pos = await Geolocator.getCurrentPosition(
locationSettings: const LocationSettings(
accuracy: LocationAccuracy.high,
),
);
await _onLocationUpdate(
pos.latitude,
pos.longitude,
heading: pos.heading,
speed: pos.speed,
);
} catch (_) {
// خطأ صامت — سيحاول مرة أخرى مع الدقيقة التالية.
}
}
void dispose() {
stop();
}
}
@@ -0,0 +1,50 @@
import '../../../core/api/api_client.dart';
import 'models/driver_profile_model.dart';
/// مستودع بيانات السائق — يُستخدم من DriverCubit.
class DriverRepository {
final ApiClient _api;
DriverRepository(this._api);
/// `GET /drivers/me` — جلب ملف السائق.
Future<DriverProfileModel?> getProfile() async {
try {
final r = await _api.dio.get('/drivers/me');
return DriverProfileModel.fromJson(r.data as Map<String, dynamic>);
} catch (_) {
return null;
}
}
/// `PATCH /drivers/status` — تبديل الحالة (مُتصل/مُنقطع).
Future<bool> setOnline(bool online) async {
try {
final r = await _api.dio.patch('/drivers/status', data: {'online': online});
return r.data['online'] as bool? ?? online;
} catch (_) {
return false;
}
}
/// `POST /drivers/location` — إرسال الموقع الحالي.
///
/// يُعيد `true` إذا كان الموقع "مُهماً" (تغيير كبير حسب السيرفر).
Future<bool> sendLocation({
required double lat,
required double lng,
double? heading,
double? speed,
}) async {
try {
final r = await _api.dio.post('/drivers/location', data: {
'lat': lat,
'lng': lng,
if (heading != null) 'heading': heading,
if (speed != null) 'speed': speed,
});
return r.data['significant'] as bool? ?? false;
} catch (_) {
return false;
}
}
}
@@ -0,0 +1,94 @@
import 'package:equatable/equatable.dart';
/// نموذج بيانات السائق — من `GET /drivers/me`.
class DriverProfileModel extends Equatable {
final String id;
final String phone;
final String? name;
final bool online;
final double? lat;
final double? lng;
final String? vehicleMake;
final String? vehicleModel;
final int? vehicleYear;
final String? vehicleColor;
final String? plateNumber;
final String? serviceClass;
final double? rating;
final int? totalTrips;
final int? creditBalance;
const DriverProfileModel({
required this.id,
required this.phone,
this.name,
this.online = false,
this.lat,
this.lng,
this.vehicleMake,
this.vehicleModel,
this.vehicleYear,
this.vehicleColor,
this.plateNumber,
this.serviceClass,
this.rating,
this.totalTrips,
this.creditBalance,
});
factory DriverProfileModel.fromJson(Map<String, dynamic> j) =>
DriverProfileModel(
id: j['id'] as String,
phone: (j['phone'] ?? '') as String,
name: j['name'] as String?,
online: j['online'] as bool? ?? false,
lat: (j['lat'] as num?)?.toDouble(),
lng: (j['lng'] as num?)?.toDouble(),
vehicleMake: j['vehicleMake'] as String?,
vehicleModel: j['vehicleModel'] as String?,
vehicleYear: j['vehicleYear'] as int?,
vehicleColor: j['vehicleColor'] as String?,
plateNumber: j['plateNumber'] as String?,
serviceClass: j['serviceClass'] as String?,
rating: (j['rating'] as num?)?.toDouble(),
totalTrips: j['totalTrips'] as int?,
creditBalance: j['creditBalance'] as int?,
);
DriverProfileModel copyWith({
String? name,
bool? online,
double? lat,
double? lng,
String? vehicleMake,
String? vehicleModel,
int? vehicleYear,
String? vehicleColor,
String? plateNumber,
String? serviceClass,
double? rating,
int? totalTrips,
int? creditBalance,
}) {
return DriverProfileModel(
id: id,
phone: phone,
name: name ?? this.name,
online: online ?? this.online,
lat: lat ?? this.lat,
lng: lng ?? this.lng,
vehicleMake: vehicleMake ?? this.vehicleMake,
vehicleModel: vehicleModel ?? this.vehicleModel,
vehicleYear: vehicleYear ?? this.vehicleYear,
vehicleColor: vehicleColor ?? this.vehicleColor,
plateNumber: plateNumber ?? this.plateNumber,
serviceClass: serviceClass ?? this.serviceClass,
rating: rating ?? this.rating,
totalTrips: totalTrips ?? this.totalTrips,
creditBalance: creditBalance ?? this.creditBalance,
);
}
@override
List<Object?> get props => [id, phone, name, online, vehicleMake, vehicleModel, plateNumber];
}
@@ -0,0 +1,62 @@
import 'package:equatable/equatable.dart';
/// عرض رحلة وارد — من `trip:offer` عبر WebSocket.
///
/// يحتوي على كل المعلومات التي يحتاجها السائق للقبول/الرفض.
/// المهلة الزمنية (`expiresAt`) تُدار من OffersBloc.
class TripOfferModel extends Equatable {
final String tripId;
final double pickupLat;
final double pickupLng;
final String? pickupAddress;
final double dropoffLat;
final double dropoffLng;
final String? dropoffAddress;
final double distance;
final num fare;
final String currency;
final int etaPickup;
final String vehicleClass;
final DateTime expiresAt;
const TripOfferModel({
required this.tripId,
required this.pickupLat,
required this.pickupLng,
this.pickupAddress,
required this.dropoffLat,
required this.dropoffLng,
this.dropoffAddress,
required this.distance,
required this.fare,
required this.currency,
required this.etaPickup,
required this.vehicleClass,
required this.expiresAt,
});
factory TripOfferModel.fromJson(Map<String, dynamic> j) => TripOfferModel(
tripId: j['tripId'] as String,
pickupLat: (j['pickup']?['lat'] ?? 0).toDouble(),
pickupLng: (j['pickup']?['lng'] ?? 0).toDouble(),
pickupAddress: j['pickup']?['address'] as String?,
dropoffLat: (j['dropoff']?['lat'] ?? 0).toDouble(),
dropoffLng: (j['dropoff']?['lng'] ?? 0).toDouble(),
dropoffAddress: j['dropoff']?['address'] as String?,
distance: (j['distance'] ?? 0).toDouble(),
fare: (j['fare'] ?? 0) as num,
currency: (j['currency'] ?? 'IQD') as String,
etaPickup: (j['etaPickup'] ?? 0) as int,
vehicleClass: (j['vehicleClass'] ?? '') as String,
expiresAt: DateTime.parse(j['expiresAt'] as String),
);
/// المسافة المنسقة (كم مع خانتين عشريتين).
String get formattedDistance => '${distance.toStringAsFixed(1)} km';
/// الأجرة المنسقة.
String get formattedFare => '$fare $currency';
@override
List<Object?> get props => [tripId, pickupLat, pickupLng, fare, expiresAt];
}