feat: add equality operators, make history screen paginated, random performance updates

This commit is contained in:
2026-07-29 18:10:46 +02:00
parent da5588cd1c
commit 031a618220
22 changed files with 702 additions and 79 deletions
+88 -11
View File
@@ -18,11 +18,13 @@ abstract class BarTabService {
Future<void> addProductToTab({
required String tabId,
required Product product,
Future<void> Function()? stockAdjustment,
});
Future<void> updateTabItemQuantity({
required String tabItemId,
required int quantity,
Future<void> Function()? stockAdjustment,
});
/// Archives the tab's current items into history and clears them.
@@ -30,6 +32,16 @@ abstract class BarTabService {
Future<void> closeTab(String tabId);
Future<List<ClosedTab>> getClosedTabs();
Future<List<ClosedTab>> getClosedTabsPaginated({
int limit = 20,
int offset = 0,
String? customerName,
});
Future<int> getClosedTabCount({String? customerName});
Future<List<String>> getDistinctCustomerNames();
}
class DriftBarTabService implements BarTabService {
@@ -144,6 +156,7 @@ class DriftBarTabService implements BarTabService {
Future<void> addProductToTab({
required String tabId,
required Product product,
Future<void> Function()? stockAdjustment,
}) async {
await database.transaction(() async {
final existingItemQuery = database.select(database.tabItems)
@@ -162,6 +175,10 @@ class DriftBarTabService implements BarTabService {
TabItemsCompanion(quantity: Value(existingItem.quantity + 1)),
);
if (stockAdjustment != null) {
await stockAdjustment();
}
return;
}
@@ -178,6 +195,10 @@ class DriftBarTabService implements BarTabService {
createdAt: DateTime.now(),
),
);
if (stockAdjustment != null) {
await stockAdjustment();
}
});
}
@@ -185,19 +206,31 @@ class DriftBarTabService implements BarTabService {
Future<void> updateTabItemQuantity({
required String tabItemId,
required int quantity,
Future<void> Function()? stockAdjustment,
}) async {
if (quantity <= 0) {
final deleteQuery = database.delete(database.tabItems)
await database.transaction(() async {
if (quantity <= 0) {
final deleteQuery = database.delete(database.tabItems)
..where((item) => item.id.equals(tabItemId));
await deleteQuery.go();
if (stockAdjustment != null) {
await stockAdjustment();
}
return;
}
final updateQuery = database.update(database.tabItems)
..where((item) => item.id.equals(tabItemId));
await deleteQuery.go();
return;
}
await updateQuery.write(TabItemsCompanion(quantity: Value(quantity)));
final updateQuery = database.update(database.tabItems)
..where((item) => item.id.equals(tabItemId));
await updateQuery.write(TabItemsCompanion(quantity: Value(quantity)));
if (stockAdjustment != null) {
await stockAdjustment();
}
});
}
@override
@@ -255,8 +288,26 @@ class DriftBarTabService implements BarTabService {
@override
Future<List<ClosedTab>> getClosedTabs() async {
final query = database.select(database.closedTabs)
..orderBy([(tab) => OrderingTerm.desc(tab.closedAt)]);
final count = await getClosedTabCount();
if (count == 0) return [];
return getClosedTabsPaginated(limit: count, offset: 0);
}
@override
Future<List<ClosedTab>> getClosedTabsPaginated({
int limit = 20,
int offset = 0,
String? customerName,
}) async {
var query = database.select(database.closedTabs)
..orderBy([(tab) => OrderingTerm.desc(tab.closedAt)])
..limit(limit, offset: offset);
if (customerName != null && customerName.isNotEmpty) {
query = query
..where((tab) => tab.customerName.equals(customerName));
}
final closedTabRows = await query.get();
@@ -281,4 +332,30 @@ class DriftBarTabService implements BarTabService {
return closedTabs;
}
@override
Future<int> getClosedTabCount({String? customerName}) async {
final countExpr = database.closedTabs.id.count();
var query = database.selectOnly(database.closedTabs)
..addColumns([countExpr]);
if (customerName != null && customerName.isNotEmpty) {
query = query
..where(database.closedTabs.customerName.equals(customerName));
}
final row = await query.getSingle();
return row.read(countExpr) ?? 0;
}
@override
Future<List<String>> getDistinctCustomerNames() async {
final rows = await database.select(database.closedTabs).get();
return rows
.map((row) => row.customerName)
.toSet()
.toList()
..sort();
}
}