Files
sendico/frontend/pshared/lib/controllers/wallets.dart
Stephan D 218c4e20b3
All checks were successful
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/discovery Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/gateway_mntx Pipeline was successful
ci/woodpecker/push/gateway_chain Pipeline was successful
ci/woodpecker/push/gateway_tgsettle Pipeline was successful
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/notification Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline was successful
accounts creation
2026-01-23 00:13:43 +01:00

125 lines
3.4 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:collection/collection.dart';
import 'package:pshared/models/payment/wallet.dart';
import 'package:pshared/provider/payment/wallets.dart';
class WalletsController with ChangeNotifier {
late WalletsProvider _wallets;
String? _orgRef;
/// UI-only: which wallets are allowed to be visible
final Set<String> _visibleWalletRefs = <String>{};
String? _selectedWalletRef;
bool get isLoading => _wallets.isLoading;
Exception? get error => _wallets.error;
void update(WalletsProvider wallets) {
_wallets = wallets;
final nextOrgRef = wallets.organizationRef;
final orgChanged = nextOrgRef != _orgRef;
if (orgChanged) {
_orgRef = nextOrgRef;
_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();
_visibleWalletRefs.removeWhere((id) => !ids.contains(id));
_selectedWalletRef = _resolveSelectedId(
currentRef: _selectedWalletRef,
wallets: wallets.wallets,
visibleRefs: _visibleWalletRefs,
);
notifyListeners();
}
List<Wallet> get wallets => _wallets.wallets;
bool isVisible(String walletRef) => _visibleWalletRefs.contains(walletRef);
bool isHidden(String walletRef) => !isVisible(walletRef);
List<Wallet> get visibleWallets =>
wallets.where((w) => _visibleWalletRefs.contains(w.id)).toList(growable: false);
Wallet? get selectedWallet {
final id = _selectedWalletRef;
if (id == null) return null;
return wallets.firstWhereOrNull((w) => w.id == id);
}
String? get selectedWalletRef => _selectedWalletRef;
void selectWallet(Wallet wallet, {bool allowHidden = false}) =>
selectWalletByRef(wallet.id, allowHidden: allowHidden);
void selectWalletByRef(String walletRef, {bool allowHidden = false}) {
if (_selectedWalletRef == walletRef) return;
// Prevent selecting a hidden wallet
if (!allowHidden && !_visibleWalletRefs.contains(walletRef)) return;
_selectedWalletRef = walletRef;
notifyListeners();
}
/// Toggle wallet visibility
void toggleVisibility(String accountRef) {
final existed = _visibleWalletRefs.remove(accountRef);
if (!existed) _visibleWalletRefs.add(accountRef);
_selectedWalletRef = _resolveSelectedId(
currentRef: _selectedWalletRef,
wallets: wallets,
visibleRefs: _visibleWalletRefs,
);
notifyListeners();
}
/// Show all wallets (bulk action)
void showAll() {
final allRefs = wallets.map((w) => w.id);
_visibleWalletRefs
..clear()
..addAll(allRefs);
_selectedWalletRef = _resolveSelectedId(
currentRef: _selectedWalletRef,
wallets: wallets,
visibleRefs: _visibleWalletRefs,
);
notifyListeners();
}
String? _resolveSelectedId({
required String? currentRef,
required List<Wallet> wallets,
required Set<String> visibleRefs,
}) {
if (wallets.isEmpty) return null;
// Keep current selection if it still exists and is visible
if (currentRef != null &&
visibleRefs.contains(currentRef) &&
wallets.any((w) => w.id == currentRef)) {
return currentRef;
}
// Select the first visible wallet
final firstVisible = wallets.firstWhereOrNull((w) => visibleRefs.contains(w.id));
return firstVisible?.id;
}
}