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/ui/async_state_views.dart';
|
||||
import '../../auth/data/auth_providers.dart';
|
||||
import '../domain/inventory_item.dart';
|
||||
import '../data/inventory_providers.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(() {
|
||||
_selectedIds
|
||||
..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
|
||||
.where((item) => _selectedIds.contains(item.id))
|
||||
.toList();
|
||||
@@ -165,7 +166,7 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
||||
|
||||
try {
|
||||
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(
|
||||
ids,
|
||||
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
|
||||
.where((item) => _selectedIds.contains(item.id))
|
||||
.map<int>((item) => item.id as int)
|
||||
.map<int>((item) => item.id)
|
||||
.toList();
|
||||
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;
|
||||
final action = await showDialog<String>(
|
||||
context: context,
|
||||
@@ -260,30 +261,7 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
@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 itemIds = items.map((item) => item.id).toSet();
|
||||
final staleIds = _selectedIds.where((id) => !itemIds.contains(id)).toList();
|
||||
if (staleIds.isNotEmpty) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_selectedIds.removeWhere((id) => !itemIds.contains(id));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
List<InventoryItem> _sortedVisibleItems(List<InventoryItem> items, String sort) {
|
||||
final visibleItems = [...items];
|
||||
if (sort == 'l1CategoryAsc') {
|
||||
visibleItems.sort((a, b) {
|
||||
@@ -294,8 +272,11 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
||||
return a.displayName.toLowerCase().compareTo(b.displayName.toLowerCase());
|
||||
});
|
||||
}
|
||||
return visibleItems;
|
||||
}
|
||||
|
||||
final filterSection = Padding(
|
||||
Widget _buildFilterSection(BuildContext context, String location, String sort) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(12, 12, 12, 4),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
@@ -337,16 +318,16 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
||||
)
|
||||
.toList(),
|
||||
onChanged: (value) {
|
||||
ref
|
||||
.read(inventorySortFilterProvider.notifier)
|
||||
.setValue(value ?? '');
|
||||
ref.read(inventorySortFilterProvider.notifier).setValue(value ?? '');
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final headerSection = Padding(
|
||||
Widget _buildHeaderSection(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(12, 12, 12, 4),
|
||||
child: Card(
|
||||
child: Padding(
|
||||
@@ -375,10 +356,14 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final selectedSection = _selectedIds.isEmpty
|
||||
? const SizedBox.shrink()
|
||||
: Padding(
|
||||
Widget _buildSelectedSection(
|
||||
BuildContext context,
|
||||
List<InventoryItem> visibleItems,
|
||||
List<InventoryItem> allItems,
|
||||
) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(12, 4, 12, 4),
|
||||
child: Card(
|
||||
child: Padding(
|
||||
@@ -396,7 +381,7 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
FilledButton.icon(
|
||||
onPressed: () => _openBulkActions(context, items),
|
||||
onPressed: () => _openBulkActions(context, allItems),
|
||||
icon: const Icon(Icons.playlist_add_check),
|
||||
label: const Text('Hantera markerade'),
|
||||
),
|
||||
@@ -417,6 +402,37 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmptyState(BuildContext context) {
|
||||
return EmptyStateView(title: context.l10n.inventoryEmpty);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
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 itemIds = items.map((item) => item.id).toSet();
|
||||
final staleIds = _selectedIds.where((id) => !itemIds.contains(id)).toList();
|
||||
if (staleIds.isNotEmpty) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_selectedIds.removeWhere((id) => !itemIds.contains(id));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
final visibleItems = _sortedVisibleItems(items, sort);
|
||||
|
||||
if (visibleItems.isEmpty) {
|
||||
return Stack(
|
||||
@@ -425,10 +441,10 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
||||
key: const PageStorageKey<String>('inventory-empty-list'),
|
||||
padding: const EdgeInsets.only(bottom: 88),
|
||||
children: [
|
||||
headerSection,
|
||||
selectedSection,
|
||||
filterSection,
|
||||
EmptyStateView(title: context.l10n.inventoryEmpty),
|
||||
_buildHeaderSection(context),
|
||||
if (_selectedIds.isNotEmpty) _buildSelectedSection(context, visibleItems, items),
|
||||
_buildFilterSection(context, location, sort),
|
||||
_buildEmptyState(context),
|
||||
],
|
||||
),
|
||||
Positioned(
|
||||
@@ -452,9 +468,11 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
||||
itemCount: visibleItems.length + (_selectedIds.isEmpty ? 2 : 3),
|
||||
separatorBuilder: (_, __) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
if (index == 0) return filterSection;
|
||||
if (index == 1) return headerSection;
|
||||
if (_selectedIds.isNotEmpty && index == 2) return selectedSection;
|
||||
if (index == 0) return _buildFilterSection(context, location, sort);
|
||||
if (index == 1) return _buildHeaderSection(context);
|
||||
if (_selectedIds.isNotEmpty && index == 2) {
|
||||
return _buildSelectedSection(context, visibleItems, items);
|
||||
}
|
||||
final item = visibleItems[index - (_selectedIds.isEmpty ? 2 : 3)];
|
||||
return SwipeableInventoryTile(
|
||||
item: item,
|
||||
|
||||
Reference in New Issue
Block a user