124 lines
3.3 KiB
Dart
124 lines
3.3 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.organizationId;
|
|
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) => selectWalletByRef(wallet.id);
|
|
|
|
void selectWalletByRef(String walletRef) {
|
|
if (_selectedWalletRef == walletRef) return;
|
|
|
|
// Prevent selecting a hidden wallet
|
|
if (!_visibleWalletRefs.contains(walletRef)) return;
|
|
|
|
_selectedWalletRef = walletRef;
|
|
notifyListeners();
|
|
}
|
|
|
|
/// Toggle wallet visibility
|
|
void toggleVisibility(String walletId) {
|
|
final existed = _visibleWalletRefs.remove(walletId);
|
|
if (!existed) _visibleWalletRefs.add(walletId);
|
|
|
|
_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;
|
|
}
|
|
}
|