ionic-planb-logistic-app-fl.../lib/features/routes/data/models/route_model.dart

119 lines
3.5 KiB
Dart

import 'stop_model.dart';
enum RouteStatus { notStarted, inProgress, completed, cancelled }
class RouteModel {
final String id;
final String driverId;
final String driverName;
final DateTime date;
final RouteStatus status;
final List<StopModel> stops;
final double totalDistance; // in kilometers
final int estimatedDuration; // in minutes
final DateTime? startTime;
final DateTime? endTime;
final String? vehicleId;
final String? notes;
RouteModel({
required this.id,
required this.driverId,
required this.driverName,
required this.date,
required this.status,
required this.stops,
required this.totalDistance,
required this.estimatedDuration,
this.startTime,
this.endTime,
this.vehicleId,
this.notes,
});
factory RouteModel.fromJson(Map<String, dynamic> json) {
return RouteModel(
id: json['id'] as String,
driverId: json['driverId'] as String,
driverName: json['driverName'] as String,
date: DateTime.parse(json['date'] as String),
status: RouteStatus.values.firstWhere(
(e) => e.toString() == 'RouteStatus.${json['status']}',
),
stops: (json['stops'] as List<dynamic>)
.map((stop) => StopModel.fromJson(stop as Map<String, dynamic>))
.toList(),
totalDistance: (json['totalDistance'] as num).toDouble(),
estimatedDuration: json['estimatedDuration'] as int,
startTime: json['startTime'] != null
? DateTime.parse(json['startTime'] as String)
: null,
endTime: json['endTime'] != null
? DateTime.parse(json['endTime'] as String)
: null,
vehicleId: json['vehicleId'] as String?,
notes: json['notes'] as String?,
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'driverId': driverId,
'driverName': driverName,
'date': date.toIso8601String(),
'status': status.toString().split('.').last,
'stops': stops.map((stop) => stop.toJson()).toList(),
'totalDistance': totalDistance,
'estimatedDuration': estimatedDuration,
'startTime': startTime?.toIso8601String(),
'endTime': endTime?.toIso8601String(),
'vehicleId': vehicleId,
'notes': notes,
};
}
RouteModel copyWith({
String? id,
String? driverId,
String? driverName,
DateTime? date,
RouteStatus? status,
List<StopModel>? stops,
double? totalDistance,
int? estimatedDuration,
DateTime? startTime,
DateTime? endTime,
String? vehicleId,
String? notes,
}) {
return RouteModel(
id: id ?? this.id,
driverId: driverId ?? this.driverId,
driverName: driverName ?? this.driverName,
date: date ?? this.date,
status: status ?? this.status,
stops: stops ?? this.stops,
totalDistance: totalDistance ?? this.totalDistance,
estimatedDuration: estimatedDuration ?? this.estimatedDuration,
startTime: startTime ?? this.startTime,
endTime: endTime ?? this.endTime,
vehicleId: vehicleId ?? this.vehicleId,
notes: notes ?? this.notes,
);
}
int get completedStopsCount =>
stops.where((stop) => stop.status == StopStatus.completed).length;
int get totalStopsCount => stops.length;
double get progressPercentage =>
totalStopsCount > 0 ? (completedStopsCount / totalStopsCount) * 100 : 0;
StopModel? get nextStop => stops.firstWhere(
(stop) => stop.status == StopStatus.pending,
orElse: () => stops.first,
);
}