Skip to content

Commit 95a1c93

Browse files
Add a new file manager
1 parent db9b21e commit 95a1c93

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// WatchOnlyBitcoinWallet
2+
// Copyright (c) 2016 Coding Enthusiast
3+
// Distributed under the MIT software license, see the accompanying
4+
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
5+
6+
using Newtonsoft.Json;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.IO;
10+
using WatchOnlyBitcoinWallet.Models;
11+
12+
namespace WatchOnlyBitcoinWallet.Services
13+
{
14+
public interface IFileManager
15+
{
16+
SettingsModel ReadSettingsFile();
17+
List<BitcoinAddress> ReadWalletFile();
18+
void WriteSettings(SettingsModel settings);
19+
void WriteWallet(List<BitcoinAddress> addresses);
20+
}
21+
22+
23+
public class FileManager : IFileManager
24+
{
25+
public FileManager()
26+
{
27+
mainDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
28+
"Autarkysoft",
29+
"Watch Only Bitcoin Wallet");
30+
31+
walletPath = Path.Combine(mainDir, WalletFileName);
32+
settingsPath = Path.Combine(mainDir, SettingsFileName);
33+
}
34+
35+
36+
private const string WalletFileName = "Wallet.json";
37+
private const string SettingsFileName = "Settings.json";
38+
39+
private readonly string mainDir, walletPath, settingsPath;
40+
41+
42+
public static T? ReadFile<T>(string filePath)
43+
{
44+
if (File.Exists(filePath))
45+
{
46+
using StreamReader st = File.OpenText(filePath);
47+
JsonSerializer ser = new();
48+
var obj = ser.Deserialize(st, typeof(T));
49+
if (obj is not null)
50+
{
51+
return (T?)obj;
52+
}
53+
}
54+
55+
return default;
56+
}
57+
58+
public SettingsModel ReadSettingsFile()
59+
{
60+
SettingsModel? result = ReadFile<SettingsModel>(settingsPath);
61+
return result ?? new SettingsModel();
62+
}
63+
64+
public List<BitcoinAddress> ReadWalletFile()
65+
{
66+
List<BitcoinAddress>? result = ReadFile<List<BitcoinAddress>>(walletPath);
67+
return result ?? new List<BitcoinAddress>();
68+
}
69+
70+
71+
public void WriteFile<T>(T dataToSave, string filePath)
72+
{
73+
if (!Directory.Exists(mainDir))
74+
{
75+
Directory.CreateDirectory(mainDir);
76+
}
77+
78+
using StreamWriter str = File.CreateText(filePath);
79+
JsonSerializer ser = new();
80+
ser.Serialize(str, dataToSave);
81+
}
82+
83+
public void WriteSettings(SettingsModel settings)
84+
{
85+
WriteFile(settings, settingsPath);
86+
}
87+
88+
public void WriteWallet(List<BitcoinAddress> addresses)
89+
{
90+
WriteFile(addresses, walletPath);
91+
}
92+
}
93+
}

WatchOnlyBitcoinWallet/ViewModels/MainWindowViewModel.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ public class MainWindowViewModel : ViewModelBase
2222
public MainWindowViewModel()
2323
{
2424
WindowMan = new WindowManager();
25+
FileMan = new FileManager();
2526

26-
AddressList = new BindingList<BitcoinAddress>(DataManager.ReadFile<List<BitcoinAddress>>(DataManager.FileType.Wallet));
27+
AddressList = new BindingList<BitcoinAddress>(FileMan.ReadWalletFile());
2728
AddressList.ListChanged += AddressList_ListChanged;
2829

29-
SettingsInstance = DataManager.ReadFile<SettingsModel>(DataManager.FileType.Settings);
30+
SettingsInstance = FileMan.ReadSettingsFile();
3031

3132
GetBalanceCommand = new BindableCommand(GetBalance, () => !IsReceiving);
3233
OpenAboutCommand = new BindableCommand(OpenAbout);
@@ -36,7 +37,7 @@ public MainWindowViewModel()
3637
ImportFromTextCommand = new BindableCommand(ImportFromText);
3738
ImportFromFileCommand = new BindableCommand(ImportFromFile);
3839

39-
var ver = Assembly.GetExecutingAssembly().GetName().Version;
40+
Version ver = Assembly.GetExecutingAssembly().GetName().Version ?? new Version();
4041
VersionString = string.Format("Version {0}.{1}.{2}", ver.Major, ver.Minor, ver.Build);
4142
}
4243

@@ -65,6 +66,7 @@ void AddressList_ListChanged(object sender, ListChangedEventArgs e)
6566

6667
public IWindowManager WindowMan { get; set; }
6768
public IClipboard Clipboard { get; set; }
69+
public IFileManager FileMan { get; set; }
6870

6971

7072
/// <summary>

0 commit comments

Comments
 (0)