front dev update
This commit is contained in:
181
frontend/pshared/lib/provider/payment/payments.dart
Normal file
181
frontend/pshared/lib/provider/payment/payments.dart
Normal file
@@ -0,0 +1,181 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'package:pshared/models/payment/payment.dart';
|
||||
import 'package:pshared/provider/organizations.dart';
|
||||
import 'package:pshared/provider/resource.dart';
|
||||
import 'package:pshared/service/payment/service.dart';
|
||||
import 'package:pshared/utils/exception.dart';
|
||||
|
||||
|
||||
class PaymentsProvider with ChangeNotifier {
|
||||
OrganizationsProvider? _organizations;
|
||||
String? _loadedOrganizationRef;
|
||||
|
||||
Resource<List<Payment>> _resource = Resource(data: []);
|
||||
bool _isLoaded = false;
|
||||
bool _isLoadingMore = false;
|
||||
String? _nextCursor;
|
||||
|
||||
int? _limit;
|
||||
String? _sourceRef;
|
||||
String? _destinationRef;
|
||||
List<String>? _states;
|
||||
|
||||
int _opSeq = 0;
|
||||
|
||||
Resource<List<Payment>> get resource => _resource;
|
||||
List<Payment> get payments => _resource.data ?? [];
|
||||
bool get isLoading => _resource.isLoading;
|
||||
Exception? get error => _resource.error;
|
||||
bool get isReady => _isLoaded && !_resource.isLoading && _resource.error == null;
|
||||
|
||||
bool get isLoadingMore => _isLoadingMore;
|
||||
String? get nextCursor => _nextCursor;
|
||||
bool get canLoadMore => _nextCursor != null && _nextCursor!.isNotEmpty;
|
||||
|
||||
void update(OrganizationsProvider organizations) {
|
||||
_organizations = organizations;
|
||||
if (!organizations.isOrganizationSet) {
|
||||
reset();
|
||||
return;
|
||||
}
|
||||
|
||||
final orgRef = organizations.current.id;
|
||||
if (_loadedOrganizationRef != orgRef) {
|
||||
_loadedOrganizationRef = orgRef;
|
||||
unawaited(refresh());
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refresh({
|
||||
int? limit,
|
||||
String? sourceRef,
|
||||
String? destinationRef,
|
||||
List<String>? states,
|
||||
}) async {
|
||||
final org = _organizations;
|
||||
if (org == null || !org.isOrganizationSet) return;
|
||||
|
||||
_limit = limit;
|
||||
_sourceRef = _normalize(sourceRef);
|
||||
_destinationRef = _normalize(destinationRef);
|
||||
_states = _normalizeStates(states);
|
||||
_nextCursor = null;
|
||||
_isLoadingMore = false;
|
||||
|
||||
final seq = ++_opSeq;
|
||||
|
||||
_applyResource(_resource.copyWith(isLoading: true, error: null), notify: true);
|
||||
|
||||
try {
|
||||
final page = await PaymentService.listPage(
|
||||
org.current.id,
|
||||
limit: _limit,
|
||||
cursor: null,
|
||||
sourceRef: _sourceRef,
|
||||
destinationRef: _destinationRef,
|
||||
states: _states,
|
||||
);
|
||||
|
||||
if (seq != _opSeq) return;
|
||||
|
||||
_isLoaded = true;
|
||||
_nextCursor = _normalize(page.nextCursor);
|
||||
_applyResource(
|
||||
Resource(data: page.items, isLoading: false, error: null),
|
||||
notify: true,
|
||||
);
|
||||
} catch (e) {
|
||||
if (seq != _opSeq) return;
|
||||
|
||||
_applyResource(
|
||||
_resource.copyWith(isLoading: false, error: toException(e)),
|
||||
notify: true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadMore() async {
|
||||
final org = _organizations;
|
||||
if (org == null || !org.isOrganizationSet) return;
|
||||
if (_isLoadingMore || _resource.isLoading) return;
|
||||
|
||||
final cursor = _normalize(_nextCursor);
|
||||
if (cursor == null) return;
|
||||
|
||||
final seq = _opSeq;
|
||||
|
||||
_isLoadingMore = true;
|
||||
_applyResource(_resource.copyWith(error: null), notify: false);
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
final page = await PaymentService.listPage(
|
||||
org.current.id,
|
||||
limit: _limit,
|
||||
cursor: cursor,
|
||||
sourceRef: _sourceRef,
|
||||
destinationRef: _destinationRef,
|
||||
states: _states,
|
||||
);
|
||||
|
||||
if (seq != _opSeq) return;
|
||||
|
||||
final combined = List<Payment>.from(payments)..addAll(page.items);
|
||||
_nextCursor = _normalize(page.nextCursor);
|
||||
|
||||
_applyResource(
|
||||
_resource.copyWith(data: combined, error: null),
|
||||
notify: false,
|
||||
);
|
||||
} catch (e) {
|
||||
if (seq != _opSeq) return;
|
||||
|
||||
_applyResource(
|
||||
_resource.copyWith(error: toException(e)),
|
||||
notify: false,
|
||||
);
|
||||
} finally {
|
||||
if (seq == _opSeq) {
|
||||
_isLoadingMore = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void reset() {
|
||||
_opSeq++;
|
||||
_isLoaded = false;
|
||||
_isLoadingMore = false;
|
||||
_nextCursor = null;
|
||||
_limit = null;
|
||||
_sourceRef = null;
|
||||
_destinationRef = null;
|
||||
_states = null;
|
||||
_resource = Resource(data: []);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void _applyResource(Resource<List<Payment>> newResource, {required bool notify}) {
|
||||
_resource = newResource;
|
||||
if (notify) notifyListeners();
|
||||
}
|
||||
|
||||
String? _normalize(String? value) {
|
||||
final trimmed = value?.trim();
|
||||
if (trimmed == null || trimmed.isEmpty) return null;
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
List<String>? _normalizeStates(List<String>? states) {
|
||||
if (states == null || states.isEmpty) return null;
|
||||
final normalized = states
|
||||
.map((state) => state.trim())
|
||||
.where((state) => state.isNotEmpty)
|
||||
.toList();
|
||||
if (normalized.isEmpty) return null;
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import 'package:pshared/provider/organizations.dart';
|
||||
import 'package:pshared/provider/payment/quotation/quotation.dart';
|
||||
import 'package:pshared/provider/resource.dart';
|
||||
import 'package:pshared/service/payment/service.dart';
|
||||
import 'package:pshared/utils/exception.dart';
|
||||
|
||||
|
||||
class PaymentProvider extends ChangeNotifier {
|
||||
@@ -53,11 +54,7 @@ class PaymentProvider extends ChangeNotifier {
|
||||
_isLoaded = true;
|
||||
_setResource(_payment.copyWith(data: response, isLoading: false, error: null));
|
||||
} catch (e) {
|
||||
_setResource(_payment.copyWith(
|
||||
data: null,
|
||||
error: e is Exception ? e : Exception(e.toString()),
|
||||
isLoading: false,
|
||||
));
|
||||
_setResource(_payment.copyWith(data: null, error: toException(e), isLoading: false));
|
||||
}
|
||||
return _payment.data;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import 'package:pshared/controllers/balance_mask/wallets.dart';
|
||||
import 'package:pshared/models/payment/asset.dart';
|
||||
import 'package:pshared/models/payment/chain_network.dart';
|
||||
import 'package:pshared/models/payment/currency_pair.dart';
|
||||
import 'package:pshared/models/payment/customer.dart';
|
||||
import 'package:pshared/models/payment/fx/intent.dart';
|
||||
@@ -55,6 +57,10 @@ class QuotationIntentBuilder {
|
||||
destination: paymentData,
|
||||
source: ManagedWalletPaymentMethod(
|
||||
managedWalletRef: selectedWallet.id,
|
||||
asset: PaymentAsset(
|
||||
tokenSymbol: selectedWallet.tokenSymbol ?? '',
|
||||
chain: selectedWallet.network ?? ChainNetwork.unspecified,
|
||||
)
|
||||
),
|
||||
fx: fxIntent,
|
||||
settlementMode: payment.payerCoversFee ? SettlementMode.fixReceived : SettlementMode.fixSource,
|
||||
|
||||
Reference in New Issue
Block a user