Skip to content

Commit bb60eb1

Browse files
Add a file picker window to be used for importing addresses from text file
1 parent 5e9ea16 commit bb60eb1

File tree

6 files changed

+103
-10
lines changed

6 files changed

+103
-10
lines changed

WatchOnlyBitcoinWallet/App.axaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public override void OnFrameworkInitializationCompleted()
3030

3131
Debug.Assert(desktop.MainWindow.Clipboard is not null);
3232
vm.Clipboard = desktop.MainWindow.Clipboard;
33+
vm.FileMan.StorageProvider = desktop.MainWindow.StorageProvider;
3334
}
3435

3536
base.OnFrameworkInitializationCompleted();

WatchOnlyBitcoinWallet/Services/FileManager.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@
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 Avalonia.Platform.Storage;
67
using Newtonsoft.Json;
78
using System;
89
using System.Collections.Generic;
910
using System.IO;
11+
using System.Linq;
12+
using System.Threading.Tasks;
1013
using WatchOnlyBitcoinWallet.Models;
1114

1215
namespace WatchOnlyBitcoinWallet.Services
1316
{
1417
public interface IFileManager
1518
{
19+
public Task<string[]> OpenFilePickerAsync();
20+
public IStorageProvider? StorageProvider { get; set; }
21+
1622
SettingsModel ReadSettingsFile();
1723
List<BitcoinAddress> ReadWalletFile();
1824
void WriteSettings(SettingsModel settings);
@@ -38,6 +44,48 @@ public FileManager()
3844

3945
private readonly string mainDir, walletPath, settingsPath;
4046

47+
public IWindowManager? WinMan { get; set; }
48+
public IStorageProvider? StorageProvider { get; set; }
49+
50+
51+
public async Task<string[]> OpenFilePickerAsync()
52+
{
53+
if (WinMan is null)
54+
{
55+
return ["WindowManager instance is not set (this is a bug)."];
56+
}
57+
if (StorageProvider is null)
58+
{
59+
await WinMan.ShowMessageBox(MessageBoxType.Ok, "StorageProvider is not set (this is a bug).");
60+
return Array.Empty<string>();
61+
}
62+
63+
FilePickerFileType fileType = new("txt")
64+
{
65+
Patterns = ["*.txt"]
66+
};
67+
68+
FilePickerOpenOptions options = new()
69+
{
70+
AllowMultiple = false,
71+
FileTypeFilter = [fileType],
72+
Title = "Text files (.txt)"
73+
};
74+
75+
try
76+
{
77+
IReadOnlyList<IStorageFile> dir = await StorageProvider.OpenFilePickerAsync(options);
78+
if (dir != null && dir.Count > 0)
79+
{
80+
return File.ReadAllLines(dir.ElementAt(0).Path.LocalPath);
81+
}
82+
}
83+
catch (Exception ex)
84+
{
85+
await WinMan.ShowMessageBox(MessageBoxType.Ok, ex.Message);
86+
}
87+
return Array.Empty<string>();
88+
}
4189

4290
public static T? ReadFile<T>(string filePath)
4391
{

WatchOnlyBitcoinWallet/Services/WindowManager.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace WatchOnlyBitcoinWallet.Services
1515
public interface IWindowManager
1616
{
1717
Task ShowDialog(ViewModelBase vm);
18+
Task<MessageBoxResult> ShowMessageBox(MessageBoxType mbType, string message);
1819
}
1920

2021
public class WindowManager : IWindowManager
@@ -39,5 +40,28 @@ public Task ShowDialog(ViewModelBase vm)
3940

4041
return win.ShowDialog(lf.MainWindow);
4142
}
43+
44+
public async Task<MessageBoxResult> ShowMessageBox(MessageBoxType mbType, string message)
45+
{
46+
MessageBoxViewModel vm = new(mbType, message);
47+
Window win = new()
48+
{
49+
Content = vm,
50+
WindowStartupLocation = WindowStartupLocation.CenterOwner,
51+
CanResize = false,
52+
ShowInTaskbar = false,
53+
SizeToContent = SizeToContent.WidthAndHeight,
54+
Title = "Warning!",
55+
};
56+
vm.CLoseEvent += (s, e) => win.Close();
57+
58+
var lf = (IClassicDesktopStyleApplicationLifetime?)Application.Current?.ApplicationLifetime;
59+
Debug.Assert(lf is not null);
60+
Debug.Assert(lf.MainWindow is not null);
61+
62+
await win.ShowDialog(lf.MainWindow);
63+
64+
return vm.Result;
65+
}
4266
}
4367
}

