Skip to content

Commit

Permalink
Don't allow adding an already existing address
Browse files Browse the repository at this point in the history
  • Loading branch information
Coding-Enthusiast committed Feb 19, 2025
1 parent 162fed1 commit c7bb156
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
23 changes: 21 additions & 2 deletions WatchOnlyBitcoinWallet/ViewModels/AddEditViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,32 @@

using Autarkysoft.Bitcoin;
using Autarkysoft.Bitcoin.Encoders;
using System;
using System.Collections.Generic;
using System.Linq;
using WatchOnlyBitcoinWallet.Models;
using WatchOnlyBitcoinWallet.MVVM;

namespace WatchOnlyBitcoinWallet.ViewModels
{
public class AddEditViewModel : ViewModelBase
{
public AddEditViewModel() : this(string.Empty, string.Empty)
/// <summary>
/// Make designer happy!
/// </summary>
public AddEditViewModel() : this(string.Empty, string.Empty, Array.Empty<BitcoinAddress>())
{
}

public AddEditViewModel(string addr, string tag)


public AddEditViewModel(IList<BitcoinAddress> addrList) : this(string.Empty, string.Empty, addrList)
{
}

public AddEditViewModel(string addr, string tag, IList<BitcoinAddress> addrList)
{
addresses = addrList;
// Set fields since we don't want to change IsChanged to true.
_addr = addr;
_tag = tag;
Expand All @@ -26,6 +40,7 @@ public AddEditViewModel(string addr, string tag)
}


private readonly IList<BitcoinAddress> addresses;
public bool IsChanged { get; private set; } = false;

private string _addr;
Expand Down Expand Up @@ -70,6 +85,10 @@ private void Ok()
{
Error = "Invalid address type.";
}
else if (addresses.Any(x => x.Address == AddressString))
{
Error = "Wallet already contains the given address.";
}
else
{
RaiseCloseEvent();
Expand Down
11 changes: 3 additions & 8 deletions WatchOnlyBitcoinWallet/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ private async void GetBalance()
public BindableCommand AddCommand { get; }
public async void Add()
{
AddEditViewModel vm = new();
AddEditViewModel vm = new(AddressList);
await WindowMan.ShowDialog(vm);
if (vm.IsChanged)
{
Expand Down Expand Up @@ -324,20 +324,15 @@ public async void Edit()
{
if (SelectedAddress is not null)
{
AddEditViewModel vm = new()
{
AddressString = SelectedAddress.Address,
Tag = SelectedAddress.Name
};

AddEditViewModel vm = new(SelectedAddress.Address, SelectedAddress.Name, Array.Empty<BitcoinAddress>());
await WindowMan.ShowDialog(vm);
if (vm.IsChanged)
{
SelectedAddress.Address = vm.AddressString;
SelectedAddress.Name = vm.Tag;

SaveWallet();
}
}
}
else
{
Expand Down

0 comments on commit c7bb156

Please sign in to comment.