128 lines
3.3 KiB
Dart
128 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> _visibleWalletIds = <String>{};
|
|
|
|
String? _selectedWalletId;
|
|
|
|
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;
|
|
_visibleWalletIds.clear(); // All wallets hidden on org change
|
|
_selectedWalletId = null;
|
|
}
|
|
|
|
// Remove ids that no longer exist
|
|
final ids = wallets.wallets.map((w) => w.id).toSet();
|
|
_visibleWalletIds.removeWhere((id) => !ids.contains(id));
|
|
|
|
final beforeSelected = _selectedWalletId;
|
|
|
|
_selectedWalletId = _resolveSelectedId(
|
|
currentId: _selectedWalletId,
|
|
wallets: wallets.wallets,
|
|
visibleIds: _visibleWalletIds,
|
|
);
|
|
|
|
if (beforeSelected != _selectedWalletId || orgChanged) {
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
List<Wallet> get wallets => _wallets.wallets;
|
|
|
|
bool isVisible(String walletId) => _visibleWalletIds.contains(walletId);
|
|
bool isHidden(String walletId) => !isVisible(walletId);
|
|
|
|
List<Wallet> get visibleWallets =>
|
|
wallets.where((w) => _visibleWalletIds.contains(w.id)).toList(growable: false);
|
|
|
|
Wallet? get selectedWallet {
|
|
final id = _selectedWalletId;
|
|
if (id == null) return null;
|
|
return wallets.firstWhereOrNull((w) => w.id == id);
|
|
}
|
|
|
|
String? get selectedWalletId => _selectedWalletId;
|
|
|
|
void selectWallet(Wallet wallet) => selectWalletId(wallet.id);
|
|
|
|
void selectWalletId(String walletId) {
|
|
if (_selectedWalletId == walletId) return;
|
|
|
|
// Prevent selecting a hidden wallet
|
|
if (!_visibleWalletIds.contains(walletId)) return;
|
|
|
|
_selectedWalletId = walletId;
|
|
notifyListeners();
|
|
}
|
|
|
|
/// Toggle wallet visibility
|
|
void toggleVisibility(String walletId) {
|
|
final existed = _visibleWalletIds.remove(walletId);
|
|
if (!existed) _visibleWalletIds.add(walletId);
|
|
|
|
_selectedWalletId = _resolveSelectedId(
|
|
currentId: _selectedWalletId,
|
|
wallets: wallets,
|
|
visibleIds: _visibleWalletIds,
|
|
);
|
|
|
|
notifyListeners();
|
|
}
|
|
|
|
/// Show all wallets (bulk action)
|
|
void showAll() {
|
|
final allIds = wallets.map((w) => w.id);
|
|
_visibleWalletIds
|
|
..clear()
|
|
..addAll(allIds);
|
|
|
|
_selectedWalletId = _resolveSelectedId(
|
|
currentId: _selectedWalletId,
|
|
wallets: wallets,
|
|
visibleIds: _visibleWalletIds,
|
|
);
|
|
|
|
notifyListeners();
|
|
}
|
|
|
|
String? _resolveSelectedId({
|
|
required String? currentId,
|
|
required List<Wallet> wallets,
|
|
required Set<String> visibleIds,
|
|
}) {
|
|
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;
|
|
}
|
|
|
|
// Select the first visible wallet
|
|
final firstVisible = wallets.firstWhereOrNull((w) => visibleIds.contains(w.id));
|
|
return firstVisible?.id;
|
|
}
|
|
}
|