feat: Add bulk delete and merge functionality for inventory items with DTOs and API endpoints
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:
@@ -0,0 +1,11 @@
|
|||||||
|
import { Type } from 'class-transformer';
|
||||||
|
import { ArrayMinSize, IsArray, IsInt, Min } from 'class-validator';
|
||||||
|
|
||||||
|
export class BulkDeleteInventoryDto {
|
||||||
|
@IsArray()
|
||||||
|
@ArrayMinSize(1)
|
||||||
|
@Type(() => Number)
|
||||||
|
@IsInt({ each: true })
|
||||||
|
@Min(1, { each: true })
|
||||||
|
ids!: number[];
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import { Type } from 'class-transformer';
|
||||||
|
import { ArrayMinSize, IsArray, IsInt, IsOptional, IsString, Min } from 'class-validator';
|
||||||
|
|
||||||
|
export class MergeManyInventoryDto {
|
||||||
|
@IsArray()
|
||||||
|
@ArrayMinSize(2)
|
||||||
|
@Type(() => Number)
|
||||||
|
@IsInt({ each: true })
|
||||||
|
@Min(1, { each: true })
|
||||||
|
ids!: number[];
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsString()
|
||||||
|
targetUnit?: string;
|
||||||
|
}
|
||||||
@@ -18,6 +18,8 @@ import { CurrentUser } from '../auth/decorators/current-user.decorator';
|
|||||||
import { Roles } from '../auth/decorators/roles.decorator';
|
import { Roles } from '../auth/decorators/roles.decorator';
|
||||||
import { MergeInventoryDto } from './dto/merge-inventory.dto';
|
import { MergeInventoryDto } from './dto/merge-inventory.dto';
|
||||||
import { CreateAdminInventoryDto } from './dto/create-admin-inventory.dto';
|
import { CreateAdminInventoryDto } from './dto/create-admin-inventory.dto';
|
||||||
|
import { BulkDeleteInventoryDto } from './dto/bulk-delete-inventory.dto';
|
||||||
|
import { MergeManyInventoryDto } from './dto/merge-many-inventory.dto';
|
||||||
|
|
||||||
@Controller('inventory')
|
@Controller('inventory')
|
||||||
export class InventoryController {
|
export class InventoryController {
|
||||||
@@ -98,6 +100,22 @@ export class InventoryController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Post('merge-many')
|
||||||
|
mergeMany(
|
||||||
|
@CurrentUser() user: { userId: number },
|
||||||
|
@Body() body: MergeManyInventoryDto,
|
||||||
|
) {
|
||||||
|
return this.inventoryService.mergeMany(user.userId, body.ids, body.targetUnit);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post('bulk-delete')
|
||||||
|
bulkDelete(
|
||||||
|
@CurrentUser() user: { userId: number },
|
||||||
|
@Body() body: BulkDeleteInventoryDto,
|
||||||
|
) {
|
||||||
|
return this.inventoryService.bulkDelete(user.userId, body.ids);
|
||||||
|
}
|
||||||
|
|
||||||
@Post(':id/consume')
|
@Post(':id/consume')
|
||||||
consume(
|
consume(
|
||||||
@CurrentUser() user: { userId: number },
|
@CurrentUser() user: { userId: number },
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { Prisma } from '@prisma/client';
|
|||||||
import { PrismaService } from '../prisma/prisma.service';
|
import { PrismaService } from '../prisma/prisma.service';
|
||||||
import { CreateInventoryDto } from './dto/create-inventory.dto';
|
import { CreateInventoryDto } from './dto/create-inventory.dto';
|
||||||
import { UpdateInventoryDto } from './dto/update-inventory.dto';
|
import { UpdateInventoryDto } from './dto/update-inventory.dto';
|
||||||
|
import { convertUnit, normalizeUnit } from '../common/utils/units';
|
||||||
|
|
||||||
type InventoryQuery = {
|
type InventoryQuery = {
|
||||||
location?: string;
|
location?: string;
|
||||||
@@ -348,6 +349,132 @@ export class InventoryService {
|
|||||||
return this.prisma.inventoryItem.delete({ where: { id } });
|
return this.prisma.inventoryItem.delete({ where: { id } });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async bulkDelete(userId: number, ids: number[]) {
|
||||||
|
const uniqueIds = [...new Set(ids)];
|
||||||
|
if (uniqueIds.length === 0) {
|
||||||
|
throw new BadRequestException('No inventory ids supplied');
|
||||||
|
}
|
||||||
|
|
||||||
|
const items = await this.prisma.inventoryItem.findMany({
|
||||||
|
where: { id: { in: uniqueIds }, userId },
|
||||||
|
select: { id: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (items.length !== uniqueIds.length) {
|
||||||
|
throw new ForbiddenException('One or more inventory items are missing or do not belong to current user');
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await this.prisma.inventoryItem.deleteMany({
|
||||||
|
where: { id: { in: uniqueIds }, userId },
|
||||||
|
});
|
||||||
|
|
||||||
|
return { deletedCount: result.count };
|
||||||
|
}
|
||||||
|
|
||||||
|
async mergeMany(userId: number, ids: number[], targetUnit?: string) {
|
||||||
|
const uniqueIds = [...new Set(ids)];
|
||||||
|
if (uniqueIds.length < 2) {
|
||||||
|
throw new BadRequestException('At least two inventory items are required to merge');
|
||||||
|
}
|
||||||
|
|
||||||
|
const items = await this.prisma.inventoryItem.findMany({
|
||||||
|
where: { id: { in: uniqueIds }, userId },
|
||||||
|
include: {
|
||||||
|
product: this.productWithCategoryInclude,
|
||||||
|
},
|
||||||
|
orderBy: { createdAt: 'asc' },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (items.length !== uniqueIds.length) {
|
||||||
|
throw new ForbiddenException('One or more inventory items are missing or do not belong to current user');
|
||||||
|
}
|
||||||
|
|
||||||
|
const firstProductId = items[0].productId;
|
||||||
|
if (items.some((item) => item.productId !== firstProductId)) {
|
||||||
|
throw new BadRequestException('Selected inventory items must belong to the same product');
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalizedUnits = new Set(items.map((item) => normalizeUnit(item.unit)));
|
||||||
|
const resolvedTargetUnitRaw = targetUnit?.trim();
|
||||||
|
|
||||||
|
if (!resolvedTargetUnitRaw && normalizedUnits.size > 1) {
|
||||||
|
throw new BadRequestException('targetUnit is required when merging different units');
|
||||||
|
}
|
||||||
|
|
||||||
|
const resolvedTargetUnit = normalizeUnit(
|
||||||
|
resolvedTargetUnitRaw && resolvedTargetUnitRaw.length > 0
|
||||||
|
? resolvedTargetUnitRaw
|
||||||
|
: items[0].unit,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!items.some((item) => normalizeUnit(item.unit) === resolvedTargetUnit)) {
|
||||||
|
throw new BadRequestException('targetUnit must match one of the selected item units');
|
||||||
|
}
|
||||||
|
|
||||||
|
let mergedQuantity = 0;
|
||||||
|
for (const item of items) {
|
||||||
|
const quantity = Number(item.quantity);
|
||||||
|
try {
|
||||||
|
mergedQuantity += convertUnit(quantity, item.unit, resolvedTargetUnit);
|
||||||
|
} catch {
|
||||||
|
throw new BadRequestException(
|
||||||
|
`Cannot merge item ${item.id}: incompatible unit "${item.unit}" for target unit "${resolvedTargetUnit}"`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const target =
|
||||||
|
items.find((item) => normalizeUnit(item.unit) === resolvedTargetUnit) ??
|
||||||
|
items[0];
|
||||||
|
const sourceItems = items.filter((item) => item.id !== target.id);
|
||||||
|
|
||||||
|
const firstNonNull = <T>(values: (T | null | undefined)[]): T | null => {
|
||||||
|
for (const value of values) {
|
||||||
|
if (value !== null && value !== undefined) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
return this.prisma.$transaction(async (tx) => {
|
||||||
|
const updated = await tx.inventoryItem.update({
|
||||||
|
where: { id: target.id },
|
||||||
|
data: {
|
||||||
|
quantity: new Prisma.Decimal(mergedQuantity),
|
||||||
|
unit: resolvedTargetUnit,
|
||||||
|
location: target.location ?? firstNonNull(sourceItems.map((s) => s.location)),
|
||||||
|
brand: target.brand ?? firstNonNull(sourceItems.map((s) => s.brand)),
|
||||||
|
origin: target.origin ?? firstNonNull(sourceItems.map((s) => s.origin)),
|
||||||
|
receiptName: target.receiptName ?? firstNonNull(sourceItems.map((s) => s.receiptName)),
|
||||||
|
purchaseDate: target.purchaseDate ?? firstNonNull(sourceItems.map((s) => s.purchaseDate)),
|
||||||
|
opened: target.opened,
|
||||||
|
suitableFor: target.suitableFor ?? firstNonNull(sourceItems.map((s) => s.suitableFor)),
|
||||||
|
bestBeforeDate: target.bestBeforeDate ?? firstNonNull(sourceItems.map((s) => s.bestBeforeDate)),
|
||||||
|
comment: target.comment ?? firstNonNull(sourceItems.map((s) => s.comment)),
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
product: this.productWithCategoryInclude,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const sourceIds = sourceItems.map((item) => item.id);
|
||||||
|
|
||||||
|
if (sourceIds.length > 0) {
|
||||||
|
await tx.inventoryConsumption.updateMany({
|
||||||
|
where: { inventoryItemId: { in: sourceIds } },
|
||||||
|
data: { inventoryItemId: target.id },
|
||||||
|
});
|
||||||
|
|
||||||
|
await tx.inventoryItem.deleteMany({
|
||||||
|
where: { id: { in: sourceIds }, userId },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return updated;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private async moveInventoryItemToPantryCore(item: {
|
private async moveInventoryItemToPantryCore(item: {
|
||||||
id: number;
|
id: number;
|
||||||
userId: number;
|
userId: number;
|
||||||
|
|||||||
@@ -59,6 +59,8 @@ class RecipeApiPaths {
|
|||||||
|
|
||||||
class InventoryApiPaths {
|
class InventoryApiPaths {
|
||||||
static const list = '/inventory';
|
static const list = '/inventory';
|
||||||
|
static const mergeMany = '/inventory/merge-many';
|
||||||
|
static const bulkDelete = '/inventory/bulk-delete';
|
||||||
static String update(int id) => '/inventory/$id';
|
static String update(int id) => '/inventory/$id';
|
||||||
static String remove(int id) => '/inventory/$id';
|
static String remove(int id) => '/inventory/$id';
|
||||||
static String moveToPantry(int id) => '/inventory/$id/move-to-pantry';
|
static String moveToPantry(int id) => '/inventory/$id/move-to-pantry';
|
||||||
|
|||||||
@@ -168,12 +168,19 @@ class AppShell extends ConsumerWidget {
|
|||||||
PopupMenuButton<String>(
|
PopupMenuButton<String>(
|
||||||
tooltip: 'Profil och konto',
|
tooltip: 'Profil och konto',
|
||||||
icon: const Icon(Icons.account_circle_outlined),
|
icon: const Icon(Icons.account_circle_outlined),
|
||||||
onSelected: (value) {
|
onSelected: (value) async {
|
||||||
switch (value) {
|
switch (value) {
|
||||||
case 'profile':
|
case 'profile':
|
||||||
if (location != '/profile' && context.mounted) {
|
if (location != '/profile' && context.mounted) {
|
||||||
onNavigateToPath('/profile');
|
onNavigateToPath('/profile');
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case 'logout':
|
||||||
|
await ref.read(authStateProvider.notifier).logout();
|
||||||
|
if (context.mounted) {
|
||||||
|
onNavigateToPath('/login');
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
itemBuilder: (context) => const [
|
itemBuilder: (context) => const [
|
||||||
@@ -185,6 +192,15 @@ class AppShell extends ConsumerWidget {
|
|||||||
contentPadding: EdgeInsets.zero,
|
contentPadding: EdgeInsets.zero,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
PopupMenuDivider(),
|
||||||
|
PopupMenuItem<String>(
|
||||||
|
value: 'logout',
|
||||||
|
child: ListTile(
|
||||||
|
leading: Icon(Icons.logout),
|
||||||
|
title: Text('Logga ut'),
|
||||||
|
contentPadding: EdgeInsets.zero,
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -52,6 +52,31 @@ class InventoryRepository {
|
|||||||
await _api.deleteJson(InventoryApiPaths.remove(id), token: token);
|
await _api.deleteJson(InventoryApiPaths.remove(id), token: token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> bulkDeleteInventoryItems(List<int> ids, {String? token}) async {
|
||||||
|
await _api.postJson(
|
||||||
|
InventoryApiPaths.bulkDelete,
|
||||||
|
body: {'ids': ids},
|
||||||
|
token: token,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<InventoryItem> mergeInventoryItems(
|
||||||
|
List<int> ids, {
|
||||||
|
String? targetUnit,
|
||||||
|
String? token,
|
||||||
|
}) async {
|
||||||
|
final body = <String, dynamic>{'ids': ids};
|
||||||
|
if (targetUnit != null && targetUnit.trim().isNotEmpty) {
|
||||||
|
body['targetUnit'] = targetUnit.trim();
|
||||||
|
}
|
||||||
|
final data = await _api.postJson(
|
||||||
|
InventoryApiPaths.mergeMany,
|
||||||
|
body: body,
|
||||||
|
token: token,
|
||||||
|
);
|
||||||
|
return InventoryItem.fromJson(data as Map<String, dynamic>);
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> moveInventoryItemToPantry(int id, {String? token}) async {
|
Future<void> moveInventoryItemToPantry(int id, {String? token}) async {
|
||||||
await _api.postJson(InventoryApiPaths.moveToPantry(id), body: null, token: token);
|
await _api.postJson(InventoryApiPaths.moveToPantry(id), body: null, token: token);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,27 @@ import 'package:go_router/go_router.dart';
|
|||||||
import '../../../core/api/api_error_mapper.dart';
|
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 '../data/inventory_providers.dart';
|
import '../data/inventory_providers.dart';
|
||||||
import 'swipeable_inventory_tile.dart';
|
import 'swipeable_inventory_tile.dart';
|
||||||
|
|
||||||
class InventoryScreen extends ConsumerWidget {
|
class InventoryScreen extends ConsumerStatefulWidget {
|
||||||
const InventoryScreen({super.key});
|
const InventoryScreen({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
ConsumerState<InventoryScreen> createState() => _InventoryScreenState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _InventoryScreenState extends ConsumerState<InventoryScreen> {
|
||||||
|
final Set<int> _selectedIds = <int>{};
|
||||||
|
|
||||||
static const _locationOptions = <String>['', 'Kyl', 'Frys', 'Skafferi'];
|
static const _locationOptions = <String>['', 'Kyl', 'Frys', 'Skafferi'];
|
||||||
|
|
||||||
|
static const _weightOrVolumeUnits = <String>{
|
||||||
|
'g', 'gram', 'mg', 'milligram', 'hg', 'hektogram', 'kg', 'kilo', 'kilogram',
|
||||||
|
'ml', 'milliliter', 'cl', 'centiliter', 'dl', 'deciliter', 'l', 'liter',
|
||||||
|
};
|
||||||
|
|
||||||
List<({String value, String label})> _sortOptions(BuildContext context) => [
|
List<({String value, String label})> _sortOptions(BuildContext context) => [
|
||||||
(value: '', label: context.l10n.inventorySortLatest),
|
(value: '', label: context.l10n.inventorySortLatest),
|
||||||
(value: 'nameAsc', label: context.l10n.inventorySortNameAsc),
|
(value: 'nameAsc', label: context.l10n.inventorySortNameAsc),
|
||||||
@@ -20,6 +34,232 @@ class InventoryScreen extends ConsumerWidget {
|
|||||||
(value: 'l1CategoryAsc', label: 'L1-kategori (A-O)'),
|
(value: 'l1CategoryAsc', label: 'L1-kategori (A-O)'),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
void _startSelection(int id) {
|
||||||
|
setState(() {
|
||||||
|
_selectedIds.add(id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _toggleSelection(int id) {
|
||||||
|
setState(() {
|
||||||
|
if (_selectedIds.contains(id)) {
|
||||||
|
_selectedIds.remove(id);
|
||||||
|
} else {
|
||||||
|
_selectedIds.add(id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _clearSelection() {
|
||||||
|
setState(() {
|
||||||
|
_selectedIds.clear();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _selectAllVisible(List<dynamic> visibleItems) {
|
||||||
|
setState(() {
|
||||||
|
_selectedIds
|
||||||
|
..clear()
|
||||||
|
..addAll(visibleItems.map<int>((item) => item.id as int));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _isWeightOrVolumeUnit(String unit) {
|
||||||
|
return _weightOrVolumeUnits.contains(unit.trim().toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
String _preferredUnit(List<String> units) {
|
||||||
|
for (final unit in units) {
|
||||||
|
if (_isWeightOrVolumeUnit(unit)) return unit;
|
||||||
|
}
|
||||||
|
return units.first;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String?> _askMergeTargetUnit(BuildContext context, List<String> units) async {
|
||||||
|
if (units.isEmpty) return null;
|
||||||
|
if (units.length == 1) return units.first;
|
||||||
|
|
||||||
|
var selected = _preferredUnit(units);
|
||||||
|
|
||||||
|
return showDialog<String>(
|
||||||
|
context: context,
|
||||||
|
builder: (dialogContext) => StatefulBuilder(
|
||||||
|
builder: (dialogContext, setDialogState) => AlertDialog(
|
||||||
|
title: const Text('Välj enhet för merge'),
|
||||||
|
content: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
const Text(
|
||||||
|
'Poster har olika enhet. Välj vilken enhet den sammanslagna posten ska använda.',
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
DropdownButtonFormField<String>(
|
||||||
|
initialValue: selected,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
labelText: 'Enhet',
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
items: units
|
||||||
|
.map(
|
||||||
|
(unit) => DropdownMenuItem<String>(
|
||||||
|
value: unit,
|
||||||
|
child: Text(unit),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
onChanged: (value) {
|
||||||
|
if (value == null) return;
|
||||||
|
setDialogState(() => selected = value);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(dialogContext),
|
||||||
|
child: Text(context.l10n.cancelAction),
|
||||||
|
),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: () => Navigator.pop(dialogContext, selected),
|
||||||
|
child: const Text('Merge'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _mergeSelected(BuildContext context, List<dynamic> allItems) async {
|
||||||
|
final selectedItems = allItems
|
||||||
|
.where((item) => _selectedIds.contains(item.id))
|
||||||
|
.toList();
|
||||||
|
|
||||||
|
if (selectedItems.length < 2) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(content: Text('Välj minst två poster för merge.')),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final productIds = selectedItems.map((item) => item.productId).toSet();
|
||||||
|
if (productIds.length > 1) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(content: Text('Valda poster måste ha samma produkt för merge.')),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final units = <String>[];
|
||||||
|
final seenUnits = <String>{};
|
||||||
|
for (final item in selectedItems) {
|
||||||
|
final unit = (item.unit as String).trim();
|
||||||
|
final key = unit.toLowerCase();
|
||||||
|
if (seenUnits.add(key)) {
|
||||||
|
units.add(unit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final targetUnit = await _askMergeTargetUnit(context, units);
|
||||||
|
if (!context.mounted || targetUnit == null) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
final token = await ref.read(authStateProvider.future);
|
||||||
|
final ids = selectedItems.map<int>((item) => item.id as int).toList();
|
||||||
|
await ref.read(inventoryRepositoryProvider).mergeInventoryItems(
|
||||||
|
ids,
|
||||||
|
targetUnit: units.length > 1 ? targetUnit : null,
|
||||||
|
token: token,
|
||||||
|
);
|
||||||
|
ref.invalidate(inventoryProvider);
|
||||||
|
if (!mounted) return;
|
||||||
|
setState(() => _selectedIds.clear());
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(content: Text('Poster sammanslagna.')),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
if (!mounted) return;
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
buildCopyableErrorSnackBar(context, mapErrorToUserMessage(e, context)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _deleteSelected(BuildContext context, List<dynamic> allItems) async {
|
||||||
|
final ids = allItems
|
||||||
|
.where((item) => _selectedIds.contains(item.id))
|
||||||
|
.map<int>((item) => item.id as int)
|
||||||
|
.toList();
|
||||||
|
if (ids.isEmpty) return;
|
||||||
|
|
||||||
|
final confirmed = await showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
builder: (dialogContext) => AlertDialog(
|
||||||
|
title: const Text('Ta bort markerade poster'),
|
||||||
|
content: Text('Vill du verkligen ta bort ${ids.length} markerade poster?'),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(dialogContext, false),
|
||||||
|
child: Text(context.l10n.cancelAction),
|
||||||
|
),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: () => Navigator.pop(dialogContext, true),
|
||||||
|
child: Text(context.l10n.deleteAction),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (confirmed != true || !context.mounted) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
final token = await ref.read(authStateProvider.future);
|
||||||
|
await ref.read(inventoryRepositoryProvider).bulkDeleteInventoryItems(ids, token: token);
|
||||||
|
ref.invalidate(inventoryProvider);
|
||||||
|
if (!mounted) return;
|
||||||
|
setState(() => _selectedIds.clear());
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('${ids.length} poster borttagna.')),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
if (!mounted) return;
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
buildCopyableErrorSnackBar(context, mapErrorToUserMessage(e, context)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _openBulkActions(BuildContext context, List<dynamic> allItems) async {
|
||||||
|
if (_selectedIds.isEmpty) return;
|
||||||
|
final action = await showDialog<String>(
|
||||||
|
context: context,
|
||||||
|
builder: (dialogContext) => AlertDialog(
|
||||||
|
title: const Text('Hantera markerade poster'),
|
||||||
|
content: Text('Du har markerat ${_selectedIds.length} poster. Välj åtgärd.'),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(dialogContext),
|
||||||
|
child: Text(context.l10n.cancelAction),
|
||||||
|
),
|
||||||
|
OutlinedButton(
|
||||||
|
onPressed: () => Navigator.pop(dialogContext, 'delete'),
|
||||||
|
child: Text(context.l10n.deleteAction),
|
||||||
|
),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: () => Navigator.pop(dialogContext, 'merge'),
|
||||||
|
child: const Text('Merge'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (action == 'merge') {
|
||||||
|
await _mergeSelected(context, allItems);
|
||||||
|
} else if (action == 'delete') {
|
||||||
|
await _deleteSelected(context, allItems);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final location = ref.watch(inventoryLocationFilterProvider);
|
final location = ref.watch(inventoryLocationFilterProvider);
|
||||||
@@ -33,6 +273,17 @@ class InventoryScreen extends ConsumerWidget {
|
|||||||
onRetry: () => ref.invalidate(inventoryProvider),
|
onRetry: () => ref.invalidate(inventoryProvider),
|
||||||
),
|
),
|
||||||
data: (items) {
|
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) {
|
||||||
@@ -125,6 +376,48 @@ class InventoryScreen extends ConsumerWidget {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
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(
|
||||||
children: [
|
children: [
|
||||||
@@ -133,6 +426,7 @@ class InventoryScreen extends ConsumerWidget {
|
|||||||
padding: const EdgeInsets.only(bottom: 88),
|
padding: const EdgeInsets.only(bottom: 88),
|
||||||
children: [
|
children: [
|
||||||
headerSection,
|
headerSection,
|
||||||
|
selectedSection,
|
||||||
filterSection,
|
filterSection,
|
||||||
EmptyStateView(title: context.l10n.inventoryEmpty),
|
EmptyStateView(title: context.l10n.inventoryEmpty),
|
||||||
],
|
],
|
||||||
@@ -155,13 +449,26 @@ class InventoryScreen extends ConsumerWidget {
|
|||||||
ListView.separated(
|
ListView.separated(
|
||||||
key: const PageStorageKey<String>('inventory-main-list'),
|
key: const PageStorageKey<String>('inventory-main-list'),
|
||||||
padding: const EdgeInsets.only(bottom: 88),
|
padding: const EdgeInsets.only(bottom: 88),
|
||||||
itemCount: visibleItems.length + 2,
|
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 filterSection;
|
||||||
if (index == 1) return headerSection;
|
if (index == 1) return headerSection;
|
||||||
final item = visibleItems[index - 2];
|
if (_selectedIds.isNotEmpty && index == 2) return selectedSection;
|
||||||
return SwipeableInventoryTile(item: item);
|
final item = visibleItems[index - (_selectedIds.isEmpty ? 2 : 3)];
|
||||||
|
return SwipeableInventoryTile(
|
||||||
|
item: item,
|
||||||
|
selectableMode: _selectedIds.isNotEmpty,
|
||||||
|
selected: _selectedIds.contains(item.id),
|
||||||
|
onToggleSelected: () => _toggleSelection(item.id),
|
||||||
|
onLongPress: () {
|
||||||
|
if (_selectedIds.isEmpty) {
|
||||||
|
_startSelection(item.id);
|
||||||
|
} else {
|
||||||
|
_toggleSelection(item.id);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
Positioned(
|
Positioned(
|
||||||
|
|||||||
@@ -52,8 +52,19 @@ String _fmtStep(double step) =>
|
|||||||
/// know the gesture is available without any extra instruction.
|
/// know the gesture is available without any extra instruction.
|
||||||
class SwipeableInventoryTile extends ConsumerStatefulWidget {
|
class SwipeableInventoryTile extends ConsumerStatefulWidget {
|
||||||
final InventoryItem item;
|
final InventoryItem item;
|
||||||
|
final bool selectableMode;
|
||||||
|
final bool selected;
|
||||||
|
final VoidCallback? onToggleSelected;
|
||||||
|
final VoidCallback? onLongPress;
|
||||||
|
|
||||||
const SwipeableInventoryTile({super.key, required this.item});
|
const SwipeableInventoryTile({
|
||||||
|
super.key,
|
||||||
|
required this.item,
|
||||||
|
this.selectableMode = false,
|
||||||
|
this.selected = false,
|
||||||
|
this.onToggleSelected,
|
||||||
|
this.onLongPress,
|
||||||
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
ConsumerState<SwipeableInventoryTile> createState() =>
|
ConsumerState<SwipeableInventoryTile> createState() =>
|
||||||
@@ -74,14 +85,14 @@ class _SwipeableInventoryTileState
|
|||||||
// ── Gesture handlers ────────────────────────────────────────────────────
|
// ── Gesture handlers ────────────────────────────────────────────────────
|
||||||
|
|
||||||
void _onUpdate(DragUpdateDetails d) {
|
void _onUpdate(DragUpdateDetails d) {
|
||||||
if (_acting) return;
|
if (_acting || widget.selectableMode) return;
|
||||||
setState(() {
|
setState(() {
|
||||||
_drag = (_drag + d.delta.dx).clamp(-_maxReveal, _maxReveal);
|
_drag = (_drag + d.delta.dx).clamp(-_maxReveal, _maxReveal);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onEnd(DragEndDetails d) {
|
void _onEnd(DragEndDetails d) {
|
||||||
if (_acting) return;
|
if (_acting || widget.selectableMode) return;
|
||||||
if (_drag > _threshold) {
|
if (_drag > _threshold) {
|
||||||
_adjust(1);
|
_adjust(1);
|
||||||
} else if (_drag < -_threshold) {
|
} else if (_drag < -_threshold) {
|
||||||
@@ -144,10 +155,28 @@ class _SwipeableInventoryTileState
|
|||||||
final stepLabel = _fmtStep(step);
|
final stepLabel = _fmtStep(step);
|
||||||
final unit = widget.item.unit;
|
final unit = widget.item.unit;
|
||||||
|
|
||||||
|
final foreground = _ForegroundTile(
|
||||||
|
item: widget.item,
|
||||||
|
acting: _acting,
|
||||||
|
selectableMode: widget.selectableMode,
|
||||||
|
selected: widget.selected,
|
||||||
|
onToggleSelected: widget.onToggleSelected,
|
||||||
|
onLongPress: widget.onLongPress,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (widget.selectableMode) {
|
||||||
|
return GestureDetector(
|
||||||
|
onLongPress: widget.onLongPress,
|
||||||
|
behavior: HitTestBehavior.opaque,
|
||||||
|
child: foreground,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return GestureDetector(
|
return GestureDetector(
|
||||||
onHorizontalDragUpdate: _onUpdate,
|
onHorizontalDragUpdate: _onUpdate,
|
||||||
onHorizontalDragEnd: _onEnd,
|
onHorizontalDragEnd: _onEnd,
|
||||||
onHorizontalDragCancel: _snapBack,
|
onHorizontalDragCancel: _snapBack,
|
||||||
|
onLongPress: widget.onLongPress,
|
||||||
behavior: HitTestBehavior.opaque,
|
behavior: HitTestBehavior.opaque,
|
||||||
child: ClipRect(
|
child: ClipRect(
|
||||||
child: Stack(
|
child: Stack(
|
||||||
@@ -226,7 +255,7 @@ class _SwipeableInventoryTileState
|
|||||||
// ── Foreground tile ─────────────────────────────────────────
|
// ── Foreground tile ─────────────────────────────────────────
|
||||||
Transform.translate(
|
Transform.translate(
|
||||||
offset: Offset(_drag, 0),
|
offset: Offset(_drag, 0),
|
||||||
child: _ForegroundTile(item: widget.item, acting: _acting),
|
child: foreground,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@@ -240,8 +269,19 @@ class _SwipeableInventoryTileState
|
|||||||
class _ForegroundTile extends ConsumerWidget {
|
class _ForegroundTile extends ConsumerWidget {
|
||||||
final InventoryItem item;
|
final InventoryItem item;
|
||||||
final bool acting;
|
final bool acting;
|
||||||
|
final bool selectableMode;
|
||||||
|
final bool selected;
|
||||||
|
final VoidCallback? onToggleSelected;
|
||||||
|
final VoidCallback? onLongPress;
|
||||||
|
|
||||||
const _ForegroundTile({required this.item, required this.acting});
|
const _ForegroundTile({
|
||||||
|
required this.item,
|
||||||
|
required this.acting,
|
||||||
|
this.selectableMode = false,
|
||||||
|
this.selected = false,
|
||||||
|
this.onToggleSelected,
|
||||||
|
this.onLongPress,
|
||||||
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
@@ -258,16 +298,24 @@ class _ForegroundTile extends ConsumerWidget {
|
|||||||
// Opaque background so the tile sits on top of the reveal panels.
|
// Opaque background so the tile sits on top of the reveal panels.
|
||||||
color: theme.colorScheme.surface,
|
color: theme.colorScheme.surface,
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
title: Text(item.productName),
|
leading: selectableMode
|
||||||
|
? Checkbox(
|
||||||
|
value: selected,
|
||||||
|
onChanged: (_) => onToggleSelected?.call(),
|
||||||
|
)
|
||||||
|
: null,
|
||||||
|
title: Text(item.displayName),
|
||||||
// Subtitle: small swipe-hint icon + text on the same row.
|
// Subtitle: small swipe-hint icon + text on the same row.
|
||||||
subtitle: Row(
|
subtitle: Row(
|
||||||
children: [
|
children: [
|
||||||
|
if (!selectableMode) ...[
|
||||||
Icon(
|
Icon(
|
||||||
Icons.swap_horiz,
|
Icons.swap_horiz,
|
||||||
size: 13,
|
size: 13,
|
||||||
color: theme.colorScheme.outline,
|
color: theme.colorScheme.outline,
|
||||||
),
|
),
|
||||||
const SizedBox(width: 3),
|
const SizedBox(width: 3),
|
||||||
|
],
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Text(
|
child: Text(
|
||||||
subtitleText,
|
subtitleText,
|
||||||
@@ -282,8 +330,11 @@ class _ForegroundTile extends ConsumerWidget {
|
|||||||
height: 24,
|
height: 24,
|
||||||
child: CircularProgressIndicator(strokeWidth: 2),
|
child: CircularProgressIndicator(strokeWidth: 2),
|
||||||
)
|
)
|
||||||
: _TrailingActions(item: item),
|
: (selectableMode ? null : _TrailingActions(item: item)),
|
||||||
onTap: () => context.push('/inventory/${item.id}'),
|
onTap: selectableMode
|
||||||
|
? onToggleSelected
|
||||||
|
: () => context.push('/inventory/${item.id}'),
|
||||||
|
onLongPress: onLongPress,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user