feat: add equality operators, make history screen paginated, random performance updates
This commit is contained in:
@@ -88,9 +88,11 @@ class BarScreenViewModel extends ChangeNotifier {
|
||||
throw Exception('Product is out of stock.');
|
||||
}
|
||||
|
||||
await barTabService.addProductToTab(tabId: tab.id, product: product);
|
||||
|
||||
await inventory.decreaseStock(product.id, 1);
|
||||
await barTabService.addProductToTab(
|
||||
tabId: tab.id,
|
||||
product: product,
|
||||
stockAdjustment: () => inventory.decreaseStock(product.id, 1),
|
||||
);
|
||||
|
||||
await _reloadTabs();
|
||||
}
|
||||
@@ -101,14 +103,15 @@ class BarScreenViewModel extends ChangeNotifier {
|
||||
await barTabService.updateTabItemQuantity(
|
||||
tabItemId: item.id,
|
||||
quantity: quantity,
|
||||
stockAdjustment: () async {
|
||||
if (difference > 0) {
|
||||
await inventory.decreaseStock(item.productId, difference);
|
||||
} else if (difference < 0) {
|
||||
await inventory.increaseStock(item.productId, -difference);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
if (difference > 0) {
|
||||
await inventory.decreaseStock(item.productId, difference);
|
||||
} else if (difference < 0) {
|
||||
await inventory.increaseStock(item.productId, -difference);
|
||||
}
|
||||
|
||||
await _reloadTabs();
|
||||
}
|
||||
|
||||
|
||||
@@ -254,4 +254,73 @@ class DevMenuViewModel extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> generateMockOrders({int count = 100}) async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final products = await productService.getProducts();
|
||||
if (products.isEmpty) {
|
||||
_lastAction = 'No products available — seed demo data first';
|
||||
return;
|
||||
}
|
||||
|
||||
final uuid = const Uuid();
|
||||
final customerNames = [
|
||||
'Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank',
|
||||
'Grace', 'Hank', 'Ivy', 'Jack', 'Kate', 'Leo',
|
||||
'Mia', 'Noah', 'Olivia', 'Pete', 'Quinn', 'Rose',
|
||||
'Sam', 'Tina', 'Umar', 'Vera', 'Wes', 'Xena',
|
||||
'Yves', 'Zara',
|
||||
];
|
||||
|
||||
await database.transaction(() async {
|
||||
for (var i = 0; i < count; i++) {
|
||||
final customer = customerNames[i % customerNames.length];
|
||||
final closedTabId = uuid.v4();
|
||||
final itemCount = 1 + (i % 5);
|
||||
|
||||
final closedAt = DateTime.now().subtract(
|
||||
Duration(
|
||||
days: i % 90,
|
||||
hours: i % 24,
|
||||
minutes: i % 60,
|
||||
),
|
||||
);
|
||||
|
||||
await database.into(database.closedTabs).insert(
|
||||
ClosedTabsCompanion.insert(
|
||||
id: closedTabId,
|
||||
originalTabId: uuid.v4(),
|
||||
customerName: customer,
|
||||
closedAt: closedAt,
|
||||
),
|
||||
);
|
||||
|
||||
for (var j = 0; j < itemCount; j++) {
|
||||
final product = products[(i + j) % products.length];
|
||||
final quantity = 1 + ((i * 7 + j * 13) % 6);
|
||||
|
||||
await database.into(database.closedTabItems).insert(
|
||||
ClosedTabItemsCompanion.insert(
|
||||
id: uuid.v4(),
|
||||
closedTabId: closedTabId,
|
||||
productId: product.id,
|
||||
productName: product.name,
|
||||
quantity: quantity,
|
||||
unitPriceInCents: product.priceInCents,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
_lastAction = 'Generated $count mock orders across '
|
||||
'${customerNames.length} customers';
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,16 @@ class HistoryViewModel extends ChangeNotifier {
|
||||
|
||||
HistoryViewModel({required this.barTabService});
|
||||
|
||||
static const int _pageSize = 20;
|
||||
|
||||
List<ClosedTab> _closedTabs = [];
|
||||
List<String> _customerNames = [];
|
||||
String? _selectedCustomer;
|
||||
int _offset = 0;
|
||||
int _totalCount = 0;
|
||||
bool _isLoading = false;
|
||||
bool _hasLoaded = false;
|
||||
bool _isLoadingMore = false;
|
||||
String? _errorMessage;
|
||||
String _searchQuery = '';
|
||||
|
||||
@@ -26,10 +33,18 @@ class HistoryViewModel extends ChangeNotifier {
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<String> get customerNames => _customerNames;
|
||||
|
||||
String? get selectedCustomer => _selectedCustomer;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
bool get hasLoaded => _hasLoaded;
|
||||
|
||||
bool get isLoadingMore => _isLoadingMore;
|
||||
|
||||
bool get hasMore => _closedTabs.length < _totalCount;
|
||||
|
||||
String? get errorMessage => _errorMessage;
|
||||
|
||||
Future<void> ensureLoaded() async {
|
||||
@@ -46,7 +61,18 @@ class HistoryViewModel extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
_closedTabs = await barTabService.getClosedTabs();
|
||||
final results = await Future.wait([
|
||||
_fetchPage(offset: 0),
|
||||
barTabService.getClosedTabCount(
|
||||
customerName: _selectedCustomer,
|
||||
),
|
||||
barTabService.getDistinctCustomerNames(),
|
||||
]);
|
||||
|
||||
_closedTabs = results[0] as List<ClosedTab>;
|
||||
_totalCount = results[1] as int;
|
||||
_customerNames = results[2] as List<String>;
|
||||
_offset = _closedTabs.length;
|
||||
} catch (_) {
|
||||
_errorMessage = 'Could not load tab history.';
|
||||
} finally {
|
||||
@@ -56,8 +82,43 @@ class HistoryViewModel extends ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadMore() async {
|
||||
if (_isLoadingMore || !hasMore) return;
|
||||
|
||||
_isLoadingMore = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final more = await _fetchPage(offset: _offset);
|
||||
_closedTabs = [..._closedTabs, ...more];
|
||||
_offset = _closedTabs.length;
|
||||
} catch (_) {
|
||||
_errorMessage = 'Could not load more tabs.';
|
||||
} finally {
|
||||
_isLoadingMore = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<ClosedTab>> _fetchPage({required int offset}) {
|
||||
return barTabService.getClosedTabsPaginated(
|
||||
limit: _pageSize,
|
||||
offset: offset,
|
||||
customerName: _selectedCustomer,
|
||||
);
|
||||
}
|
||||
|
||||
void search(String query) {
|
||||
_searchQuery = query;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> filterByCustomer(String? customerName) async {
|
||||
_selectedCustomer = customerName;
|
||||
_searchQuery = '';
|
||||
_offset = 0;
|
||||
_closedTabs = [];
|
||||
|
||||
await load();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import 'package:ota_update/ota_update.dart';
|
||||
|
||||
import '../models/settings.dart';
|
||||
import '../services/settings_service.dart';
|
||||
import '../utils/app_config.dart';
|
||||
|
||||
class SettingsViewModel extends ChangeNotifier {
|
||||
final SettingsService settingsService;
|
||||
@@ -15,9 +16,7 @@ class SettingsViewModel extends ChangeNotifier {
|
||||
bool _hasLoaded = false;
|
||||
String? _errorMessage;
|
||||
|
||||
final AppUpdateUtil _updater = AppUpdateUtil(
|
||||
serverUrl: "https://updater.brammie15.dev",
|
||||
);
|
||||
final AppUpdateUtil _updater = AppUpdateUtil(serverUrl: kUpdateServerUrl);
|
||||
|
||||
bool _checkingForUpdates = false;
|
||||
|
||||
@@ -74,7 +73,7 @@ class SettingsViewModel extends ChangeNotifier {
|
||||
}) async {
|
||||
final url = update.download.startsWith("http")
|
||||
? update.download
|
||||
: "https://updater.brammie15.dev${update.download}";
|
||||
: "$kUpdateServerUrl${update.download}";
|
||||
|
||||
debugPrint("Download Url: ${url}");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user