stack_wallet/lib/utilities/amount/amount_formatter.dart

76 lines
2 KiB
Dart
Raw Normal View History

import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-05-29 21:10:55 +00:00
import 'package:stackwallet/models/isar/models/ethereum/eth_contract.dart';
import 'package:stackwallet/providers/global/locale_provider.dart';
import 'package:stackwallet/providers/global/prefs_provider.dart';
import 'package:stackwallet/utilities/amount/amount.dart';
import 'package:stackwallet/utilities/amount/amount_unit.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
final pAmountUnit = Provider.family<AmountUnit, Coin>(
(ref, coin) => ref.watch(
prefsChangeNotifierProvider.select(
(value) => value.amountUnit(coin),
),
),
);
final pMaxDecimals = Provider.family<int, Coin>(
(ref, coin) => ref.watch(
prefsChangeNotifierProvider.select(
(value) => value.maxDecimals(coin),
),
),
);
final pAmountFormatter = Provider.family<AmountFormatter, Coin>((ref, coin) {
2023-07-12 22:06:06 +00:00
final locale = ref.watch(
localeServiceChangeNotifierProvider.select((value) => value.locale),
);
return AmountFormatter(
unit: ref.watch(pAmountUnit(coin)),
2023-07-12 22:06:06 +00:00
locale: locale,
coin: coin,
maxDecimals: ref.watch(pMaxDecimals(coin)),
);
});
class AmountFormatter {
final AmountUnit unit;
final String locale;
final Coin coin;
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
}) {
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,
);
}
Amount? tryParse(
String string, {
EthContract? ethContract,
}) {
return unit.tryParse(string, locale: locale, coin: coin);
}
}