Fixed compilation

This commit is contained in:
Stephan D
2026-01-22 14:15:14 +01:00
parent 8456263dd8
commit 32e8376700
41 changed files with 549 additions and 190 deletions

View File

@@ -0,0 +1,129 @@
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:pshared/models/payment/wallet.dart';
import 'package:pshared/provider/payment/wallets.dart';
class WalletsController with ChangeNotifier {
late WalletsProvider _wallets;
// If you want per-org isolation, we reset when org changes.
String? _orgRef;
// Visibility is UI-only: store hidden wallet ids here.
final Set<String> _hiddenWalletIds = <String>{};
String? _selectedWalletId;
bool get isLoading => _wallets.isLoading;
Exception? get error => _wallets.error;
/// Inject / update dependency (use ProxyProvider).
void update(WalletsProvider wallets) {
_wallets = wallets;
final nextOrgRef = wallets.organizationId;
final orgChanged = nextOrgRef != _orgRef;
if (orgChanged) {
_orgRef = nextOrgRef;
_hiddenWalletIds.clear();
_selectedWalletId = null;
}
// Prune hidden ids for wallets that no longer exist.
final ids = wallets.wallets.map((w) => w.id).toSet();
final beforeHiddenLen = _hiddenWalletIds.length;
_hiddenWalletIds.removeWhere((id) => !ids.contains(id));
// Ensure selection is valid.
final beforeSelected = _selectedWalletId;
_selectedWalletId = _resolveSelectedId(
currentId: _selectedWalletId,
wallets: wallets.wallets,
hiddenIds: _hiddenWalletIds,
);
final selectionChanged = beforeSelected != _selectedWalletId;
final hiddenChanged = beforeHiddenLen != _hiddenWalletIds.length;
if (orgChanged || selectionChanged || hiddenChanged) {
notifyListeners();
}
}
List<Wallet> get wallets => _wallets.wallets;
bool isHidden(String walletId) => _hiddenWalletIds.contains(walletId);
List<Wallet> get visibleWallets =>
wallets.where((w) => !_hiddenWalletIds.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;
// Allow selecting hidden wallet if you want; if not, block it:
// if (isHidden(walletId)) return;
_selectedWalletId = walletId;
notifyListeners();
}
void toggleVisibility(String walletId) {
final existed = _hiddenWalletIds.remove(walletId);
if (!existed) _hiddenWalletIds.add(walletId);
// If we hid the selected wallet, move selection to next visible.
_selectedWalletId = _resolveSelectedId(
currentId: _selectedWalletId,
wallets: wallets,
hiddenIds: _hiddenWalletIds,
);
notifyListeners();
}
void showAll() {
if (_hiddenWalletIds.isEmpty) return;
_hiddenWalletIds.clear();
_selectedWalletId = _resolveSelectedId(
currentId: _selectedWalletId,
wallets: wallets,
hiddenIds: _hiddenWalletIds,
);
notifyListeners();
}
String? _resolveSelectedId({
required String? currentId,
required List<Wallet> wallets,
required Set<String> hiddenIds,
}) {
if (wallets.isEmpty) return null;
// Keep current if exists and is not hidden.
if (currentId != null) {
final exists = wallets.any((w) => w.id == currentId);
if (exists && !hiddenIds.contains(currentId)) return currentId;
}
// Pick first visible.
final firstVisible = wallets.firstWhereOrNull((w) => !hiddenIds.contains(w.id));
if (firstVisible != null) return firstVisible.id;
// All hidden: fall back to first wallet id (or return null if you prefer).
return wallets.first.id;
}
}

View File

@@ -0,0 +1,47 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:pshared/data/dto/describable.dart';
import 'package:pshared/data/dto/ledger/balance.dart';
part 'account.g.dart';
@JsonSerializable(includeIfNull: false, explicitToJson: true)
class LedgerAccountDTO {
final String ledgerAccountRef;
final String organizationRef;
final String? ownerRef;
final String accountCode;
final String accountType;
final String currency;
final String status;
final bool allowNegative;
final bool isSettlement;
final Map<String, String>? metadata;
final DateTime? createdAt;
final DateTime? updatedAt;
final DescribableDTO describable;
final LedgerBalanceDTO? balance;
const LedgerAccountDTO({
required this.ledgerAccountRef,
required this.organizationRef,
this.ownerRef,
required this.accountCode,
required this.accountType,
required this.currency,
required this.status,
required this.allowNegative,
required this.isSettlement,
this.metadata,
this.createdAt,
this.updatedAt,
required this.describable,
this.balance,
});
factory LedgerAccountDTO.fromJson(Map<String, dynamic> json) => _$LedgerAccountDTOFromJson(json);
Map<String, dynamic> toJson() => _$LedgerAccountDTOToJson(this);
}

