Skip to content

Commit 80526ae

Browse files
Improve the import window
1 parent c7bb156 commit 80526ae

File tree

10 files changed

+147
-184
lines changed

10 files changed

+147
-184
lines changed

WatchOnlyBitcoinWallet/App.axaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
<DataTemplate DataType="{x:Type vm:AddEditViewModel}">
1919
<views:AddEditView/>
2020
</DataTemplate>
21+
<DataTemplate DataType="{x:Type vm:ImportViewModel}">
22+
<views:ImportView/>
23+
</DataTemplate>
2124
<DataTemplate DataType="{x:Type vm:SettingsViewModel}">
2225
<views:SettingsView/>
2326
</DataTemplate>
2427
</Application.DataTemplates>
2528

26-
<Application.Resources>
27-
28-
</Application.Resources>
29+
<Application.Styles>
30+
<Style Selector="Button">
31+
<Setter Property="HorizontalContentAlignment" Value="Center"/>
32+
</Style>
33+
</Application.Styles>
2934
</Application>
2.25 KB
Loading

WatchOnlyBitcoinWallet/MainWindow.axaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
x:CompileBindings="True"
1313
x:DataType="vm:MainWindowViewModel"
1414
Title="Watch Only Bitcoin Wallet - Version 4"
15-
Height="340" Width="740"
15+
Height="370" Width="740"
1616
CanResize="False"
1717
Icon="/Assets/Icon.png"
1818
WindowStartupLocation="CenterScreen" FontSize="14">
@@ -51,11 +51,8 @@
5151
InputGesture="Alt+F4"/>
5252
</MenuItem>
5353
<MenuItem Header="_Import">
54-
<MenuItem Header="From _text"
55-
Command="{Binding ImportFromTextCommand}"/>
56-
<MenuItem Header="From _file"
57-
Command="{Binding ImportFromFileCommand}"
58-
ToolTip.Tip="Addresses in the file should be separated by a new line."/>
54+
<MenuItem Header="From _text or file"
55+
Command="{Binding ImportCommand}"/>
5956
</MenuItem>
6057
</Menu>
6158

@@ -147,6 +144,9 @@
147144
</Style>
148145
</StackPanel.Styles>
149146

147+
<Button ToolTip.Tip="Import (multi)" Command="{Binding ImportCommand}">
148+
<Image Source="/Assets/Import.png"/>
149+
</Button>
150150
<Button ToolTip.Tip="Add New" Command="{Binding AddCommand}">
151151
<Image Source="/Assets/Add.png"/>
152152
</Button>

WatchOnlyBitcoinWallet/Models/BitcoinAddress.cs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,65 @@
55

66
using Newtonsoft.Json;
77
using System.Collections.Generic;
8-
using WatchOnlyBitcoinWallet.ViewModels;
8+
using WatchOnlyBitcoinWallet.MVVM;
99

1010
namespace WatchOnlyBitcoinWallet.Models
1111
{
12-
public class BitcoinAddress : ValidatableBase
12+
public class BitcoinAddress : InpcBase
1313
{
14-
private string name;
14+
public BitcoinAddress()
15+
{
16+
}
17+
18+
public BitcoinAddress(string addr, string tag)
19+
{
20+
Address = addr;
21+
Name = tag;
22+
}
23+
24+
private string _name = string.Empty;
1525
/// <summary>
1626
/// Name acts as a tag for the address
1727
/// </summary>
1828
public string Name
1929
{
20-
get { return name; }
21-
set { SetField(ref name, value); }
30+
get => _name;
31+
set => SetField(ref _name, value);
2232
}
2333

24-
private string address;
34+
private string _address = string.Empty;
2535
public string Address
2636
{
27-
get { return address; }
28-
set
29-
{
30-
if (SetField(ref address, value))
31-
{
32-
Validate(value);
33-
}
34-
}
37+
get => _address;
38+
set => SetField(ref _address, value);
3539
}
3640

37-
private decimal balance;
41+
private decimal _balance;
3842
public decimal Balance
3943
{
40-
get { return balance; }
41-
set { SetField(ref balance, value); }
44+
get => _balance;
45+
set => SetField(ref _balance, value);
4246
}
4347

44-
private decimal difference;
48+
private decimal _diff;
4549
[JsonIgnore]
4650
public decimal Difference
4751
{
48-
get { return difference; }
49-
set { SetField(ref difference, value); }
52+
get => _diff;
53+
set => SetField(ref _diff, value);
5054
}
5155

52-
private decimal forkBalance;
56+
private decimal _forkBalance;
5357
/// <summary>
5458
/// Total balance that was available by the time of fork
5559
/// </summary>
5660
[JsonIgnore]
5761
public decimal ForkBalance
5862
{
59-
get { return forkBalance; }
60-
set { SetField(ref forkBalance, value); }
63+
get => _forkBalance;
64+
set => SetField(ref _forkBalance, value);
6165
}
6266

63-
64-
public List<Transaction> TransactionList { get; set; }
65-
67+
public List<Transaction> TransactionList { get; set; } = new();
6668
}
6769
}

