fixed ledger account listing + quotation listing
This commit is contained in:
106
frontend/pshared/lib/controllers/balance_mask/wallets.dart
Normal file
106
frontend/pshared/lib/controllers/balance_mask/wallets.dart
Normal file
@@ -0,0 +1,106 @@
|
||||
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: wallet refs with masked balances
|
||||
final Set<String> _maskedBalanceWalletRefs = <String>{};
|
||||
Set<String> _knownWalletRefs = <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;
|
||||
_maskedBalanceWalletRefs.clear();
|
||||
_knownWalletRefs = <String>{};
|
||||
_selectedWalletRef = null;
|
||||
}
|
||||
|
||||
// Remove ids that no longer exist
|
||||
final ids = wallets.wallets.map((w) => w.id).toSet();
|
||||
_maskedBalanceWalletRefs.removeWhere((id) => !ids.contains(id));
|
||||
|
||||
final newIds = ids.difference(_knownWalletRefs);
|
||||
if (newIds.isNotEmpty) {
|
||||
_maskedBalanceWalletRefs.addAll(newIds);
|
||||
}
|
||||
_knownWalletRefs = ids;
|
||||
|
||||
_selectedWalletRef = _resolveSelectedId(
|
||||
currentRef: _selectedWalletRef,
|
||||
wallets: wallets.wallets,
|
||||
);
|
||||
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
List<Wallet> get wallets => _wallets.wallets;
|
||||
|
||||
bool isBalanceMasked(String walletRef) => _maskedBalanceWalletRefs.contains(walletRef);
|
||||
bool isBalanceVisible(String walletRef) => !isBalanceMasked(walletRef);
|
||||
|
||||
List<Wallet> get unmaskedWallets =>
|
||||
wallets.where((w) => !_maskedBalanceWalletRefs.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) => selectWalletByRef(wallet.id);
|
||||
|
||||
void selectWalletByRef(String walletRef) {
|
||||
if (_selectedWalletRef == walletRef) return;
|
||||
|
||||
_selectedWalletRef = walletRef;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Toggle wallet balance masking
|
||||
void toggleBalanceMask(String walletRef) {
|
||||
final existed = _maskedBalanceWalletRefs.remove(walletRef);
|
||||
if (!existed) _maskedBalanceWalletRefs.add(walletRef);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Unmask balances for all wallets (bulk action)
|
||||
void unmaskAllBalances() {
|
||||
_maskedBalanceWalletRefs.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
String? _resolveSelectedId({
|
||||
required String? currentRef,
|
||||
required List<Wallet> wallets,
|
||||
}) {
|
||||
if (wallets.isEmpty) return null;
|
||||
|
||||
// Keep current selection if it still exists
|
||||
if (currentRef != null && wallets.any((w) => w.id == currentRef)) {
|
||||
return currentRef;
|
||||
}
|
||||
|
||||
// Fallback to the first wallet
|
||||
return wallets.first.id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user