fixes
This commit is contained in:
@@ -3,34 +3,35 @@ import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
import 'package:pshared/utils/localization.dart';
|
||||
import 'package:pweb/utils/money_display.dart';
|
||||
|
||||
|
||||
String formatMoney(Money? money, {String fallback = '-'}) {
|
||||
if (money == null) return fallback;
|
||||
final amount = money.amount.trim();
|
||||
if (amount.isEmpty) return fallback;
|
||||
final symbol = currencySymbolFromCode(money.currency);
|
||||
final suffix = symbol ?? money.currency;
|
||||
if (suffix.trim().isEmpty) return amount;
|
||||
return '$amount $suffix';
|
||||
String formatMoney(BuildContext context, Money? money) {
|
||||
if (money == null || money.amount.trim().isEmpty) {
|
||||
return unavailableMoneyValue(context);
|
||||
}
|
||||
return formatMoneyUi(context, money);
|
||||
}
|
||||
|
||||
String formatAmount(double amount, String currency, {String fallback = '-'}) {
|
||||
final trimmed = currency.trim();
|
||||
if (trimmed.isEmpty) return amountToString(amount);
|
||||
final symbol = currencySymbolFromCode(trimmed);
|
||||
final suffix = symbol ?? trimmed;
|
||||
return '${amountToString(amount)} $suffix';
|
||||
String formatAmount(BuildContext context, double amount, String currency) {
|
||||
return formatAmountUi(context, amount: amount, currency: currency);
|
||||
}
|
||||
|
||||
String formatDateLabel(BuildContext context, DateTime? date, {String fallback = '-'}) {
|
||||
String formatDateLabel(
|
||||
BuildContext context,
|
||||
DateTime? date, {
|
||||
String fallback = '-',
|
||||
}) {
|
||||
if (date == null || date.millisecondsSinceEpoch == 0) return fallback;
|
||||
return dateTimeToLocalFormat(context, date.toLocal());
|
||||
}
|
||||
|
||||
String formatLongDate(BuildContext context, DateTime? date, {String fallback = '-'}) {
|
||||
String formatLongDate(
|
||||
BuildContext context,
|
||||
DateTime? date, {
|
||||
String fallback = '-',
|
||||
}) {
|
||||
if (date == null || date.millisecondsSinceEpoch == 0) return fallback;
|
||||
final locale = Localizations.localeOf(context).toString();
|
||||
final formatter = DateFormat('d MMMM y', locale);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'package:pshared/models/payment/intent.dart';
|
||||
import 'package:pshared/models/payment/endpoint.dart';
|
||||
import 'package:pshared/models/payment/methods/data.dart';
|
||||
import 'package:pshared/models/payment/methods/ledger.dart';
|
||||
import 'package:pshared/models/payment/methods/managed_wallet.dart';
|
||||
@@ -7,6 +7,13 @@ import 'package:pshared/models/payment/payment.dart';
|
||||
import 'package:pshared/models/payment/source_type.dart';
|
||||
|
||||
|
||||
typedef _SourceRefExtractor = String? Function(PaymentMethodData source);
|
||||
|
||||
final Map<PaymentSourceType, _SourceRefExtractor> _sourceExtractors = {
|
||||
PaymentSourceType.wallet: _walletSourceRef,
|
||||
PaymentSourceType.ledger: _ledgerSourceRef,
|
||||
};
|
||||
|
||||
bool paymentMatchesSource(
|
||||
Payment payment, {
|
||||
required PaymentSourceType sourceType,
|
||||
@@ -15,95 +22,54 @@ bool paymentMatchesSource(
|
||||
final normalizedSourceRef = _normalize(sourceRef);
|
||||
if (normalizedSourceRef == null) return false;
|
||||
|
||||
final paymentSourceRef = _paymentSourceRef(payment, sourceType);
|
||||
return paymentSourceRef != null && paymentSourceRef == normalizedSourceRef;
|
||||
final paymentSourceRefs = _paymentSourceRefs(payment, sourceType);
|
||||
if (paymentSourceRefs.isEmpty) return false;
|
||||
|
||||
return paymentSourceRefs.contains(normalizedSourceRef);
|
||||
}
|
||||
|
||||
String? _paymentSourceRef(Payment payment, PaymentSourceType sourceType) {
|
||||
final fromIntent = _sourceRefFromIntent(payment.intent, sourceType);
|
||||
if (fromIntent != null) return fromIntent;
|
||||
return _sourceRefFromMetadata(payment.metadata, sourceType);
|
||||
Set<String> _paymentSourceRefs(Payment payment, PaymentSourceType sourceType) {
|
||||
final fromSource = _sourceRefsFromEndpoint(payment.source, sourceType);
|
||||
if (fromSource.isEmpty) return const <String>{};
|
||||
return fromSource;
|
||||
}
|
||||
|
||||
String? _sourceRefFromIntent(
|
||||
PaymentIntent? intent,
|
||||
Set<String> _sourceRefsFromEndpoint(
|
||||
PaymentEndpoint? endpoint,
|
||||
PaymentSourceType sourceType,
|
||||
) {
|
||||
final source = intent?.source;
|
||||
if (source == null) return null;
|
||||
if (endpoint == null) return const <String>{};
|
||||
|
||||
final fromIntentAttributes = _sourceRefFromMetadata(
|
||||
intent?.attributes,
|
||||
sourceType,
|
||||
);
|
||||
if (fromIntentAttributes != null) return fromIntentAttributes;
|
||||
|
||||
switch (sourceType) {
|
||||
case PaymentSourceType.wallet:
|
||||
return _walletSourceRef(source);
|
||||
case PaymentSourceType.ledger:
|
||||
return _ledgerSourceRef(source);
|
||||
final refs = <String>{};
|
||||
void collect(String? value) {
|
||||
final normalized = _normalize(value);
|
||||
if (normalized == null) return;
|
||||
refs.add(normalized);
|
||||
}
|
||||
|
||||
final source = endpoint.method;
|
||||
if (source != null) {
|
||||
final fromMethod = _sourceExtractors[sourceType]?.call(source);
|
||||
collect(fromMethod);
|
||||
}
|
||||
|
||||
collect(endpoint.paymentMethodRef);
|
||||
|
||||
return refs;
|
||||
}
|
||||
|
||||
String? _walletSourceRef(PaymentMethodData source) {
|
||||
if (source is ManagedWalletPaymentMethod) {
|
||||
return _normalize(source.managedWalletRef) ??
|
||||
_sourceRefFromMetadata(source.metadata, PaymentSourceType.wallet);
|
||||
}
|
||||
if (source is WalletPaymentMethod) {
|
||||
return _normalize(source.walletId) ??
|
||||
_sourceRefFromMetadata(source.metadata, PaymentSourceType.wallet);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
String? _walletSourceRef(PaymentMethodData source) => switch (source) {
|
||||
ManagedWalletPaymentMethod(:final managedWalletRef) => _normalize(
|
||||
managedWalletRef,
|
||||
),
|
||||
WalletPaymentMethod(:final walletId) => _normalize(walletId),
|
||||
_ => null,
|
||||
};
|
||||
|
||||
String? _ledgerSourceRef(PaymentMethodData source) {
|
||||
if (source is LedgerPaymentMethod) {
|
||||
return _normalize(source.ledgerAccountRef) ??
|
||||
_sourceRefFromMetadata(source.metadata, PaymentSourceType.ledger);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
String? _sourceRefFromMetadata(
|
||||
Map<String, String>? metadata,
|
||||
PaymentSourceType sourceType,
|
||||
) {
|
||||
if (metadata == null || metadata.isEmpty) return null;
|
||||
|
||||
final keys = switch (sourceType) {
|
||||
PaymentSourceType.wallet => const <String>[
|
||||
'source_wallet_ref',
|
||||
'managed_wallet_ref',
|
||||
'wallet_ref',
|
||||
'wallet_id',
|
||||
'source_wallet_id',
|
||||
'source_wallet_user_id',
|
||||
'wallet_user_id',
|
||||
'wallet_user_ref',
|
||||
'wallet_number',
|
||||
'source_wallet_number',
|
||||
'source_managed_wallet_ref',
|
||||
'source_ref',
|
||||
],
|
||||
PaymentSourceType.ledger => const <String>[
|
||||
'source_ledger_account_ref',
|
||||
'ledger_account_ref',
|
||||
'source_account_code',
|
||||
'ledger_account_code',
|
||||
'account_code',
|
||||
'source_ref',
|
||||
],
|
||||
};
|
||||
|
||||
for (final key in keys) {
|
||||
final value = _normalize(metadata[key]);
|
||||
if (value != null) return value;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
String? _ledgerSourceRef(PaymentMethodData source) => switch (source) {
|
||||
LedgerPaymentMethod(:final ledgerAccountRef) => _normalize(ledgerAccountRef),
|
||||
_ => null,
|
||||
};
|
||||
|
||||
String? _normalize(String? value) {
|
||||
final normalized = value?.trim();
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
import 'package:pshared/utils/localization.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
|
||||
String formatMoney(Money? money, {String fallback = '-'}) {
|
||||
final amount = money?.amount.trim();
|
||||
if (amount == null || amount.isEmpty) return fallback;
|
||||
return '$amount ${money!.currency}';
|
||||
}
|
||||
|
||||
String formatAmount(double amount, String currency, {String fallback = '-'}) {
|
||||
final trimmed = currency.trim();
|
||||
if (trimmed.isEmpty) return amountToString(amount);
|
||||
final symbol = currencySymbolFromCode(trimmed);
|
||||
final suffix = symbol ?? trimmed;
|
||||
return '${amountToString(amount)} $suffix';
|
||||
}
|
||||
|
||||
String formatDateLabel(BuildContext context, DateTime? date, {String fallback = '-'}) {
|
||||
if (date == null || date.millisecondsSinceEpoch == 0) return fallback;
|
||||
return dateTimeToLocalFormat(context, date.toLocal());
|
||||
}
|
||||
|
||||
String formatLongDate(BuildContext context, DateTime? date, {String fallback = '-'}) {
|
||||
if (date == null || date.millisecondsSinceEpoch == 0) return fallback;
|
||||
final locale = Localizations.localeOf(context).toString();
|
||||
final formatter = DateFormat('d MMMM y', locale);
|
||||
return formatter.format(date.toLocal());
|
||||
}
|
||||
|
||||
String collapseWhitespace(String value) {
|
||||
return value.replaceAll(RegExp(r'\s+'), ' ').trim();
|
||||
}
|
||||
Reference in New Issue
Block a user