Files
recipe-app/flutter/lib/features/inventory/presentation/inventory_screen.dart
T
Nils-Johan Gynther 84ccabe2fe
Test Suite / test (24.15.0) (push) Has been cancelled
feat: Add functionality to move inventory items to pantry and enhance pantry management
- Implemented moveInventoryItemToPantry method in InventoryRepository to facilitate moving items from inventory to pantry.
- Enhanced InventoryScreen with a new header section providing context about the inventory.
- Added a button in SwipeableInventoryTile to move items to pantry with appropriate error handling.
- Introduced movePantryItemToInventory method in PantryRepository to support moving items back to inventory.
- Refactored PantryScreen to rename _addToInventory to _moveToInventory for clarity and updated UI to reflect changes.
- Added AdminPantryItem model to represent pantry items in the admin panel.
- Created AdminPantryPanel for managing pantry items, including moving items to inventory and listing users.
- Developed AdminPrivateProductsPanel for managing private products, allowing promotion to global products.
2026-05-11 09:06:30 +02:00

197 lines
7.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../../core/api/api_error_mapper.dart';
import '../../../core/l10n/l10n.dart';
import '../../../core/ui/async_state_views.dart';
import '../data/inventory_providers.dart';
import 'swipeable_inventory_tile.dart';
class InventoryScreen extends ConsumerWidget {
const InventoryScreen({super.key});
static const _locationOptions = <String>['', 'Kyl', 'Frys', 'Skafferi'];
List<({String value, String label})> _sortOptions(BuildContext context) => [
(value: '', label: context.l10n.inventorySortLatest),
(value: 'nameAsc', label: context.l10n.inventorySortNameAsc),
(value: 'bestBeforeAsc', label: context.l10n.inventorySortBestBeforeAsc),
(value: 'bestBeforeDesc', label: context.l10n.inventorySortBestBeforeDesc),
(value: 'l1CategoryAsc', label: 'L1-kategori (A-O)'),
];
@override
Widget build(BuildContext context, WidgetRef ref) {
final location = ref.watch(inventoryLocationFilterProvider);
final sort = ref.watch(inventorySortFilterProvider);
final inventoryAsync = ref.watch(inventoryProvider);
return inventoryAsync.when(
loading: () => LoadingStateView(label: context.l10n.inventoryLoading),
error: (e, _) => ErrorStateView(
message: mapErrorToUserMessage(e, context),
onRetry: () => ref.invalidate(inventoryProvider),
),
data: (items) {
final visibleItems = [...items];
if (sort == 'l1CategoryAsc') {
visibleItems.sort((a, b) {
final byCategory = a.l1Category.toLowerCase().compareTo(
b.l1Category.toLowerCase(),
);
if (byCategory != 0) return byCategory;
return a.displayName.toLowerCase().compareTo(b.displayName.toLowerCase());
});
}
final filterSection = Padding(
padding: const EdgeInsets.fromLTRB(12, 12, 12, 4),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
context.l10n.inventoryFilterAndSort,
style: const TextStyle(fontWeight: FontWeight.w600),
),
const SizedBox(height: 8),
Wrap(
spacing: 8,
runSpacing: 8,
children: _locationOptions
.map(
(option) => ChoiceChip(
label: Text(option.isEmpty ? context.l10n.inventoryAllFilter : option),
selected: location == option,
onSelected: (_) => ref
.read(inventoryLocationFilterProvider.notifier)
.setValue(option),
),
)
.toList(),
),
const SizedBox(height: 8),
DropdownButtonFormField<String>(
initialValue: sort,
isExpanded: true,
decoration: InputDecoration(
labelText: context.l10n.inventorySortLabel,
border: const OutlineInputBorder(),
),
items: _sortOptions(context)
.map(
(option) => DropdownMenuItem<String>(
value: option.value,
child: Text(option.label),
),
)
.toList(),
onChanged: (value) {
ref
.read(inventorySortFilterProvider.notifier)
.setValue(value ?? '');
},
),
],
),
);
final headerSection = Padding(
padding: const EdgeInsets.fromLTRB(12, 12, 12, 4),
child: Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(context.l10n.profileInventoryTab, style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 8),
Text(
'Din personliga inventarie. Här ser du sådant du faktiskt äger, kan sortera på plats och bäst före, och flytta vidare till recept eller baslager.',
style: Theme.of(context).textTheme.bodyMedium,
),
const SizedBox(height: 8),
const Wrap(
spacing: 8,
runSpacing: 8,
children: [
Chip(label: Text('User-scope')),
Chip(label: Text('Bäst före')),
Chip(label: Text('Swipa för +/-')),
],
),
],
),
),
),
);
if (visibleItems.isEmpty) {
return Stack(
children: [
ListView(
key: const PageStorageKey<String>('inventory-empty-list'),
padding: const EdgeInsets.only(bottom: 88),
children: [
headerSection,
filterSection,
EmptyStateView(title: context.l10n.inventoryEmpty),
],
),
Positioned(
right: 16,
bottom: 16,
child: FloatingActionButton.extended(
heroTag: 'inventory_add_empty',
onPressed: () => context.push('/inventory/create'),
icon: const Icon(Icons.add),
label: Text(context.l10n.addAction),
),
),
],
);
}
return Stack(
children: [
ListView.separated(
key: const PageStorageKey<String>('inventory-main-list'),
padding: const EdgeInsets.only(bottom: 88),
itemCount: visibleItems.length + 2,
separatorBuilder: (_, __) => const Divider(height: 1),
itemBuilder: (context, index) {
if (index == 0) return filterSection;
if (index == 1) return headerSection;
final item = visibleItems[index - 2];
return SwipeableInventoryTile(item: item);
},
),
Positioned(
right: 16,
bottom: 16,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
FloatingActionButton.extended(
heroTag: 'inventory_add',
onPressed: () => context.push('/inventory/create'),
icon: const Icon(Icons.add),
label: Text(context.l10n.addAction),
),
const SizedBox(height: 8),
FloatingActionButton.extended(
heroTag: 'inventory_go_recipes',
onPressed: () => context.go('/recipes'),
icon: const Icon(Icons.restaurant_menu),
label: Text(context.l10n.inventoryRecipesAction),
),
],
),
),
],
);
},
);
}
}