feat: add more detailed history and group per day
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../models/bar_tab.dart';
|
||||
import '../models/closed_tab.dart';
|
||||
import '../services/bar_tab_service.dart';
|
||||
|
||||
@@ -11,6 +12,7 @@ class HistoryViewModel extends ChangeNotifier {
|
||||
static const int _pageSize = 20;
|
||||
|
||||
List<ClosedTab> _closedTabs = [];
|
||||
List<BarTab> _openTabs = [];
|
||||
List<String> _customerNames = [];
|
||||
String? _selectedCustomer;
|
||||
int _offset = 0;
|
||||
@@ -22,14 +24,14 @@ class HistoryViewModel extends ChangeNotifier {
|
||||
String _searchQuery = '';
|
||||
|
||||
List<ClosedTab> get closedTabs {
|
||||
if (_searchQuery.isEmpty) return _closedTabs;
|
||||
|
||||
return _closedTabs
|
||||
.where(
|
||||
(tab) => tab.customerName.toLowerCase().contains(
|
||||
_searchQuery.toLowerCase(),
|
||||
),
|
||||
)
|
||||
.where((tab) => _matchesCustomer(tab.customerName))
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<BarTab> get openTabs {
|
||||
return _openTabs
|
||||
.where((tab) => _matchesCustomer(tab.customerName))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@@ -63,15 +65,19 @@ class HistoryViewModel extends ChangeNotifier {
|
||||
try {
|
||||
final results = await Future.wait([
|
||||
_fetchPage(offset: 0),
|
||||
barTabService.getClosedTabCount(
|
||||
customerName: _selectedCustomer,
|
||||
),
|
||||
barTabService.getClosedTabCount(customerName: _selectedCustomer),
|
||||
barTabService.getDistinctCustomerNames(),
|
||||
barTabService.getOpenTabs(includeRemoved: true),
|
||||
]);
|
||||
|
||||
_closedTabs = results[0] as List<ClosedTab>;
|
||||
_closedTabs = (results[0] as List<ClosedTab>).toList()
|
||||
..sort(_compareClosedTabs);
|
||||
_totalCount = results[1] as int;
|
||||
_customerNames = results[2] as List<String>;
|
||||
_openTabs = (results[3] as List<BarTab>).toList()..sort(_compareOpenTabs);
|
||||
_customerNames = {
|
||||
...(results[2] as List<String>),
|
||||
..._openTabs.map((tab) => tab.customerName),
|
||||
}.toList()..sort();
|
||||
_offset = _closedTabs.length;
|
||||
} catch (e) {
|
||||
debugPrint('HistoryViewModel: load error: $e');
|
||||
@@ -91,7 +97,7 @@ class HistoryViewModel extends ChangeNotifier {
|
||||
|
||||
try {
|
||||
final more = await _fetchPage(offset: _offset);
|
||||
_closedTabs = [..._closedTabs, ...more];
|
||||
_closedTabs = [..._closedTabs, ...more]..sort(_compareClosedTabs);
|
||||
_offset = _closedTabs.length;
|
||||
} catch (e) {
|
||||
debugPrint('HistoryViewModel: loadMore error: $e');
|
||||
@@ -110,11 +116,35 @@ class HistoryViewModel extends ChangeNotifier {
|
||||
);
|
||||
}
|
||||
|
||||
int _compareClosedTabs(ClosedTab a, ClosedTab b) {
|
||||
final byClosedAt = b.closedAt.compareTo(a.closedAt);
|
||||
if (byClosedAt != 0) return byClosedAt;
|
||||
|
||||
return b.id.compareTo(a.id);
|
||||
}
|
||||
|
||||
int _compareOpenTabs(BarTab a, BarTab b) {
|
||||
final byOpenedAt = b.openedAt.compareTo(a.openedAt);
|
||||
if (byOpenedAt != 0) return byOpenedAt;
|
||||
|
||||
return b.id.compareTo(a.id);
|
||||
}
|
||||
|
||||
void search(String query) {
|
||||
_searchQuery = query;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
bool _matchesCustomer(String customerName) {
|
||||
if (_selectedCustomer != null && customerName != _selectedCustomer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_searchQuery.isEmpty) return true;
|
||||
|
||||
return customerName.toLowerCase().contains(_searchQuery.toLowerCase());
|
||||
}
|
||||
|
||||
Future<void> filterByCustomer(String? customerName) async {
|
||||
_selectedCustomer = customerName;
|
||||
_searchQuery = '';
|
||||
@@ -123,4 +153,4 @@ class HistoryViewModel extends ChangeNotifier {
|
||||
|
||||
await load();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user