Fixed compilation
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user