WatchOnlyBitcoinWallet/ViewModels/ImportViewModel.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ public class ImportViewModel : ViewModelBase
1818
/// <summary>
1919
/// Make designer happy!
2020
/// </summary>
21-
public ImportViewModel() : this(Array.Empty<BitcoinAddress>())
21+
public ImportViewModel() : this(Array.Empty<BitcoinAddress>(), new Services.FileManager())
2222
{
2323
}
2424

25-
public ImportViewModel(IList<BitcoinAddress> addrList)
25+
public ImportViewModel(IList<BitcoinAddress> addrList, Services.IFileManager fileMan)
2626
{
2727
addresses = addrList;
28+
this.fileMan = fileMan;
2829
ImportCommand = new BindableCommand(CheckAndImport);
30+
OpenCommand = new BindableCommand(Open);
2931
}
3032

3133

@@ -35,11 +37,17 @@ public ImportViewModel(IList<BitcoinAddress> addrList)
3537

3638

3739
private readonly IList<BitcoinAddress> addresses;
40+
private readonly Services.IFileManager fileMan;
3841
public bool IsChanged { get; private set; } = false;
3942
public List<BitcoinAddress> Result { get; } = new();
4043

4144

42-
public string ImportText { get; set; } = string.Empty;
45+
private string _text = string.Empty;
46+
public string ImportText
47+
{
48+
get => _text;
49+
set => SetField(ref _text, value);
50+
}
4351

4452

4553
public BindableCommand ImportCommand { get; private set; }
@@ -79,5 +87,13 @@ private void CheckAndImport()
7987
RaiseCloseEvent();
8088
}
8189
}
90+
91+
92+
public BindableCommand OpenCommand { get; }
93+
private async void Open()
94+
{
95+
string[] res = await fileMan.OpenFilePickerAsync();
96+
ImportText = string.Join(Environment.NewLine, res);
97+
}
8298
}
8399
}

WatchOnlyBitcoinWallet/ViewModels/MainWindowViewModel.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ public class MainWindowViewModel : ViewModelBase
2222
public MainWindowViewModel()
2323
{
2424
WindowMan = new WindowManager();
25-
FileMan = new FileManager();
25+
FileMan = new FileManager()
26+
{
27+
WinMan = WindowMan
28+
};
2629

2730
AddressList = new(FileMan.ReadWalletFile());
2831
SettingsInstance = FileMan.ReadSettingsFile();
@@ -77,7 +80,7 @@ public int SelectedIndex
7780
}
7881
}
7982

80-
public IClipboard Clipboard { get; set; }
83+
public IClipboard? Clipboard { get; set; }
8184
public IFileManager FileMan { get; set; }
8285
public IWindowManager WindowMan { get; set; }
8386
public SettingsModel SettingsInstance { get; }
@@ -140,6 +143,7 @@ private async void QueueSaveWallet()
140143
public BindableCommand OpenAboutCommand { get; private set; }
141144
private async void OpenAbout()
142145
{
146+
Debug.Assert(Clipboard is not null);
143147
AboutViewModel vm = new($"({VersionString})", Clipboard);
144148
await WindowMan.ShowDialog(vm);
145149
}
@@ -164,7 +168,7 @@ private async void ForkBalance()
164168
public BindableCommand ImportCommand { get; private set; }
165169
private async void Import()
166170
{
167-
ImportViewModel vm = new(AddressList);
171+
ImportViewModel vm = new(AddressList, FileMan);
168172
await WindowMan.ShowDialog(vm);
169173
if (vm.IsChanged)
170174
{

WatchOnlyBitcoinWallet/Views/ImportView.axaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5-
xmlns:vm="using:WatchOnlyBitcoinWallet.ViewModels"
5+
xmlns:vm="clr-namespace:WatchOnlyBitcoinWallet.ViewModels"
66
mc:Ignorable="d"
77
x:Class="WatchOnlyBitcoinWallet.Views.ImportView"
88
Height="350" Width="460"
@@ -21,10 +21,10 @@
2121
<StackPanel Orientation="Vertical" Spacing="3" Margin="3" Grid.Column="1" Grid.Row="0">
2222
<Button Content="Import"
2323
Command="{Binding ImportCommand}"
24-
Width="70"/>
25-
<Button Content="Open"
24+
Width="80"/>
25+
<Button Content="Open txt"
2626
Command="{Binding OpenCommand}"
27-
Width="70"/>
27+
Width="80"/>
2828
</StackPanel>
2929

3030
<TextBox Text="{Binding ImportText}"

0 commit comments

Comments
 (0)