init
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
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';
|
||||
|
||||
class BarScreenViewModel extends ChangeNotifier {
|
||||
final BarTabService barTabService;
|
||||
|
||||
BarScreenViewModel({
|
||||
required this.barTabService,
|
||||
});
|
||||
|
||||
List<BarTab> _tabs = [];
|
||||
String? _selectedTabId;
|
||||
bool _isLoading = false;
|
||||
bool _hasLoaded = false;
|
||||
String? _errorMessage;
|
||||
|
||||
List<BarTab> get tabs => _tabs;
|
||||
|
||||
String? get selectedTabId => _selectedTabId;
|
||||
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
bool get hasLoaded => _hasLoaded;
|
||||
|
||||
String? get errorMessage => _errorMessage;
|
||||
|
||||
BarTab? get selectedTab {
|
||||
if (_selectedTabId == null) return null;
|
||||
|
||||
try {
|
||||
return _tabs.firstWhere((tab) => tab.id == _selectedTabId);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> ensureLoaded() async {
|
||||
if (_hasLoaded || _isLoading) return;
|
||||
|
||||
await load();
|
||||
}
|
||||
|
||||
Future<void> load() async {
|
||||
if (_isLoading) return;
|
||||
|
||||
_isLoading = true;
|
||||
_errorMessage = null;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
_tabs = await barTabService.getOpenTabs();
|
||||
|
||||
if (_selectedTabId == null ||
|
||||
!_tabs.any((tab) => tab.id == _selectedTabId)) {
|
||||
_selectedTabId = _tabs.isEmpty ? null : _tabs.first.id;
|
||||
}
|
||||
} catch (_) {
|
||||
_errorMessage = 'Could not load bar screen.';
|
||||
} finally {
|
||||
_hasLoaded = true;
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
void selectTab(String tabId) {
|
||||
_selectedTabId = tabId;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> createTab(String customerName) async {
|
||||
final tab = await barTabService.createTab(
|
||||
customerName: customerName,
|
||||
);
|
||||
|
||||
_selectedTabId = tab.id;
|
||||
await _reloadTabs();
|
||||
}
|
||||
|
||||
Future<void> addProductToSelectedTab(Product product) async {
|
||||
final tab = selectedTab;
|
||||
|
||||
if (tab == null) return;
|
||||
|
||||
await barTabService.addProductToTab(
|
||||
tabId: tab.id,
|
||||
product: product,
|
||||
);
|
||||
|
||||
await _reloadTabs();
|
||||
}
|
||||
|
||||
Future<void> changeItemQuantity(
|
||||
TabItem item,
|
||||
int quantity,
|
||||
) async {
|
||||
await barTabService.updateTabItemQuantity(
|
||||
tabItemId: item.id,
|
||||
quantity: quantity,
|
||||
);
|
||||
|
||||
await _reloadTabs();
|
||||
}
|
||||
|
||||
Future<void> closeSelectedTab() async {
|
||||
final tab = selectedTab;
|
||||
|
||||
if (tab == null) return;
|
||||
|
||||
await barTabService.closeTab(tab.id);
|
||||
|
||||
_selectedTabId = null;
|
||||
await _reloadTabs();
|
||||
}
|
||||
|
||||
Future<void> closeTab(String tabId) async {
|
||||
await barTabService.closeTab(tabId);
|
||||
|
||||
if (_selectedTabId == tabId) {
|
||||
_selectedTabId = null;
|
||||
}
|
||||
|
||||
await _reloadTabs();
|
||||
}
|
||||
|
||||
Future<void> _reloadTabs() async {
|
||||
_tabs = await barTabService.getOpenTabs();
|
||||
|
||||
if (_selectedTabId == null || !_tabs.any((tab) => tab.id == _selectedTabId)) {
|
||||
_selectedTabId = _tabs.isEmpty ? null : _tabs.first.id;
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user