feat: move inventory to own view model, add signing keys
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user