refactor: Remove PantryProduct class and simplify category resolution in PantryScreen
Test Suite / test (24.15.0) (push) Has been cancelled

This commit is contained in:
Nils-Johan Gynther
2026-05-11 20:01:00 +02:00
parent a635f1002a
commit 68476142c1
5 changed files with 43 additions and 101 deletions
@@ -5,6 +5,7 @@ class PantryItem {
final String? canonicalName;
final String? category;
final int? categoryId;
final String? categoryPath;
final String? location;
const PantryItem({
@@ -14,6 +15,7 @@ class PantryItem {
this.canonicalName,
this.category,
this.categoryId,
this.categoryPath,
this.location,
});
@@ -24,6 +26,17 @@ class PantryItem {
return productName;
}
String? get l1CategoryOrNull {
final path = categoryPath?.trim();
if (path != null && path.isNotEmpty) {
return path.split('>').first.trim();
}
if (category != null && category!.trim().isNotEmpty) {
return category!.trim();
}
return null;
}
factory PantryItem.fromJson(Map<String, dynamic> json) {
final product = json['product'] as Map<String, dynamic>? ?? {};
return PantryItem(
@@ -33,7 +46,25 @@ class PantryItem {
canonicalName: product['canonicalName']?.toString(),
category: product['category']?.toString(),
categoryId: (product['categoryId'] as num?)?.toInt(),
categoryPath: _buildCategoryPath(product['categoryRef']),
location: json['location']?.toString(),
);
}
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.add(name);
}
current = current['parent'];
}
if (names.isEmpty) return null;
return names.reversed.join(' > ');
}
}