Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VDG] SendViewModel: Make AmountBtc nullable #12539

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
@@ -1,4 +1,4 @@
using System.Reactive.Concurrency;

Check notice on line 1 in WalletWasabi.Fluent/ViewModels/Wallets/Send/SendViewModel.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (master)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 4.60 to 4.80, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -49,7 +49,7 @@
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 @@
return;
}

var amount = new Money(AmountBtc, MoneyUnit.BTC);
if (AmountBtc is not { } amountBtc)
{
return;
}

var amount = new Money(amountBtc, MoneyUnit.BTC);

Check warning on line 127 in WalletWasabi.Fluent/ViewModels/Wallets/Send/SendViewModel.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (master)

❌ New issue: Complex Method

SendViewModel has a cyclomatic complexity of 9, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

Check notice on line 127 in WalletWasabi.Fluent/ViewModels/Wallets/Send/SendViewModel.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (master)

✅ No longer an issue: Large Method

SendViewModel is no longer above the threshold for lines of code
var transactionInfo = new TransactionInfo(BitcoinAddress.Create(To, _wallet.Network), _wallet.AnonScoreTarget)
{
Amount = amount,
Expand Down Expand Up @@ -216,6 +221,11 @@

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