exchange view amount string provider fix

This commit is contained in:
julian-CStack 2023-07-19 08:30:15 -06:00
parent e74b0babf6
commit d867270aac
3 changed files with 50 additions and 23 deletions

View file

@ -35,6 +35,7 @@ import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart
import 'package:stackwallet/services/exchange/majestic_bank/majestic_bank_exchange.dart'; import 'package:stackwallet/services/exchange/majestic_bank/majestic_bank_exchange.dart';
import 'package:stackwallet/services/exchange/trocador/trocador_exchange.dart'; import 'package:stackwallet/services/exchange/trocador/trocador_exchange.dart';
import 'package:stackwallet/themes/stack_colors.dart'; import 'package:stackwallet/themes/stack_colors.dart';
import 'package:stackwallet/utilities/amount/amount_unit.dart';
import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart'; import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart';
@ -160,26 +161,15 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
if (value == null) { if (value == null) {
return null; return null;
} }
try {
// wtf Dart?????
// This turns "99999999999999999999" into 100000000000000000000.0
// final numFromLocalised = NumberFormat.decimalPattern(
// ref.read(localeServiceChangeNotifierProvider).locale)
// .parse(value);
// return Decimal.tryParse(numFromLocalised.toString());
try { return AmountUnit.normal
return Decimal.parse(value); .tryParse(
} catch (_) { value,
try { locale: ref.read(localeServiceChangeNotifierProvider).locale,
return Decimal.parse(value.replaceAll(",", ".")); coin: Coin.bitcoin, // dummy value (not used due to override)
} catch (_) { overrideWithDecimalPlacesFromString: true,
rethrow; )
} ?.decimal;
}
} catch (_) {
return null;
}
} }
Future<AggregateCurrency> _getAggregateCurrency(Currency currency) async { Future<AggregateCurrency> _getAggregateCurrency(Currency currency) async {
@ -824,7 +814,7 @@ class _ExchangeFormState extends ConsumerState<ExchangeForm> {
}); });
ref.listen(efEstimateProvider.notifier, (previous, next) { ref.listen(efEstimateProvider.notifier, (previous, next) {
final estimate = (next as StateController<Estimate?>).state; final estimate = (next).state;
if (ref.read(efReversedProvider)) { if (ref.read(efReversedProvider)) {
updateSend(estimate); updateSend(estimate);
} else { } else {

View file

@ -13,8 +13,12 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/models/exchange/active_pair.dart'; import 'package:stackwallet/models/exchange/active_pair.dart';
import 'package:stackwallet/models/exchange/response_objects/estimate.dart'; import 'package:stackwallet/models/exchange/response_objects/estimate.dart';
import 'package:stackwallet/models/exchange/response_objects/range.dart'; import 'package:stackwallet/models/exchange/response_objects/range.dart';
import 'package:stackwallet/providers/global/locale_provider.dart';
import 'package:stackwallet/services/exchange/exchange.dart'; import 'package:stackwallet/services/exchange/exchange.dart';
import 'package:stackwallet/services/exchange/exchange_response.dart'; import 'package:stackwallet/services/exchange/exchange_response.dart';
import 'package:stackwallet/utilities/amount/amount.dart';
import 'package:stackwallet/utilities/amount/amount_unit.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/enums/exchange_rate_type_enum.dart'; import 'package:stackwallet/utilities/enums/exchange_rate_type_enum.dart';
import 'package:tuple/tuple.dart'; import 'package:tuple/tuple.dart';
@ -44,7 +48,22 @@ final efSendAmountStringProvider = StateProvider<String>((ref) {
if (refreshing && reversed) { if (refreshing && reversed) {
return "-"; return "-";
} else { } else {
return ref.watch(efSendAmountProvider)?.toString() ?? ""; final decimal = ref.watch(efSendAmountProvider);
String string = "";
if (decimal != null) {
final amount = Amount.fromDecimal(decimal, fractionDigits: decimal.scale);
final locale = ref.watch(localeServiceChangeNotifierProvider).locale;
string = AmountUnit.normal.displayAmount(
amount: amount,
locale: locale,
coin: Coin
.nano, // use nano just to ensure decimal.scale < Coin.value.decimals
withUnitName: false,
maxDecimalPlaces: decimal.scale,
);
}
return string;
} }
}); });
final efReceiveAmountStringProvider = StateProvider<String>((ref) { final efReceiveAmountStringProvider = StateProvider<String>((ref) {
@ -54,7 +73,22 @@ final efReceiveAmountStringProvider = StateProvider<String>((ref) {
if (refreshing && reversed == false) { if (refreshing && reversed == false) {
return "-"; return "-";
} else { } else {
return ref.watch(efReceiveAmountProvider)?.toString() ?? ""; final decimal = ref.watch(efReceiveAmountProvider);
String string = "";
if (decimal != null) {
final amount = Amount.fromDecimal(decimal, fractionDigits: decimal.scale);
final locale = ref.watch(localeServiceChangeNotifierProvider).locale;
string = AmountUnit.normal.displayAmount(
amount: amount,
locale: locale,
coin: Coin
.nano, // use nano just to ensure decimal.scale < Coin.value.decimals
withUnitName: false,
maxDecimalPlaces: decimal.scale,
);
}
return string;
} }
}); });

View file

@ -168,6 +168,7 @@ extension AmountUnitExt on AmountUnit {
required String locale, required String locale,
required Coin coin, required Coin coin,
EthContract? tokenContract, EthContract? tokenContract,
bool overrideWithDecimalPlacesFromString = false,
}) { }) {
final precisionLost = value.startsWith("~"); final precisionLost = value.startsWith("~");
@ -201,7 +202,9 @@ extension AmountUnitExt on AmountUnit {
return null; return null;
} }
final decimalPlaces = tokenContract?.decimals ?? coin.decimals; final decimalPlaces = overrideWithDecimalPlacesFromString
? decimal.scale
: tokenContract?.decimals ?? coin.decimals;
final realShift = math.min(shift, decimalPlaces); final realShift = math.min(shift, decimalPlaces);
return decimal.shift(0 - realShift).toAmount(fractionDigits: decimalPlaces); return decimal.shift(0 - realShift).toAmount(fractionDigits: decimalPlaces);