import 'package:flutter/material.dart'; import 'package:pweb/models/wallet.dart'; import 'package:pweb/services/wallets.dart'; class WalletsProvider with ChangeNotifier { final WalletsService _service; WalletsProvider(this._service); List? _wallets; bool _isLoading = false; String? _error; Wallet? _selectedWallet; final bool _isHidden = true; List? get wallets => _wallets; bool get isLoading => _isLoading; String? get error => _error; Wallet? get selectedWallet => _selectedWallet; bool get isHidden => _isHidden; void selectWallet(Wallet wallet) { _selectedWallet = wallet; notifyListeners(); } Future loadData() async { _isLoading = true; _error = null; notifyListeners(); try { _wallets = await _service.getWallets(); } catch (e) { _error = e.toString(); } finally { _isLoading = false; notifyListeners(); } } Future getWalletById(String walletId) async { _isLoading = true; _error = null; notifyListeners(); try { final wallet = await _service.getWallet(walletId); return wallet; } catch (e) { _error = e.toString(); return null; } finally { _isLoading = false; notifyListeners(); } } void updateName(String walletRef, String newName) { final index = _wallets?.indexWhere((w) => w.id == walletRef); if (index != null && index >= 0) { _wallets![index] = _wallets![index].copyWith(name: newName); notifyListeners(); } } void updateBalance(String walletRef, double newBalance) { final index = _wallets?.indexWhere((w) => w.id == walletRef); if (index != null && index >= 0) { _wallets![index] = _wallets![index].copyWith(balance: newBalance); notifyListeners(); } } Future updateWallet(Wallet wallet) async { try { await _service.updateWallet(); final index = _wallets?.indexWhere((w) => w.id == wallet.id); if (index != null && index >= 0) { _wallets![index] = wallet; notifyListeners(); } } catch (e) { _error = e.toString(); notifyListeners(); } } Future addWallet(Wallet wallet) async { try { final newWallet = await _service.createWallet(); // Pass the wallet parameter _wallets = [...?_wallets, ]; // Add the new wallet notifyListeners(); } catch (e) { _error = e.toString(); notifyListeners(); } } Future deleteWallet(String walletId) async { try { await _service.deleteWallet(); // Pass the walletId parameter _wallets?.removeWhere((w) => w.id == walletId); notifyListeners(); } catch (e) { _error = e.toString(); notifyListeners(); } } void toggleVisibility(String walletId) { final index = _wallets?.indexWhere((w) => w.id == walletId); if (index != null && index >= 0) { final wallet = _wallets![index]; _wallets![index] = wallet.copyWith(isHidden: !wallet.isHidden); if (_selectedWallet?.id == walletId) { _selectedWallet = _wallets![index]; } notifyListeners(); } } }