View File

@@ -0,0 +1,27 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:pshared/data/dto/money.dart';
part 'balance.g.dart';
@JsonSerializable(
includeIfNull: false, // <- omitempty behavior
explicitToJson: true, // <- nested DTOs call toJson()
)
class LedgerBalanceDTO {
final String ledgerAccountRef;
final MoneyDTO? balance;
final int version;
final DateTime? lastUpdated;
const LedgerBalanceDTO({
required this.ledgerAccountRef,
this.balance,
required this.version,
this.lastUpdated,
});
factory LedgerBalanceDTO.fromJson(Map<String, dynamic> json) => _$LedgerBalanceDTOFromJson(json);
Map<String, dynamic> toJson() => _$LedgerBalanceDTOToJson(this);
}

View File

@@ -1,6 +1,6 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:pshared/data/dto/payment/money.dart';
import 'package:pshared/data/dto/money.dart';
part 'fee_line.g.dart';

View File

@@ -1,6 +1,6 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:pshared/data/dto/payment/money.dart';
import 'package:pshared/data/dto/money.dart';
part 'fx_quote.g.dart';

View File

@@ -3,7 +3,7 @@ import 'package:json_annotation/json_annotation.dart';
import 'package:pshared/data/dto/payment/endpoint.dart';
import 'package:pshared/data/dto/payment/intent/customer.dart';
import 'package:pshared/data/dto/payment/intent/fx.dart';
import 'package:pshared/data/dto/payment/money.dart';
import 'package:pshared/data/dto/money.dart';
part 'payment.g.dart';

View File

@@ -1,6 +1,6 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:pshared/data/dto/payment/money.dart';
import 'package:pshared/data/dto/money.dart';
part 'network_fee.g.dart';

View File

@@ -2,7 +2,7 @@ import 'package:json_annotation/json_annotation.dart';
import 'package:pshared/data/dto/payment/fee_line.dart';
import 'package:pshared/data/dto/payment/fx_quote.dart';
import 'package:pshared/data/dto/payment/money.dart';
import 'package:pshared/data/dto/money.dart';
import 'package:pshared/data/dto/payment/network_fee.dart';
part 'payment_quote.g.dart';

View File

@@ -1,6 +1,6 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:pshared/data/dto/payment/money.dart';
import 'package:pshared/data/dto/money.dart';
part 'quote_aggregate.g.dart';

View File

@@ -0,0 +1,43 @@
import 'package:pshared/data/dto/ledger/account.dart';
import 'package:pshared/data/mapper/describable.dart';
import 'package:pshared/data/mapper/ledger/balance.dart';
import 'package:pshared/models/ledger/account.dart';
extension LedgerAccountDtoMapper on LedgerAccountDTO {
LedgerAccount toModel() => LedgerAccount(
ledgerAccountRef: ledgerAccountRef,
organizationRef: organizationRef,
ownerRef: ownerRef,
accountCode: accountCode,
accountType: accountType,
currency: currency,
status: status,
allowNegative: allowNegative,
isSettlement: isSettlement,
metadata: metadata,
createdAt: createdAt,
updatedAt: updatedAt,
describable: describable.toDomain(),
balance: balance?.toDomain(),
);
}
extension LedgerAccountModelMapper on LedgerAccount {
LedgerAccountDTO toDTO() => LedgerAccountDTO(
ledgerAccountRef: ledgerAccountRef,
organizationRef: organizationRef,
ownerRef: ownerRef,
accountCode: accountCode,
accountType: accountType,
currency: currency,
status: status,
allowNegative: allowNegative,
isSettlement: isSettlement,
metadata: metadata,
createdAt: createdAt,
updatedAt: updatedAt,
describable: describable.toDTO(),
balance: balance?.toDTO(),
);
}

View File