WatchOnlyBitcoinWallet/Services/WindowManager.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,4 @@ public Task ShowDialog(ViewModelBase vm)
6363
throw new NotImplementedException();
6464
}
6565
}
66-
67-
public class ImportWindowManager : IWindowManager
68-
{
69-
public void Show(ViewModelBase ViewModel)
70-
{
71-
ImportWindow myWin = new ImportWindow();
72-
myWin.DataContext = ViewModel;
73-
((ImportViewModel)ViewModel).ClosingRequest += (sender, e) => myWin.Close();
74-
//myWin.Owner = Application.Current.MainWindow;
75-
//myWin.ShowDialog();
76-
}
77-
public Task ShowDialog(ViewModelBase vm)
78-
{
79-
throw new NotImplementedException();
80-
}
81-
}
8266
}

WatchOnlyBitcoinWallet/ViewModels/ImportViewModel.cs

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,43 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
55

6+
using Autarkysoft.Bitcoin;
7+
using Autarkysoft.Bitcoin.Encoders;
68
using System;
79
using System.Collections.Generic;
10+
using System.Linq;
811
using WatchOnlyBitcoinWallet.Models;
912
using WatchOnlyBitcoinWallet.MVVM;
10-
using WatchOnlyBitcoinWallet.Services;
1113

1214
namespace WatchOnlyBitcoinWallet.ViewModels
1315
{
1416
public class ImportViewModel : ViewModelBase
1517
{
16-
public ImportViewModel()
18+
/// <summary>
19+
/// Make designer happy!
20+
/// </summary>
21+
public ImportViewModel() : this(Array.Empty<BitcoinAddress>())
1722
{
18-
ImportCommand = new BindableCommand(CheckAndImport);
1923
}
2024

21-
22-
public string Note
25+
public ImportViewModel(IList<BitcoinAddress> addrList)
2326
{
24-
get
25-
{
26-
return "Note: 1 address per line (seperated by a new line).";
27-
}
27+
addresses = addrList;
28+
ImportCommand = new BindableCommand(CheckAndImport);
2829
}
2930

3031

31-
public List<BitcoinAddress> AddressList { get; set; }
32+
public static string Note => $"Enter 1 address per line.{Environment.NewLine}" +
33+
$"Add the optional name at the end separated by comma.{Environment.NewLine}" +
34+
$"Example: address,name";
3235

3336

34-
private string importText;
35-
public string ImportText
36-
{
37-
get { return importText; }
38-
set { SetField(ref importText, value); }
39-
}
37+
private readonly IList<BitcoinAddress> addresses;
38+
public bool IsChanged { get; private set; } = false;
39+
public List<BitcoinAddress> Result { get; } = new();
40+
41+
42+
public string ImportText { get; set; } = string.Empty;
4043

4144

4245
public BindableCommand ImportCommand { get; private set; }
@@ -49,52 +52,32 @@ private void CheckAndImport()
4952
}
5053
else
5154
{
52-
List<BitcoinAddress> temp = new List<BitcoinAddress>();
53-
int lineNum = 0;
54-
foreach (var item in ImportText.SplitToLines())
55+
string[] lines = ImportText.ReplaceLineEndings()
56+
.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
57+
for (int i = 0; i < lines.Length; i++)
5558
{
56-
lineNum++;
57-
VerificationResult vr = ValidateAddr(item);
58-
if (vr.IsVerified)
59+
int index = lines[i].IndexOf(',');
60+
string addr = lines[i].Substring(0, index > 0 ? index : lines[i].Length);
61+
string name = index > 0 ? lines[i].Substring(index + 1).Trim() : string.Empty;
62+
63+
AddressType type = Address.GetAddressType(addr, NetworkType.MainNet);
64+
if (type is AddressType.Unknown or AddressType.Invalid)
5965
{
60-
temp.Add(new BitcoinAddress() { Address = item });
66+
Errors = $"Address on the {(i + 1).ToOrdinal()} line ({addr}) is invalid.";
67+
return;
6168
}
62-
else
69+
else if (addresses.Any(x => x.Address == addr))
6370
{
64-
Errors = string.Format("Invalid address on line {0}: {1}", lineNum, vr.Error);
65-
break;
71+
Errors = $"Wallet already contains the address on the {(i + 1).ToOrdinal()} line ({addr}).";
72+
return;
6673
}
67-
}
68-
if (string.IsNullOrEmpty(Errors))
69-
{
70-
AddressList = temp;
71-
OnClosingRequest();
72-
}
73-
}
74-
}
75-
private VerificationResult ValidateAddr(string addr)
76-
{
77-
VerificationResult vr = new VerificationResult();
78-
if (addr.StartsWith("bc1"))
79-
{
80-
vr = SegWitAddress.Verify(addr, SegWitAddress.NetworkType.MainNet);
81-
}
82-
else
83-
{
84-
vr = Base58.Verify(addr);
85-
}
86-
return vr;
87-
}
8874

89-
public event EventHandler ClosingRequest;
75+
Result.Add(new BitcoinAddress(addr, name));
76+
}
9077

91-
protected void OnClosingRequest()
92-
{
93-
if (ClosingRequest != null)
94-
{
95-
ClosingRequest(this, EventArgs.Empty);
78+
IsChanged = true;
79+
RaiseCloseEvent();
9680
}
9781
}
98-
9982
}
10083
}

0 commit comments

Comments
 (0)