81 lines
2.2 KiB
Dart
81 lines
2.2 KiB
Dart
class KaufFilamentModel {
|
|
final String filamentId;
|
|
final DateTime kaufdatum;
|
|
final String haendler;
|
|
final String marke;
|
|
final String material;
|
|
final double preis;
|
|
final String farbe;
|
|
final String gewicht;
|
|
|
|
const KaufFilamentModel({
|
|
required this.filamentId,
|
|
required this.kaufdatum,
|
|
required this.haendler,
|
|
required this.marke,
|
|
required this.material,
|
|
required this.preis,
|
|
required this.farbe,
|
|
required this.gewicht,
|
|
});
|
|
|
|
/// Supports old purchases saved as "1kg" as well as gram values.
|
|
double? get gewichtGramm {
|
|
final match = RegExp(
|
|
r'^\s*(\d+(?:[.,]\d+)?)\s*(kg|g)?\s*$',
|
|
caseSensitive: false,
|
|
).firstMatch(gewicht);
|
|
if (match == null) return null;
|
|
final value = double.tryParse(match.group(1)!.replaceAll(',', '.'));
|
|
if (value == null || !value.isFinite) return null;
|
|
final grams = value * (match.group(2)?.toLowerCase() == 'kg' ? 1000 : 1);
|
|
return grams.isFinite ? grams : null;
|
|
}
|
|
|
|
Map<String, dynamic> toJson() => {
|
|
'filamentId': filamentId,
|
|
'kaufdatum': kaufdatum.toIso8601String(),
|
|
'haendler': haendler,
|
|
'marke': marke,
|
|
'material': material,
|
|
'preis': preis,
|
|
'farbe': farbe,
|
|
'gewicht': gewicht,
|
|
};
|
|
|
|
factory KaufFilamentModel.fromJson(Map<String, dynamic> json) {
|
|
return KaufFilamentModel(
|
|
filamentId: json['filamentId'] as String,
|
|
kaufdatum: DateTime.parse(json['kaufdatum'] as String),
|
|
haendler: json['haendler'] as String,
|
|
marke: json['marke'] ?? '',
|
|
material: json['material'] as String,
|
|
preis: (json['preis'] as num).toDouble(),
|
|
farbe: json['farbe'] as String,
|
|
gewicht: json['gewicht'] ?? '1000',
|
|
);
|
|
}
|
|
|
|
KaufFilamentModel copyWith({
|
|
String? filamentId,
|
|
DateTime? kaufdatum,
|
|
String? haendler,
|
|
String? marke,
|
|
String? material,
|
|
double? preis,
|
|
String? farbe,
|
|
String? gewicht,
|
|
}) {
|
|
return KaufFilamentModel(
|
|
filamentId: filamentId ?? this.filamentId,
|
|
kaufdatum: kaufdatum ?? this.kaufdatum,
|
|
haendler: haendler ?? this.haendler,
|
|
marke: marke ?? this.marke,
|
|
material: material ?? this.material,
|
|
preis: preis ?? this.preis,
|
|
farbe: farbe ?? this.farbe,
|
|
gewicht: gewicht ?? this.gewicht,
|
|
);
|
|
}
|
|
}
|