Files
receipity/lib/models/product.dart
T

76 lines
2.1 KiB
Dart

/// A product stored after the user confirms a parsed receipt.
library;
class Product {
const Product({
required this.id,
required this.name,
required this.lastPrice,
required this.timesSeen,
required this.updatedAt,
this.favourite = false,
this.imagePath,
this.supermarket,
this.notes,
this.category,
});
final int id;
final String name;
final double lastPrice;
final int timesSeen;
final DateTime updatedAt;
final bool favourite;
final String? imagePath;
final String? supermarket;
final String? notes;
final String? category;
factory Product.fromMap(Map<String, Object?> map) {
return Product(
id: map['id']! as int,
name: map['name']! as String,
lastPrice: (map['display_price'] as num? ?? map['last_price']! as num)
.toDouble(),
timesSeen: map['times_seen']! as int,
updatedAt: DateTime.parse(map['updated_at']! as String),
favourite: (map['favourite'] as int? ?? 0) == 1,
imagePath: map['image_path'] as String?,
supermarket: map['supermarket'] as String?,
notes: map['notes'] as String?,
category: map['category'] as String?,
);
}
Product copyWith({
String? name,
double? lastPrice,
int? timesSeen,
DateTime? updatedAt,
bool? favourite,
String? imagePath,
String? supermarket,
String? notes,
String? category,
bool clearImage = false,
bool clearSupermarket = false,
bool clearNotes = false,
bool clearCategory = false,
}) {
return Product(
id: id,
name: name ?? this.name,
lastPrice: lastPrice ?? this.lastPrice,
timesSeen: timesSeen ?? this.timesSeen,
updatedAt: updatedAt ?? this.updatedAt,
favourite: favourite ?? this.favourite,
imagePath: clearImage ? null : (imagePath ?? this.imagePath),
supermarket: clearSupermarket ? null : (supermarket ?? this.supermarket),
notes: clearNotes ? null : (notes ?? this.notes),
category: clearCategory ? null : (category ?? this.category),
);
}
}
enum ProductSort { name, preferred }