refactor: Remove PantryProduct class and simplify category resolution in PantryScreen
Test Suite / test (24.15.0) (push) Has been cancelled
Test Suite / test (24.15.0) (push) Has been cancelled
This commit is contained in:
@@ -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(' > ');
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
class PantryProduct {
|
||||
final int id;
|
||||
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 {
|
||||
if (canonicalName != null && canonicalName!.trim().isNotEmpty) {
|
||||
return canonicalName!;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
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