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,30 +261,7 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
List<InventoryItem> _sortedVisibleItems(List<InventoryItem> items, String sort) {
|
||||||
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));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
final visibleItems = [...items];
|
final visibleItems = [...items];
|
||||||
if (sort == 'l1CategoryAsc') {
|
if (sort == 'l1CategoryAsc') {
|
||||||
visibleItems.sort((a, b) {
|
visibleItems.sort((a, b) {
|
||||||
@@ -294,8 +272,11 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
return a.displayName.toLowerCase().compareTo(b.displayName.toLowerCase());
|
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),
|
padding: const EdgeInsets.fromLTRB(12, 12, 12, 4),
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
@@ -337,16 +318,16 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
)
|
)
|
||||||
.toList(),
|
.toList(),
|
||||||
onChanged: (value) {
|
onChanged: (value) {
|
||||||
ref
|
ref.read(inventorySortFilterProvider.notifier).setValue(value ?? '');
|
||||||
.read(inventorySortFilterProvider.notifier)
|
|
||||||
.setValue(value ?? '');
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
final headerSection = Padding(
|
Widget _buildHeaderSection(BuildContext context) {
|
||||||
|
return Padding(
|
||||||
padding: const EdgeInsets.fromLTRB(12, 12, 12, 4),
|
padding: const EdgeInsets.fromLTRB(12, 12, 12, 4),
|
||||||
child: Card(
|
child: Card(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
@@ -375,10 +356,14 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
final selectedSection = _selectedIds.isEmpty
|
Widget _buildSelectedSection(
|
||||||
? const SizedBox.shrink()
|
BuildContext context,
|
||||||
: Padding(
|
List<InventoryItem> visibleItems,
|
||||||
|
List<InventoryItem> allItems,
|
||||||
|
) {
|
||||||
|
return Padding(
|
||||||
padding: const EdgeInsets.fromLTRB(12, 4, 12, 4),
|
padding: const EdgeInsets.fromLTRB(12, 4, 12, 4),
|
||||||
child: Card(
|
child: Card(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
@@ -396,7 +381,7 @@ class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
|||||||
runSpacing: 8,
|
runSpacing: 8,
|
||||||
children: [
|
children: [
|
||||||
FilledButton.icon(
|
FilledButton.icon(
|
||||||
onPressed: () => _openBulkActions(context, items),
|
onPressed: () => _openBulkActions(context, allItems),
|
||||||
icon: const Icon(Icons.playlist_add_check),
|
icon: const Icon(Icons.playlist_add_check),
|
||||||
label: const Text('Hantera markerade'),
|
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) {
|
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