feat: Refactor inventory screen to improve type safety and enhance UI structure with dedicated widget methods
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:
@@ -6,6 +6,7 @@ import '../../../core/api/api_error_mapper.dart';
|
|||||||
import '../../../core/l10n/l10n.dart';
|
import '../../../core/l10n/l10n.dart';
|
||||||
import '../../../core/ui/async_state_views.dart';
|
import '../../../core/ui/async_state_views.dart';
|
||||||
import '../../auth/data/auth_providers.dart';
|
import '../../auth/data/auth_providers.dart';
|
||||||
|
import '../domain/inventory_item.dart';
|
||||||
import '../data/inventory_providers.dart';
|
import '../data/inventory_providers.dart';
|
||||||
import 'swipeable_inventory_tile.dart';
|
import 'swipeable_inventory_tile.dart';
|
||||||
|
|
||||||
@@ -56,11 +57,11 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void _selectAllVisible(List<dynamic> visibleItems) {
|
void _selectAllVisible(List<InventoryItem> visibleItems) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_selectedIds
|
_selectedIds
|
||||||
..clear()
|
..clear()
|
||||||
..addAll(visibleItems.map<int>((item) => item.id as int));
|
..addAll(visibleItems.map<int>((item) => item.id));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +131,7 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _mergeSelected(BuildContext context, List<dynamic> allItems) async {
|
Future<void> _mergeSelected(BuildContext context, List<InventoryItem> allItems) async {
|
||||||
final selectedItems = allItems
|
final selectedItems = allItems
|
||||||
.where((item) => _selectedIds.contains(item.id))
|
.where((item) => _selectedIds.contains(item.id))
|
||||||
.toList();
|
.toList();
|
||||||
@@ -165,7 +166,7 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
final token = await ref.read(authStateProvider.future);
|
final token = await ref.read(authStateProvider.future);
|
||||||
final ids = selectedItems.map<int>((item) => item.id as int).toList();
|
final ids = selectedItems.map<int>((item) => item.id).toList();
|
||||||
await ref.read(inventoryRepositoryProvider).mergeInventoryItems(
|
await ref.read(inventoryRepositoryProvider).mergeInventoryItems(
|
||||||
ids,
|
ids,
|
||||||
targetUnit: units.length > 1 ? targetUnit : null,
|
targetUnit: units.length > 1 ? targetUnit : null,
|
||||||
@@ -185,10 +186,10 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _deleteSelected(BuildContext context, List<dynamic> allItems) async {
|
Future<void> _deleteSelected(BuildContext context, List<InventoryItem> allItems) async {
|
||||||
final ids = allItems
|
final ids = allItems
|
||||||
.where((item) => _selectedIds.contains(item.id))
|
.where((item) => _selectedIds.contains(item.id))
|
||||||
.map<int>((item) => item.id as int)
|
.map<int>((item) => item.id)
|
||||||
.toList();
|
.toList();
|
||||||
if (ids.isEmpty) return;
|
if (ids.isEmpty) return;
|
||||||
|
|
||||||
@@ -229,7 +230,7 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _openBulkActions(BuildContext context, List<dynamic> allItems) async {
|
Future<void> _openBulkActions(BuildContext context, List<InventoryItem> allItems) async {
|
||||||
if (_selectedIds.isEmpty) return;
|
if (_selectedIds.isEmpty) return;
|
||||||
final action = await showDialog<String>(
|
final action = await showDialog<String>(
|
||||||
context: context,
|
context: context,
|
||||||
@@ -260,8 +261,155 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<InventoryItem> _sortedVisibleItems(List<InventoryItem> items, String sort) {
|
||||||
|
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());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return visibleItems;
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildFilterSection(BuildContext context, String location, String sort) {
|
||||||
|
return 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 ?? '');
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildHeaderSection(BuildContext context) {
|
||||||
|
return 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 +/-')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildSelectedSection(
|
||||||
|
BuildContext context,
|
||||||
|
List<InventoryItem> visibleItems,
|
||||||
|
List<InventoryItem> allItems,
|
||||||
|
) {
|
||||||
|
return Padding(
|
||||||
|
padding: const EdgeInsets.fromLTRB(12, 4, 12, 4),
|
||||||
|
child: Card(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(12),
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
'${_selectedIds.length} markerade poster',
|
||||||
|
style: Theme.of(context).textTheme.titleSmall,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
Wrap(
|
||||||
|
spacing: 8,
|
||||||
|
runSpacing: 8,
|
||||||
|
children: [
|
||||||
|
FilledButton.icon(
|
||||||
|
onPressed: () => _openBulkActions(context, allItems),
|
||||||
|
icon: const Icon(Icons.playlist_add_check),
|
||||||
|
label: const Text('Hantera markerade'),
|
||||||
|
),
|
||||||
|
OutlinedButton.icon(
|
||||||
|
onPressed: () => _selectAllVisible(visibleItems),
|
||||||
|
icon: const Icon(Icons.select_all),
|
||||||
|
label: const Text('Markera alla synliga'),
|
||||||
|
),
|
||||||
|
OutlinedButton.icon(
|
||||||
|
onPressed: _clearSelection,
|
||||||
|
icon: const Icon(Icons.deselect),
|
||||||
|
label: const Text('Avmarkera'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildEmptyState(BuildContext context) {
|
||||||
|
return EmptyStateView(title: context.l10n.inventoryEmpty);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context) {
|
||||||
final location = ref.watch(inventoryLocationFilterProvider);
|
final location = ref.watch(inventoryLocationFilterProvider);
|
||||||
final sort = ref.watch(inventorySortFilterProvider);
|
final sort = ref.watch(inventorySortFilterProvider);
|
||||||
final inventoryAsync = ref.watch(inventoryProvider);
|
final inventoryAsync = ref.watch(inventoryProvider);
|
||||||
@@ -284,139 +432,7 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
final visibleItems = [...items];
|
final visibleItems = _sortedVisibleItems(items, sort);
|
||||||
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 +/-')),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
final selectedSection = _selectedIds.isEmpty
|
|
||||||
? const SizedBox.shrink()
|
|
||||||
: Padding(
|
|
||||||
padding: const EdgeInsets.fromLTRB(12, 4, 12, 4),
|
|
||||||
child: Card(
|
|
||||||
child: Padding(
|
|
||||||
padding: const EdgeInsets.all(12),
|
|
||||||
child: Column(
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
|
||||||
children: [
|
|
||||||
Text(
|
|
||||||
'${_selectedIds.length} markerade poster',
|
|
||||||
style: Theme.of(context).textTheme.titleSmall,
|
|
||||||
),
|
|
||||||
const SizedBox(height: 8),
|
|
||||||
Wrap(
|
|
||||||
spacing: 8,
|
|
||||||
runSpacing: 8,
|
|
||||||
children: [
|
|
||||||
FilledButton.icon(
|
|
||||||
onPressed: () => _openBulkActions(context, items),
|
|
||||||
icon: const Icon(Icons.playlist_add_check),
|
|
||||||
label: const Text('Hantera markerade'),
|
|
||||||
),
|
|
||||||
OutlinedButton.icon(
|
|
||||||
onPressed: () => _selectAllVisible(visibleItems),
|
|
||||||
icon: const Icon(Icons.select_all),
|
|
||||||
label: const Text('Markera alla synliga'),
|
|
||||||
),
|
|
||||||
OutlinedButton.icon(
|
|
||||||
onPressed: _clearSelection,
|
|
||||||
icon: const Icon(Icons.deselect),
|
|
||||||
label: const Text('Avmarkera'),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (visibleItems.isEmpty) {
|
if (visibleItems.isEmpty) {
|
||||||
return Stack(
|
return Stack(
|
||||||
@@ -425,10 +441,10 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
key: const PageStorageKey<String>('inventory-empty-list'),
|
key: const PageStorageKey<String>('inventory-empty-list'),
|
||||||
padding: const EdgeInsets.only(bottom: 88),
|
padding: const EdgeInsets.only(bottom: 88),
|
||||||
children: [
|
children: [
|
||||||
headerSection,
|
_buildHeaderSection(context),
|
||||||
selectedSection,
|
if (_selectedIds.isNotEmpty) _buildSelectedSection(context, visibleItems, items),
|
||||||
filterSection,
|
_buildFilterSection(context, location, sort),
|
||||||
EmptyStateView(title: context.l10n.inventoryEmpty),
|
_buildEmptyState(context),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
Positioned(
|
Positioned(
|
||||||
@@ -452,9 +468,11 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
itemCount: visibleItems.length + (_selectedIds.isEmpty ? 2 : 3),
|
itemCount: visibleItems.length + (_selectedIds.isEmpty ? 2 : 3),
|
||||||
separatorBuilder: (_, __) => const Divider(height: 1),
|
separatorBuilder: (_, __) => const Divider(height: 1),
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
if (index == 0) return filterSection;
|
if (index == 0) return _buildFilterSection(context, location, sort);
|
||||||
if (index == 1) return headerSection;
|
if (index == 1) return _buildHeaderSection(context);
|
||||||
if (_selectedIds.isNotEmpty && index == 2) return selectedSection;
|
if (_selectedIds.isNotEmpty && index == 2) {
|
||||||
|
return _buildSelectedSection(context, visibleItems, items);
|
||||||
|
}
|
||||||
final item = visibleItems[index - (_selectedIds.isEmpty ? 2 : 3)];
|
final item = visibleItems[index - (_selectedIds.isEmpty ? 2 : 3)];
|
||||||
return SwipeableInventoryTile(
|
return SwipeableInventoryTile(
|
||||||
item: item,
|
item: item,
|
||||||
|
|||||||
Reference in New Issue
Block a user