@@ -0,0 +1,22 @@
import 'package:pshared/data/dto/ledger/balance.dart';
import 'package:pshared/data/mapper/money.dart';
import 'package:pshared/models/ledger/balance.dart';
extension LedgerBalanceDtoMapper on LedgerBalanceDTO {
LedgerBalance toDomain() => LedgerBalance(
ledgerAccountRef: ledgerAccountRef,
balance: balance?.toDomain(),
version: version,
lastUpdated: lastUpdated,
);
}
extension LedgerBalanceModelMapper on LedgerBalance {
LedgerBalanceDTO toDTO() => LedgerBalanceDTO(
ledgerAccountRef: ledgerAccountRef,
balance: balance?.toDTO(),
version: version,
lastUpdated: lastUpdated,
);
}

View File

@@ -1,4 +1,4 @@
import 'package:pshared/data/dto/payment/money.dart';
import 'package:pshared/data/dto/money.dart';
import 'package:pshared/models/money.dart';

View File

@@ -1,5 +1,5 @@
import 'package:pshared/data/dto/payment/fee_line.dart';
import 'package:pshared/data/mapper/payment/money.dart';
import 'package:pshared/data/mapper/money.dart';
import 'package:pshared/models/payment/fees/line.dart';

View File

@@ -1,5 +1,5 @@
import 'package:pshared/data/dto/payment/fx_quote.dart';
import 'package:pshared/data/mapper/payment/money.dart';
import 'package:pshared/data/mapper/money.dart';
import 'package:pshared/models/payment/fx/quote.dart';

View File

@@ -3,7 +3,7 @@ import 'package:pshared/data/mapper/payment/payment.dart';
import 'package:pshared/data/mapper/payment/enums.dart';
import 'package:pshared/data/mapper/payment/intent/customer.dart';
import 'package:pshared/data/mapper/payment/intent/fx.dart';
import 'package:pshared/data/mapper/payment/money.dart';
import 'package:pshared/data/mapper/money.dart';
import 'package:pshared/models/payment/intent.dart';

View File

@@ -1,5 +1,5 @@
import 'package:pshared/data/dto/payment/network_fee.dart';
import 'package:pshared/data/mapper/payment/money.dart';
import 'package:pshared/data/mapper/money.dart';
import 'package:pshared/models/payment/fees/network.dart';

View File

@@ -1,7 +1,7 @@
import 'package:pshared/data/dto/payment/payment_quote.dart';
import 'package:pshared/data/mapper/payment/fee_line.dart';
import 'package:pshared/data/mapper/payment/fx_quote.dart';
import 'package:pshared/data/mapper/payment/money.dart';
import 'package:pshared/data/mapper/money.dart';
import 'package:pshared/data/mapper/payment/network_fee.dart';
import 'package:pshared/models/payment/quote/quote.dart';

View File

@@ -1,5 +1,5 @@
import 'package:pshared/data/dto/payment/quote_aggregate.dart';
import 'package:pshared/data/mapper/payment/money.dart';
import 'package:pshared/data/mapper/money.dart';
import 'package:pshared/models/payment/quote/aggregate.dart';

View File

