Fracht mit Maßen und Gewicht anzeigen und Stationsabschlüsse gegen Wiederholung absichern. Backend-Abschluss atomar ausführen und mit Regressionstests abdecken. Optionale Aufgaben beim Kopieren erhalten und Kunden mit Adressen anzeigen. App und Backend auf Version 0.9.23 aktualisieren.
109 lines
3.1 KiB
Dart
109 lines
3.1 KiB
Dart
class CargoItem {
|
||
final String id; // Will store the timestamp as string
|
||
final String jobId; // Will store the timestamp as string
|
||
final String description;
|
||
final int quantity;
|
||
final double weightKg;
|
||
final double lengthCm;
|
||
final double widthCm;
|
||
final double heightCm;
|
||
|
||
CargoItem({
|
||
required this.id,
|
||
required this.jobId,
|
||
required this.description,
|
||
required this.quantity,
|
||
required this.weightKg,
|
||
required this.lengthCm,
|
||
required this.widthCm,
|
||
required this.heightCm,
|
||
});
|
||
|
||
static String _readString(dynamic value, {String fallback = ''}) {
|
||
if (value is String) {
|
||
return value;
|
||
}
|
||
if (value is num || value is bool) {
|
||
return value.toString();
|
||
}
|
||
return fallback;
|
||
}
|
||
|
||
factory CargoItem.fromJson(Map<String, dynamic> json) {
|
||
// Parse the complex id object - can be either a Map or a simple string
|
||
String idValue = '';
|
||
if (json['id'] is Map) {
|
||
final idMap = json['id'] as Map<String, dynamic>;
|
||
idValue = idMap['timestamp']?.toString() ?? '';
|
||
} else {
|
||
idValue = json['id']?.toString() ?? '';
|
||
}
|
||
|
||
// Parse the complex jobId object - can be either a Map or a simple string
|
||
String jobIdValue = '';
|
||
if (json['jobId'] is Map) {
|
||
final jobIdMap = json['jobId'] as Map<String, dynamic>;
|
||
jobIdValue = jobIdMap['timestamp']?.toString() ?? '';
|
||
} else {
|
||
jobIdValue = json['jobId']?.toString() ?? '';
|
||
}
|
||
|
||
return CargoItem(
|
||
id: idValue,
|
||
jobId: jobIdValue,
|
||
description: _readString(json['description']),
|
||
quantity: json['quantity'] is num ? json['quantity'].toInt() : 0,
|
||
weightKg: json['weightKg'] is num ? json['weightKg'].toDouble() : 0.0,
|
||
lengthCm: json['lengthMm'] is num ? json['lengthMm'].toDouble() : 0.0,
|
||
widthCm: json['widthMm'] is num ? json['widthMm'].toDouble() : 0.0,
|
||
heightCm: json['heightMm'] is num ? json['heightMm'].toDouble() : 0.0,
|
||
);
|
||
}
|
||
|
||
Map<String, dynamic> toJson() {
|
||
return {
|
||
'id': id,
|
||
'jobId': jobId,
|
||
'description': description,
|
||
'quantity': quantity,
|
||
'weightKg': weightKg,
|
||
'lengthMm': lengthCm,
|
||
'widthMm': widthCm,
|
||
'heightMm': heightCm,
|
||
};
|
||
}
|
||
|
||
@override
|
||
String toString() {
|
||
return 'CargoItem(id: $id, description: $description, quantity: $quantity)';
|
||
}
|
||
|
||
@override
|
||
bool operator ==(Object other) {
|
||
if (identical(this, other)) return true;
|
||
return other is CargoItem && other.id == id;
|
||
}
|
||
|
||
@override
|
||
int get hashCode => id.hashCode;
|
||
|
||
bool get hasDimensions =>
|
||
[lengthCm, widthCm, heightCm].any((value) => value.isFinite && value > 0);
|
||
|
||
/// Get formatted dimensions string for display
|
||
String get formattedDimensions {
|
||
final dimensions = [lengthCm, widthCm, heightCm].map((value) {
|
||
if (!value.isFinite || value <= 0) return '–';
|
||
return value == value.truncateToDouble()
|
||
? value.toStringAsFixed(0)
|
||
: value.toString();
|
||
});
|
||
return '${dimensions.join(' × ')} cm';
|
||
}
|
||
|
||
/// Get formatted weight string for display
|
||
String get formattedWeight {
|
||
return '${weightKg.toStringAsFixed(1)} kg';
|
||
}
|
||
}
|