import 'package:flutter/foundation.dart'; import '../models/closed_tab.dart'; import '../services/bar_tab_service.dart'; class HistoryViewModel extends ChangeNotifier { final BarTabService barTabService; HistoryViewModel({ required this.barTabService, }); List _closedTabs = []; bool _isLoading = false; bool _hasLoaded = false; String? _errorMessage; String _searchQuery = ''; List get closedTabs { if (_searchQuery.isEmpty) return _closedTabs; return _closedTabs .where((tab) => tab.customerName.toLowerCase().contains(_searchQuery.toLowerCase())) .toList(); } bool get isLoading => _isLoading; bool get hasLoaded => _hasLoaded; String? get errorMessage => _errorMessage; Future ensureLoaded() async { if (_hasLoaded || _isLoading) return; await load(); } Future load() async { if (_isLoading) return; _isLoading = true; _errorMessage = null; notifyListeners(); try { _closedTabs = await barTabService.getClosedTabs(); } catch (_) { _errorMessage = 'Could not load tab history.'; } finally { _hasLoaded = true; _isLoading = false; notifyListeners(); } } void search(String query) { _searchQuery = query; notifyListeners(); } }