@@ -9,7 +9,6 @@ extension WalletUiMapper on domain.WalletModel {
walletUserID: walletRef,
balance: double.tryParse(availableMoney?.amount ?? balance?.available?.amount ?? '0') ?? 0,
currency: currencyStringToCode(asset.tokenSymbol),
isHidden: true,
calculatedAt: balance?.calculatedAt ?? DateTime.now(),
depositAddress: depositAddress,
network: asset.chain,

View File

@@ -1,4 +1,5 @@
import 'package:pshared/models/describable.dart';
import 'package:pshared/models/ledger/balance.dart';
class LedgerAccount implements Describable {
@@ -15,6 +16,7 @@ class LedgerAccount implements Describable {
final DateTime? createdAt;
final DateTime? updatedAt;
final Describable describable;
final LedgerBalance? balance;
@override
String get name => describable.name;
@@ -36,6 +38,7 @@ class LedgerAccount implements Describable {
this.createdAt,
this.updatedAt,
required this.describable,
this.balance,
});
LedgerAccount copyWith({
@@ -54,5 +57,6 @@ class LedgerAccount implements Describable {
createdAt: createdAt,
updatedAt: updatedAt,
describable: describable ?? this.describable,
balance: balance,
);
}

View File

@@ -0,0 +1,16 @@
import 'package:pshared/models/money.dart';
class LedgerBalance {
final String ledgerAccountRef;
final Money? balance;
final int version;
final DateTime? lastUpdated;
const LedgerBalance({
required this.ledgerAccountRef,
this.balance,
required this.version,
this.lastUpdated,
});
}

View File

@@ -8,7 +8,6 @@ class Wallet implements Describable {
final String walletUserID; // ID or number that we show the user
final double balance;
final Currency currency;
final bool isHidden;
final DateTime calculatedAt;
final String? depositAddress;
final ChainNetwork? network;
@@ -29,7 +28,6 @@ class Wallet implements Describable {
required this.currency,
required this.calculatedAt,
required this.describable,
this.isHidden = true,
this.depositAddress,
this.network,
this.tokenSymbol,
@@ -37,32 +35,18 @@ class Wallet implements Describable {
});
Wallet copyWith({
String? id,
double? balance,
Currency? currency,
String? walletUserID,
bool? isHidden,
String? depositAddress,
ChainNetwork? network,
String? tokenSymbol,
String? contractAddress,
Describable? describable,
String? name,
String? Function()? description,
double? balance,
}) => Wallet(
id: id ?? this.id,
id: id,
balance: balance ?? this.balance,
currency: currency ?? this.currency,
walletUserID: walletUserID ?? this.walletUserID,
isHidden: isHidden ?? this.isHidden,
currency: currency,
walletUserID: walletUserID,
calculatedAt: calculatedAt,
depositAddress: depositAddress ?? this.depositAddress,
network: network ?? this.network,
tokenSymbol: tokenSymbol ?? this.tokenSymbol,
contractAddress: contractAddress ?? this.contractAddress,
describable: describable
?? (name != null || description != null
? this.describable.copyWith(name: name, description: description)
: this.describable),
depositAddress: depositAddress,
network: network,
tokenSymbol: tokenSymbol,
contractAddress: contractAddress,
describable: describable ?? this.describable,
);
}

View File

@@ -1,5 +1,6 @@
import 'package:collection/collection.dart';
import 'package:pshared/controllers/wallets.dart';
import 'package:pshared/models/payment/currency_pair.dart';
import 'package:pshared/models/payment/customer.dart';
import 'package:pshared/models/payment/fx/intent.dart';
@@ -13,7 +14,6 @@ import 'package:pshared/models/payment/intent.dart';
import 'package:pshared/models/recipient/recipient.dart';
import 'package:pshared/provider/payment/amount.dart';
import 'package:pshared/provider/payment/flow.dart';
import 'package:pshared/provider/payment/wallets.dart';
import 'package:pshared/provider/recipient/provider.dart';
import 'package:pshared/provider/recipient/pmethods.dart';
import 'package:pshared/utils/currency.dart';
@@ -22,7 +22,7 @@ import 'package:pshared/utils/currency.dart';
class QuotationIntentBuilder {
PaymentIntent? build({
required PaymentAmountProvider payment,
required WalletsProvider wallets,
required WalletsController wallets,
required PaymentFlowProvider flow,
required RecipientsProvider recipients,
required PaymentMethodsProvider methods,

View File

@@ -1,11 +1,13 @@
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'dart:convert';
import 'package:uuid/uuid.dart';
import 'package:pshared/api/requests/payment/quote.dart';
import 'package:pshared/controllers/wallets.dart';
import 'package:pshared/data/mapper/payment/intent/payment.dart';
import 'package:pshared/models/asset.dart';
import 'package:pshared/models/payment/intent.dart';
@@ -14,7 +16,6 @@ import 'package:pshared/models/money.dart';
import 'package:pshared/provider/organizations.dart';
import 'package:pshared/provider/payment/amount.dart';
import 'package:pshared/provider/payment/flow.dart';
import 'package:pshared/provider/payment/wallets.dart';
import 'package:pshared/provider/recipient/provider.dart';
import 'package:pshared/provider/recipient/pmethods.dart';
import 'package:pshared/provider/resource.dart';
@@ -34,7 +35,7 @@ class QuotationProvider extends ChangeNotifier {
void update(
OrganizationsProvider venue,
PaymentAmountProvider payment,
WalletsProvider wallets,
WalletsController wallets,
PaymentFlowProvider flow,
RecipientsProvider recipients,
PaymentMethodsProvider methods,

View File

@@ -1,5 +1,9 @@
import 'dart:async';
import 'dart:math';
import 'package:flutter/foundation.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:pshared/models/payment/wallet.dart';
import 'package:pshared/provider/organizations.dart';
@@ -8,10 +12,9 @@ import 'package:pshared/service/payment/wallets.dart';
import 'package:pshared/utils/exception.dart';
class WalletsProvider with ChangeNotifier {
final WalletsService _service;
late OrganizationsProvider _organizations;
OrganizationsProvider? _organizations;
WalletsProvider(this._service);
@@ -22,126 +25,204 @@ class WalletsProvider with ChangeNotifier {
bool get isLoading => _resource.isLoading;
Exception? get error => _resource.error;
Wallet? _selectedWallet;
Wallet? get selectedWallet => _selectedWallet;
final bool _isHidden = true;
bool get isHidden => _isHidden;
bool _isRefreshingBalances = false;
bool get isRefreshingBalances => _isRefreshingBalances;
final Set<String> _refreshingWallets = <String>{};
bool isWalletRefreshing(String walletId) => _refreshingWallets.contains(walletId);
// Expose current org id so UI controller can reset per-org state if needed.
String? get organizationId =>
(_organizations?.isOrganizationSet ?? false) ? _organizations!.current.id : null;
// Used to ignore stale async results (org changes / overlapping requests).
int _opSeq = 0;
// Per-wallet refresh sequence guard.
final Map<String, int> _walletSeq = <String, int>{};
// Keep modest concurrency to avoid hammering the backend.
static const int _balanceConcurrency = 6;
void update(OrganizationsProvider organizations) {
_organizations = organizations;
if (_organizations.isOrganizationSet) loadWalletsWithBalances();
if (organizations.isOrganizationSet) {
unawaited(loadWalletsWithBalances());
}
}
Future<Wallet> updateWallet(Wallet newWallet) {
throw Exception('update wallet is not implemented');
}
void selectWallet(Wallet wallet) => _setSelectedWallet(wallet);
Future<void> loadWalletsWithBalances() async {
_setResource(_resource.copyWith(isLoading: true, error: null));
final org = _organizations;
if (org == null || !org.isOrganizationSet) return;
final orgId = org.current.id;
final seq = ++_opSeq;
_isRefreshingBalances = false;
_refreshingWallets.clear();
_applyResource(_resource.copyWith(isLoading: true, error: null), notify: true);
try {
final base = await _service.getWallets(_organizations.current.id);
final withBalances = <Wallet>[];
for (final wallet in base) {
try {
final balance = await _service.getBalance(_organizations.current.id, wallet.id);
withBalances.add(wallet.copyWith(balance: balance));
} catch (e) {
_setResource(_resource.copyWith(error: toException(e)));
withBalances.add(wallet);
}
}
_setResource(Resource(data: withBalances, isLoading: false, error: _resource.error));
final base = await _service.getWallets(orgId);
final result = await _withBalances(orgId, base);
if (seq != _opSeq) return;
_applyResource(
Resource<List<Wallet>>(
data: result.wallets,
isLoading: false,
error: result.error,
),
notify: true,
);
} catch (e) {
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
if (seq != _opSeq) return;
_applyResource(
_resource.copyWith(isLoading: false, error: toException(e)),
notify: true,
);
}
}
Future<void> refreshBalances() async {
final org = _organizations;
if (org == null || !org.isOrganizationSet) return;
if (wallets.isEmpty) return;
final orgId = org.current.id;
final seq = ++_opSeq;
_isRefreshingBalances = true;
_applyResource(_resource.copyWith(error: null), notify: false);
notifyListeners();
try {
final updated = <Wallet>[];
for (final wallet in wallets) {
final balance = await _service.getBalance(_organizations.current.id, wallet.id);
updated.add(wallet.copyWith(balance: balance));
}
_setResource(_resource.copyWith(data: updated));
final result = await _withBalances(orgId, wallets);
if (seq != _opSeq) return;
_applyResource(
_resource.copyWith(data: result.wallets, error: result.error),
notify: false,
);
} catch (e) {
_setResource(_resource.copyWith(error: toException(e)));
if (seq != _opSeq) return;
_applyResource(_resource.copyWith(error: toException(e)), notify: false);
} finally {
_isRefreshingBalances = false;
notifyListeners();
if (seq == _opSeq) {
_isRefreshingBalances = false;
notifyListeners();
}
}
}
Future<void> refreshBalance(String walletId) async {
final org = _organizations;
if (org == null || !org.isOrganizationSet) return;
if (_refreshingWallets.contains(walletId)) return;
final wallet = wallets.firstWhereOrNull((w) => w.id == walletId);
if (wallet == null) return;
final existing = wallets.firstWhereOrNull((w) => w.id == walletId);
if (existing == null) return;
final orgId = org.current.id;
final seq = (_walletSeq[walletId] ?? 0) + 1;
_walletSeq[walletId] = seq;
_refreshingWallets.add(walletId);
notifyListeners();
try {
final balance = await _service.getBalance(_organizations.current.id, walletId);
final updatedWallet = wallet.copyWith(balance: balance);
final next = List<Wallet>.from(wallets);
final idx = next.indexWhere((w) => w.id == walletId);
if (idx >= 0) {
next[idx] = updatedWallet;
_setResource(_resource.copyWith(data: next));
}
final balance = await _service.getBalance(orgId, walletId);
if ((_walletSeq[walletId] ?? 0) != seq) return;
final next = _replaceWallet(walletId, (w) => w.copyWith(balance: balance));
if (next == null) return;
_applyResource(_resource.copyWith(data: next), notify: false);
} catch (e) {
_setResource(_resource.copyWith(error: toException(e)));
if ((_walletSeq[walletId] ?? 0) != seq) return;
_applyResource(_resource.copyWith(error: toException(e)), notify: false);
} finally {
_refreshingWallets.remove(walletId);
notifyListeners();
if ((_walletSeq[walletId] ?? 0) == seq) {
_refreshingWallets.remove(walletId);
notifyListeners();
}
}
}
void toggleVisibility(String walletId) {
final index = wallets.indexWhere((w) => w.id == walletId);
if (index < 0) return;
final wallet = wallets[index];
final updated = wallet.copyWith(isHidden: !wallet.isHidden);
final next = List<Wallet>.from(wallets);
next[index] = updated;
_setResource(_resource.copyWith(data: next));
if (_selectedWallet?.id == walletId) {
_selectedWallet = updated;
}
}
// ---------- internals ----------
void _setResource(Resource<List<Wallet>> newResource) {
void _applyResource(Resource<List<Wallet>> newResource, {required bool notify}) {
_resource = newResource;
_selectedWallet = _resolveSelectedWallet(_selectedWallet, wallets);
notifyListeners();
if (notify) notifyListeners();
}
Wallet? _resolveSelectedWallet(Wallet? current, List<Wallet> available) {
if (available.isEmpty) return null;
final currentId = current?.id;
if (currentId != null) {
final existing = available.firstWhereOrNull((wallet) => wallet.id == currentId);
if (existing != null) return existing;
}
return available.firstWhereOrNull((wallet) => !wallet.isHidden) ?? available.first;
List<Wallet>? _replaceWallet(String walletId, Wallet Function(Wallet) updater) {
final idx = wallets.indexWhere((w) => w.id == walletId);
if (idx < 0) return null;
final next = List<Wallet>.from(wallets);
next[idx] = updater(next[idx]);
return next;
}
void _setSelectedWallet(Wallet wallet) {
if (_selectedWallet?.id == wallet.id && _selectedWallet?.isHidden == wallet.isHidden) {
return;
Future<_WalletLoadResult> _withBalances(String orgId, List<Wallet> base) async {
Exception? firstError;
final withBalances = await _mapConcurrent<Wallet, Wallet>(
base,
_balanceConcurrency,
(wallet) async {
try {
final balance = await _service.getBalance(orgId, wallet.id);
return wallet.copyWith(balance: balance);
} catch (e) {
firstError ??= toException(e);
return wallet;
}
},
);
return _WalletLoadResult(withBalances, firstError);
}
static Future<List<R>> _mapConcurrent<T, R>(
List<T> items,
int concurrency,
Future<R> Function(T) fn,
) async {
if (items.isEmpty) return <R>[];
final results = List<R?>.filled(items.length, null);
var nextIndex = 0;
Future<void> worker() async {
while (true) {
final i = nextIndex++;
if (i >= items.length) return;
results[i] = await fn(items[i]);
}
}
_selectedWallet = wallet;
notifyListeners();
final workers = List.generate(min(concurrency, items.length), (_) => worker());
await Future.wait(workers);
return results.cast<R>();
}
}
class _WalletLoadResult {
final List<Wallet> wallets;
final Exception? error;
const _WalletLoadResult(this.wallets, this.error);
}