Files
kooltab/lib/viewmodels/inventory_view_model.dart
T
2026-07-29 01:42:34 +02:00

48 lines
1.2 KiB
Dart

import 'package:flutter/foundation.dart';
import '../models/product.dart';
import '../services/product_service.dart';
class InventoryViewModel extends ChangeNotifier {
final ProductService productService;
InventoryViewModel({required this.productService});
List<Product> _products = [];
List<Product> get products => _products;
Future<void> load() async {
_products = await productService.getProducts();
notifyListeners();
}
Future<void> decreaseStock(String productId, int amount) async {
await productService.decreaseStock(productId, amount);
final index = _products.indexWhere((product) => product.id == productId);
if (index != -1) {
_products[index] = _products[index].copyWith(
stockQuantity: _products[index].stockQuantity - amount,
);
}
notifyListeners();
}
Future<void> increaseStock(String productId, int amount) async {
await productService.increaseStock(productId, amount);
final index = _products.indexWhere((product) => product.id == productId);
if (index != -1) {
_products[index] = _products[index].copyWith(
stockQuantity: _products[index].stockQuantity + amount,
);
}
notifyListeners();
}
}