feat: move inventory to own view model, add signing keys
This commit is contained in:
@@ -4,15 +4,15 @@ import '../models/bar_tab.dart';
|
||||
import '../models/product.dart';
|
||||
import '../models/tab_item.dart';
|
||||
import '../services/bar_tab_service.dart';
|
||||
import '../services/product_service.dart';
|
||||
import 'inventory_view_model.dart';
|
||||
|
||||
class BarScreenViewModel extends ChangeNotifier {
|
||||
final BarTabService barTabService;
|
||||
final ProductService productService;
|
||||
final InventoryViewModel inventory;
|
||||
|
||||
BarScreenViewModel({
|
||||
required this.barTabService,
|
||||
required this.productService,
|
||||
required this.inventory,
|
||||
});
|
||||
|
||||
List<BarTab> _tabs = [];
|
||||
@@ -96,7 +96,7 @@ class BarScreenViewModel extends ChangeNotifier {
|
||||
product: product,
|
||||
);
|
||||
|
||||
await productService.decreaseStock(product.id, 1);
|
||||
await inventory.decreaseStock(product.id, 1);
|
||||
|
||||
await _reloadTabs();
|
||||
}
|
||||
@@ -110,9 +110,9 @@ class BarScreenViewModel extends ChangeNotifier {
|
||||
);
|
||||
|
||||
if (difference > 0) {
|
||||
await productService.decreaseStock(item.productId, difference);
|
||||
await inventory.decreaseStock(item.productId, difference);
|
||||
} else if (difference < 0) {
|
||||
await productService.increaseStock(item.productId, -difference);
|
||||
await inventory.increaseStock(item.productId, -difference);
|
||||
}
|
||||
|
||||
await _reloadTabs();
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,15 @@ import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../models/product.dart';
|
||||
import '../services/product_service.dart';
|
||||
import 'inventory_view_model.dart';
|
||||
|
||||
class ProductListViewModel extends ChangeNotifier {
|
||||
final ProductService productService;
|
||||
final InventoryViewModel inventory;
|
||||
|
||||
ProductListViewModel({
|
||||
required this.productService,
|
||||
required this.inventory,
|
||||
});
|
||||
|
||||
static const List<String> _defaultCategories = [
|
||||
@@ -21,12 +24,11 @@ class ProductListViewModel extends ChangeNotifier {
|
||||
'Other',
|
||||
];
|
||||
|
||||
List<Product> _products = [];
|
||||
bool _isLoading = false;
|
||||
bool _hasLoaded = false;
|
||||
String? _errorMessage;
|
||||
|
||||
List<Product> get products => _products;
|
||||
List<Product> get products => inventory.products;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
@@ -48,7 +50,7 @@ class ProductListViewModel extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
_products = await productService.getProducts();
|
||||
await inventory.load();
|
||||
} catch (_) {
|
||||
_errorMessage = 'Could not load products.';
|
||||
} finally {
|
||||
@@ -109,7 +111,7 @@ class ProductListViewModel extends ChangeNotifier {
|
||||
List<String> get categories {
|
||||
final categories = {
|
||||
..._defaultCategories,
|
||||
..._products
|
||||
...inventory.products
|
||||
.map((p) => p.category.trim())
|
||||
.where((c) => c.isNotEmpty),
|
||||
}.toList();
|
||||
|
||||
Reference in New Issue
Block a user