2023-05-29 17:42:11 +00:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2024-05-23 00:37:06 +00:00
|
|
|
import '../../models/isar/models/ethereum/eth_contract.dart';
|
|
|
|
import '../../providers/global/locale_provider.dart';
|
|
|
|
import '../../providers/global/prefs_provider.dart';
|
|
|
|
import 'amount.dart';
|
|
|
|
import 'amount_unit.dart';
|
|
|
|
import '../../wallets/crypto_currency/crypto_currency.dart';
|
2023-05-29 17:42:11 +00:00
|
|
|
|
2024-05-15 21:20:45 +00:00
|
|
|
final pAmountUnit = Provider.family<AmountUnit, CryptoCurrency>(
|
2023-05-29 17:42:11 +00:00
|
|
|
(ref, coin) => ref.watch(
|
|
|
|
prefsChangeNotifierProvider.select(
|
|
|
|
(value) => value.amountUnit(coin),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2024-05-15 21:20:45 +00:00
|
|
|
final pMaxDecimals = Provider.family<int, CryptoCurrency>(
|
2023-05-29 17:42:11 +00:00
|
|
|
(ref, coin) => ref.watch(
|
|
|
|
prefsChangeNotifierProvider.select(
|
|
|
|
(value) => value.maxDecimals(coin),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2024-05-15 21:20:45 +00:00
|
|
|
final pAmountFormatter =
|
|
|
|
Provider.family<AmountFormatter, CryptoCurrency>((ref, coin) {
|
2023-07-12 22:06:06 +00:00
|
|
|
final locale = ref.watch(
|
|
|
|
localeServiceChangeNotifierProvider.select((value) => value.locale),
|
|
|
|
);
|
|
|
|
|
2023-05-29 17:42:11 +00:00
|
|
|
return AmountFormatter(
|
|
|
|
unit: ref.watch(pAmountUnit(coin)),
|
2023-07-12 22:06:06 +00:00
|
|
|
locale: locale,
|
2023-05-29 17:42:11 +00:00
|
|
|
coin: coin,
|
|
|
|
maxDecimals: ref.watch(pMaxDecimals(coin)),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
class AmountFormatter {
|
|
|
|
final AmountUnit unit;
|
|
|
|
final String locale;
|
2024-05-15 21:20:45 +00:00
|
|
|
final CryptoCurrency coin;
|
2023-05-29 17:42:11 +00:00
|
|
|
final int maxDecimals;
|
|
|
|
|
|
|
|
AmountFormatter({
|
|
|
|
required this.unit,
|
|
|
|
required this.locale,
|
|
|
|
required this.coin,
|
|
|
|
required this.maxDecimals,
|
|
|
|
});
|
|
|
|
|
2023-05-29 21:10:55 +00:00
|
|
|
String format(
|
|
|
|
Amount amount, {
|
|
|
|
String? overrideUnit,
|
|
|
|
EthContract? ethContract,
|
|
|
|
bool withUnitName = true,
|
2023-05-30 15:02:09 +00:00
|
|
|
bool indicatePrecisionLoss = true,
|
2023-05-29 21:10:55 +00:00
|
|
|
}) {
|
2023-05-29 17:42:11 +00:00
|
|
|
return unit.displayAmount(
|
|
|
|
amount: amount,
|
|
|
|
locale: locale,
|
|
|
|
coin: coin,
|
|
|
|
maxDecimalPlaces: maxDecimals,
|
2023-05-29 21:10:55 +00:00
|
|
|
withUnitName: withUnitName,
|
2023-05-30 15:02:09 +00:00
|
|
|
indicatePrecisionLoss: indicatePrecisionLoss,
|
2023-05-29 21:10:55 +00:00
|
|
|
overrideUnit: overrideUnit,
|
|
|
|
tokenContract: ethContract,
|
2023-05-29 17:42:11 +00:00
|
|
|
);
|
|
|
|
}
|
2023-06-16 16:43:20 +00:00
|
|
|
|
2023-06-16 19:42:37 +00:00
|
|
|
Amount? tryParse(
|
2023-06-16 16:43:20 +00:00
|
|
|
String string, {
|
|
|
|
EthContract? ethContract,
|
|
|
|
}) {
|
2023-07-20 15:11:36 +00:00
|
|
|
return unit.tryParse(
|
|
|
|
string,
|
|
|
|
locale: locale,
|
|
|
|
coin: coin,
|
|
|
|
tokenContract: ethContract,
|
|
|
|
);
|
2023-06-16 16:43:20 +00:00
|
|
|
}
|
2023-05-29 17:42:11 +00:00
|
|
|
}
|