61 lines
1.3 KiB
Dart
61 lines
1.3 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();
|
|
}
|
|
} |