|
| 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 | +} |
0 commit comments