Fixed wallets load

This commit is contained in:
Stephan D
2026-01-22 18:17:41 +01:00
parent f202acd8ab
commit 71753e09ba
9 changed files with 135 additions and 167 deletions

View File

@@ -12,9 +12,9 @@ class WalletsController with ChangeNotifier {
String? _orgRef;
/// UI-only: which wallets are allowed to be visible
final Set<String> _visibleWalletIds = <String>{};
final Set<String> _visibleWalletRefs = <String>{};
String? _selectedWalletId;
String? _selectedWalletRef;
bool get isLoading => _wallets.isLoading;
Exception? get error => _wallets.error;
@@ -27,64 +27,60 @@ class WalletsController with ChangeNotifier {
if (orgChanged) {
_orgRef = nextOrgRef;
_visibleWalletIds.clear(); // All wallets hidden on org change
_selectedWalletId = null;
_visibleWalletRefs.clear(); // All wallets hidden on org change
_selectedWalletRef = null;
}
// Remove ids that no longer exist
final ids = wallets.wallets.map((w) => w.id).toSet();
_visibleWalletIds.removeWhere((id) => !ids.contains(id));
_visibleWalletRefs.removeWhere((id) => !ids.contains(id));
final beforeSelected = _selectedWalletId;
_selectedWalletId = _resolveSelectedId(
currentId: _selectedWalletId,
_selectedWalletRef = _resolveSelectedId(
currentRef: _selectedWalletRef,
wallets: wallets.wallets,
visibleIds: _visibleWalletIds,
visibleRefs: _visibleWalletRefs,
);
if (beforeSelected != _selectedWalletId || orgChanged) {
notifyListeners();
}
notifyListeners();
}
List<Wallet> get wallets => _wallets.wallets;
bool isVisible(String walletId) => _visibleWalletIds.contains(walletId);
bool isHidden(String walletId) => !isVisible(walletId);
bool isVisible(String walletRef) => _visibleWalletRefs.contains(walletRef);
bool isHidden(String walletRef) => !isVisible(walletRef);
List<Wallet> get visibleWallets =>
wallets.where((w) => _visibleWalletIds.contains(w.id)).toList(growable: false);
wallets.where((w) => _visibleWalletRefs.contains(w.id)).toList(growable: false);
Wallet? get selectedWallet {
final id = _selectedWalletId;
final id = _selectedWalletRef;
if (id == null) return null;
return wallets.firstWhereOrNull((w) => w.id == id);
}
String? get selectedWalletId => _selectedWalletId;
String? get selectedWalletRef => _selectedWalletRef;
void selectWallet(Wallet wallet) => selectWalletId(wallet.id);
void selectWallet(Wallet wallet) => selectWalletByRef(wallet.id);
void selectWalletId(String walletId) {
if (_selectedWalletId == walletId) return;
void selectWalletByRef(String walletRef) {
if (_selectedWalletRef == walletRef) return;
// Prevent selecting a hidden wallet
if (!_visibleWalletIds.contains(walletId)) return;
if (!_visibleWalletRefs.contains(walletRef)) return;
_selectedWalletId = walletId;
_selectedWalletRef = walletRef;
notifyListeners();
}
/// Toggle wallet visibility
void toggleVisibility(String walletId) {
final existed = _visibleWalletIds.remove(walletId);
if (!existed) _visibleWalletIds.add(walletId);
final existed = _visibleWalletRefs.remove(walletId);
if (!existed) _visibleWalletRefs.add(walletId);
_selectedWalletId = _resolveSelectedId(
currentId: _selectedWalletId,
_selectedWalletRef = _resolveSelectedId(
currentRef: _selectedWalletRef,
wallets: wallets,
visibleIds: _visibleWalletIds,
visibleRefs: _visibleWalletRefs,
);
notifyListeners();
@@ -92,36 +88,36 @@ class WalletsController with ChangeNotifier {
/// Show all wallets (bulk action)
void showAll() {
final allIds = wallets.map((w) => w.id);
_visibleWalletIds
final allRefs = wallets.map((w) => w.id);
_visibleWalletRefs
..clear()
..addAll(allIds);
..addAll(allRefs);
_selectedWalletId = _resolveSelectedId(
currentId: _selectedWalletId,
_selectedWalletRef = _resolveSelectedId(
currentRef: _selectedWalletRef,
wallets: wallets,
visibleIds: _visibleWalletIds,
visibleRefs: _visibleWalletRefs,
);
notifyListeners();
}
String? _resolveSelectedId({
required String? currentId,
required String? currentRef,
required List<Wallet> wallets,
required Set<String> visibleIds,
required Set<String> visibleRefs,
}) {
if (wallets.isEmpty) return null;
// Keep current selection if it still exists and is visible
if (currentId != null &&
visibleIds.contains(currentId) &&
wallets.any((w) => w.id == currentId)) {
return currentId;
if (currentRef != null &&
visibleRefs.contains(currentRef) &&
wallets.any((w) => w.id == currentRef)) {
return currentRef;
}
// Select the first visible wallet
final firstVisible = wallets.firstWhereOrNull((w) => visibleIds.contains(w.id));
final firstVisible = wallets.firstWhereOrNull((w) => visibleRefs.contains(w.id));
return firstVisible?.id;
}
}