mounted checks and include fee in estimate for required amount in coin control selection view

This commit is contained in:
julian 2023-03-09 09:09:15 -06:00
parent 4cc738fd3a
commit 210fac593a

View file

@ -159,6 +159,20 @@ class _SendViewState extends ConsumerState<SendView> {
} }
} }
int _currentFee = 0;
void _setCurrentFee(String fee, bool shouldSetState) {
final value = Format.decimalAmountToSatoshis(
Decimal.parse(fee),
coin,
);
if (shouldSetState) {
setState(() => _currentFee = value);
} else {
_currentFee = value;
}
}
String? _updateInvalidAddressText(String address, Manager manager) { String? _updateInvalidAddressText(String address, Manager manager) {
if (_data != null && _data!.contactLabel == address) { if (_data != null && _data!.contactLabel == address) {
return null; return null;
@ -328,45 +342,48 @@ class _SendViewState extends ConsumerState<SendView> {
selectedUTXOs.isEmpty)) { selectedUTXOs.isEmpty)) {
// confirm send all // confirm send all
if (amount == availableBalance) { if (amount == availableBalance) {
final bool? shouldSendAll = await showDialog<bool>( bool? shouldSendAll;
context: context, if (mounted) {
useSafeArea: false, shouldSendAll = await showDialog<bool>(
barrierDismissible: true, context: context,
builder: (context) { useSafeArea: false,
return StackDialog( barrierDismissible: true,
title: "Confirm send all", builder: (context) {
message: return StackDialog(
"You are about to send your entire balance. Would you like to continue?", title: "Confirm send all",
leftButton: TextButton( message:
style: Theme.of(context) "You are about to send your entire balance. Would you like to continue?",
.extension<StackColors>()! leftButton: TextButton(
.getSecondaryEnabledButtonStyle(context), style: Theme.of(context)
child: Text( .extension<StackColors>()!
"Cancel", .getSecondaryEnabledButtonStyle(context),
style: STextStyles.button(context).copyWith( child: Text(
color: Theme.of(context) "Cancel",
.extension<StackColors>()! style: STextStyles.button(context).copyWith(
.accentColorDark), color: Theme.of(context)
.extension<StackColors>()!
.accentColorDark),
),
onPressed: () {
Navigator.of(context).pop(false);
},
), ),
onPressed: () { rightButton: TextButton(
Navigator.of(context).pop(false); style: Theme.of(context)
}, .extension<StackColors>()!
), .getPrimaryEnabledButtonStyle(context),
rightButton: TextButton( child: Text(
style: Theme.of(context) "Yes",
.extension<StackColors>()! style: STextStyles.button(context),
.getPrimaryEnabledButtonStyle(context), ),
child: Text( onPressed: () {
"Yes", Navigator.of(context).pop(true);
style: STextStyles.button(context), },
), ),
onPressed: () { );
Navigator.of(context).pop(true); },
}, );
), }
);
},
);
if (shouldSendAll == null || shouldSendAll == false) { if (shouldSendAll == null || shouldSendAll == false) {
// cancel preview // cancel preview
@ -378,22 +395,24 @@ class _SendViewState extends ConsumerState<SendView> {
try { try {
bool wasCancelled = false; bool wasCancelled = false;
unawaited( if (mounted) {
showDialog<dynamic>( unawaited(
context: context, showDialog<void>(
useSafeArea: false, context: context,
barrierDismissible: false, useSafeArea: false,
builder: (context) { barrierDismissible: false,
return BuildingTransactionDialog( builder: (context) {
onCancel: () { return BuildingTransactionDialog(
wasCancelled = true; onCancel: () {
wasCancelled = true;
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },
); );
}, },
), ),
); );
}
Map<String, dynamic> txData; Map<String, dynamic> txData;
@ -1569,18 +1588,33 @@ class _SendViewState extends ConsumerState<SendView> {
? "Select coins" ? "Select coins"
: "Selected coins (${selectedUTXOs.length})", : "Selected coins (${selectedUTXOs.length})",
onTap: () async { onTap: () async {
final spendable = ref
.read(walletsChangeNotifierProvider)
.getManager(widget.walletId)
.balance
.spendable;
int? amount;
if (_amountToSend != null) {
amount = Format.decimalAmountToSatoshis(
_amountToSend!,
coin,
);
if (spendable == amount) {
// this is now a send all
} else {
amount += _currentFee;
}
}
final result = final result =
await Navigator.of(context).pushNamed( await Navigator.of(context).pushNamed(
CoinControlView.routeName, CoinControlView.routeName,
arguments: Tuple4( arguments: Tuple4(
walletId, walletId,
CoinControlViewType.use, CoinControlViewType.use,
_amountToSend != null amount,
? Format.decimalAmountToSatoshis(
_amountToSend!,
coin,
)
: null,
selectedUTXOs, selectedUTXOs,
), ),
); );
@ -1711,6 +1745,10 @@ class _SendViewState extends ConsumerState<SendView> {
.text) ?? .text) ??
Decimal.zero, Decimal.zero,
updateChosen: (String fee) { updateChosen: (String fee) {
_setCurrentFee(
fee,
true,
);
setState(() { setState(() {
_calculateFeesFuture = _calculateFeesFuture =
Future(() => fee); Future(() => fee);
@ -1736,6 +1774,10 @@ class _SendViewState extends ConsumerState<SendView> {
ConnectionState ConnectionState
.done && .done &&
snapshot.hasData) { snapshot.hasData) {
_setCurrentFee(
snapshot.data! as String,
false,
);
return Text( return Text(
"~${snapshot.data! as String} ${coin.ticker}", "~${snapshot.data! as String} ${coin.ticker}",
style: STextStyles style: STextStyles
@ -1788,6 +1830,11 @@ class _SendViewState extends ConsumerState<SendView> {
ConnectionState ConnectionState
.done && .done &&
snapshot.hasData) { snapshot.hasData) {
_setCurrentFee(
snapshot.data!
as String,
false,
);
return Text( return Text(
"~${snapshot.data! as String} ${coin.ticker}", "~${snapshot.data! as String} ${coin.ticker}",
style: STextStyles style: STextStyles