Skip to content

Commit

Permalink
Make AmountBtc nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
ichthus1604 authored and wieslawsoltes committed Feb 22, 2024
1 parent 7515da1 commit 5e0b070
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
24 changes: 12 additions & 12 deletions WalletWasabi.Fluent/Controls/DualCurrencyEntryBox.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class DualCurrencyEntryBox : TemplatedControl
public static readonly StyledProperty<VerticalAlignment> VerticalContentAlignmentProperty =
AvaloniaProperty.Register<DualCurrencyEntryBox, VerticalAlignment>(nameof(VerticalContentAlignment));

public static readonly DirectProperty<DualCurrencyEntryBox, decimal> AmountBtcProperty =
AvaloniaProperty.RegisterDirect<DualCurrencyEntryBox, decimal>(
public static readonly DirectProperty<DualCurrencyEntryBox, decimal?> AmountBtcProperty =
AvaloniaProperty.RegisterDirect<DualCurrencyEntryBox, decimal?>(
nameof(AmountBtc),
o => o.AmountBtc,
(o, v) => o.AmountBtc = v,
Expand Down Expand Up @@ -84,7 +84,7 @@ public class DualCurrencyEntryBox : TemplatedControl

private CompositeDisposable? _disposable;
private Button? _swapButton;
private decimal _amountBtc;
private decimal? _amountBtc;
private bool _isTextInputFocused;
private bool _isConversationTextFocused;
private bool _skipProcessing;
Expand Down Expand Up @@ -116,7 +116,7 @@ public VerticalAlignment VerticalContentAlignment
set { SetValue(VerticalContentAlignmentProperty, value); }
}

public decimal AmountBtc
public decimal? AmountBtc
{
get => _amountBtc;
set => SetAndRaise(AmountBtcProperty, ref _amountBtc, value);
Expand Down Expand Up @@ -233,7 +233,7 @@ private void InputText(string? text)

if (string.IsNullOrWhiteSpace(text))
{
SetBtcAmount(0);
SetBtcAmount(null);
}
else
{
Expand All @@ -260,7 +260,7 @@ private void InputConversionText(string? text)

if (string.IsNullOrWhiteSpace(text))
{
SetBtcAmount(0);
SetBtcAmount(null);
}
else
{
Expand Down Expand Up @@ -288,11 +288,11 @@ private void UpdateTextDisplay(bool insertValue)
var text = LeftEntryBox?.Text ?? "";
if (_isTextInputFocused)
{
text = insertValue ? AmountBtc.ToString(CultureInfo.InvariantCulture) : RemoveFormat(text);
text = insertValue ? AmountBtc?.ToString(CultureInfo.InvariantCulture) : RemoveFormat(text);
}
else
{
text = AmountBtc > 0 ? AmountBtc.FormattedBtc() : string.Empty;
text = AmountBtc > 0 ? AmountBtc?.FormattedBtc() : string.Empty;
}

SetCurrentValue(TextProperty, text);
Expand All @@ -313,17 +313,17 @@ private void UpdateConversationTextDisplay(bool insertValue)
var text = RightEntryBox?.Text ?? "";
if (_isConversationTextFocused)
{
text = insertValue ? RemoveFormat(conversion.FormattedFiat()) : RemoveFormat(text);
text = insertValue ? RemoveFormat(conversion?.FormattedFiat() ?? "") : RemoveFormat(text);
}
else
{
text = AmountBtc > 0 ? conversion.FormattedFiat() : string.Empty;
text = AmountBtc > 0 ? conversion?.FormattedFiat() ?? string.Empty : string.Empty;
}

SetCurrentValue(ConversionTextProperty, text);
}

private void SetBtcAmount(decimal amount)
private void SetBtcAmount(decimal? amount)
{
_skipProcessing = true;
SetCurrentValue(AmountBtcProperty, amount);
Expand All @@ -335,7 +335,7 @@ private decimal FiatToBitcoin(decimal fiatValue)
return fiatValue / ConversionRate;
}

private decimal BitcoinToFiat(decimal btcValue)
private decimal? BitcoinToFiat(decimal? btcValue)
{
return btcValue * ConversionRate;
}
Expand Down
14 changes: 12 additions & 2 deletions WalletWasabi.Fluent/ViewModels/Wallets/Send/SendViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public partial class SendViewModel : RoutableViewModel
private LabelsArray _parsedLabel = LabelsArray.Empty;

[AutoNotify] private string _to;
[AutoNotify] private decimal _amountBtc;
[AutoNotify] private decimal? _amountBtc;
[AutoNotify] private decimal _exchangeRate;
[AutoNotify] private bool _isFixedAmount;
[AutoNotify] private bool _isPayJoin;
Expand Down Expand Up @@ -119,7 +119,12 @@ public SendViewModel(UiContext uiContext, WalletViewModel walletVm)
return;
}
var amount = new Money(AmountBtc, MoneyUnit.BTC);
if (AmountBtc is not { } amountBtc)
{
return;
}
var amount = new Money(amountBtc, MoneyUnit.BTC);
var transactionInfo = new TransactionInfo(BitcoinAddress.Create(To, _wallet.Network), _wallet.AnonScoreTarget)
{
Amount = amount,
Expand Down Expand Up @@ -216,6 +221,11 @@ private async Task OnPasteAsync(bool pasteIfInvalid = true)

private void ValidateAmount(IValidationErrors errors)
{
if (AmountBtc is null)
{
return;
}

if (AmountBtc > Constants.MaximumNumberOfBitcoins)
{
errors.Add(ErrorSeverity.Error, "Amount must be less than the total supply of BTC.");
Expand Down

0 comments on commit 5e0b070

Please sign in to comment.