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:
@@ -11,7 +11,6 @@ import '../../auth/data/auth_providers.dart';
|
||||
import '../../inventory/data/inventory_providers.dart';
|
||||
import '../data/pantry_providers.dart';
|
||||
import '../domain/pantry_item.dart';
|
||||
import '../domain/pantry_product.dart';
|
||||
|
||||
final _logger = Logger('PantryScreen');
|
||||
|
||||
@@ -214,35 +213,26 @@ class _PantryScreenState extends ConsumerState<PantryScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
String _resolveL1Category(PantryItem item, Map<int, PantryProduct> productById) {
|
||||
final path = productById[item.productId]?.categoryPath?.trim();
|
||||
if (path != null && path.isNotEmpty) {
|
||||
return path.split('>').first.trim();
|
||||
}
|
||||
if (item.category != null && item.category!.trim().isNotEmpty) {
|
||||
return item.category!.trim();
|
||||
}
|
||||
return context.l10n.pantryOtherCategory;
|
||||
String _resolveL1Category(PantryItem item) {
|
||||
return item.l1CategoryOrNull ?? context.l10n.pantryOtherCategory;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final pantryAsync = ref.watch(pantryProvider);
|
||||
final productsAsync = ref.watch(pantryProductsProvider);
|
||||
|
||||
if (pantryAsync.isLoading || productsAsync.isLoading) {
|
||||
if (pantryAsync.isLoading) {
|
||||
return LoadingStateView(label: context.l10n.pantryLoading);
|
||||
}
|
||||
|
||||
if (pantryAsync.hasError || productsAsync.hasError) {
|
||||
final error = pantryAsync.error ?? productsAsync.error;
|
||||
if (pantryAsync.hasError) {
|
||||
final error = pantryAsync.error;
|
||||
_logger.severe('Error loading pantry or products: $error');
|
||||
return buildCopyableErrorPanel(
|
||||
context: context,
|
||||
message: mapErrorToUserMessage(error ?? 'Okänt fel', context),
|
||||
onRetry: () {
|
||||
ref.invalidate(pantryProvider);
|
||||
ref.invalidate(pantryProductsProvider);
|
||||
},
|
||||
title: 'Kunde inte läsa baslagret',
|
||||
);
|
||||
@@ -250,15 +240,17 @@ class _PantryScreenState extends ConsumerState<PantryScreen> {
|
||||
|
||||
final pantryItems =
|
||||
pantryAsync.maybeWhen(data: (d) => d, orElse: () => null) ?? const [];
|
||||
final products =
|
||||
productsAsync.maybeWhen(data: (d) => d, orElse: () => null) ?? const [];
|
||||
final productById = {for (final product in products) product.id: product};
|
||||
|
||||
final filteredItems = pantryItems.where((item) {
|
||||
if (_locationFilter.isEmpty) return true;
|
||||
return (item.location ?? '').trim() == _locationFilter;
|
||||
}).toList();
|
||||
|
||||
final l1LowerByItemId = {
|
||||
for (final item in filteredItems)
|
||||
item.id: _resolveL1Category(item).toLowerCase(),
|
||||
};
|
||||
|
||||
filteredItems.sort((a, b) {
|
||||
if (_sort == 'nameDesc') {
|
||||
return b.displayName.toLowerCase().compareTo(a.displayName.toLowerCase());
|
||||
@@ -271,9 +263,7 @@ class _PantryScreenState extends ConsumerState<PantryScreen> {
|
||||
return a.displayName.toLowerCase().compareTo(b.displayName.toLowerCase());
|
||||
}
|
||||
if (_sort == 'l1CategoryAsc') {
|
||||
final byL1 = _resolveL1Category(a, productById).toLowerCase().compareTo(
|
||||
_resolveL1Category(b, productById).toLowerCase(),
|
||||
);
|
||||
final byL1 = (l1LowerByItemId[a.id] ?? '').compareTo(l1LowerByItemId[b.id] ?? '');
|
||||
if (byL1 != 0) return byL1;
|
||||
return a.displayName.toLowerCase().compareTo(b.displayName.toLowerCase());
|
||||
}
|
||||
@@ -379,7 +369,7 @@ class _PantryScreenState extends ConsumerState<PantryScreen> {
|
||||
if (index == 0) return filterSection;
|
||||
if (index == 1) return headerSection;
|
||||
final item = filteredItems[index - 2];
|
||||
final l1Category = _resolveL1Category(item, productById);
|
||||
final l1Category = _resolveL1Category(item);
|
||||
|
||||
return ListTile(
|
||||
title: Text(item.displayName),
|
||||
|
||||
Reference in New Issue
Block a user