feat: enhance inventory and pantry features with filtering, sorting, and error handling improvements
This commit is contained in:
@@ -3,12 +3,16 @@ class PantryProduct {
|
||||
final String name;
|
||||
final String? canonicalName;
|
||||
final String? category;
|
||||
final int? categoryId;
|
||||
final String? categoryPath;
|
||||
|
||||
const PantryProduct({
|
||||
required this.id,
|
||||
required this.name,
|
||||
this.canonicalName,
|
||||
this.category,
|
||||
this.categoryId,
|
||||
this.categoryPath,
|
||||
});
|
||||
|
||||
String get displayName {
|
||||
@@ -19,11 +23,33 @@ class PantryProduct {
|
||||
}
|
||||
|
||||
factory PantryProduct.fromJson(Map<String, dynamic> json) {
|
||||
final categoryRef = json['categoryRef'];
|
||||
final path = _buildCategoryPath(categoryRef);
|
||||
|
||||
return PantryProduct(
|
||||
id: (json['id'] as num).toInt(),
|
||||
name: (json['name'] ?? '').toString(),
|
||||
canonicalName: json['canonicalName']?.toString(),
|
||||
category: json['category']?.toString(),
|
||||
categoryId: (json['categoryId'] as num?)?.toInt(),
|
||||
categoryPath: path,
|
||||
);
|
||||
}
|
||||
|
||||
static String? _buildCategoryPath(dynamic rawCategoryRef) {
|
||||
if (rawCategoryRef is! Map<String, dynamic>) return null;
|
||||
|
||||
final names = <String>[];
|
||||
dynamic current = rawCategoryRef;
|
||||
while (current is Map<String, dynamic>) {
|
||||
final name = current['name']?.toString().trim();
|
||||
if (name != null && name.isNotEmpty) {
|
||||
names.insert(0, name);
|
||||
}
|
||||
current = current['parent'];
|
||||
}
|
||||
|
||||
if (names.isEmpty) return null;
|
||||
return names.join(' > ');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user