Refactor backend logic
MODIFY
WalletWasabi.Gui/Config.cs
WalletWasabi.Gui/Config.cs
@@ -169,6 +169,18 @@ namespace WalletWasabi.Gui
Logger.LogInfo<Config>($"{nameof(Config)} file did not exist. Created at path: `{FilePath}`.");
}
else
+ {
+ await LoadFileAsync();
+ }
+
+ // Just debug convenience.
+ _backendUri = GetCurrentBackendUri();
+ _blindingRsaPubKey = GetBlindingRsaPubKey();
+
+ await ToFileAsync();
+ }
+
+ public async Task LoadFileAsync()
{
string jsonString = await File.ReadAllTextAsync(FilePath, Encoding.UTF8);
var config = JsonConvert.DeserializeObject<Config>(jsonString);
@@ -185,12 +197,10 @@ namespace WalletWasabi.Gui
TorHost = config.TorHost ?? TorHost;
TorSocks5Port = config.TorSocks5Port ?? TorSocks5Port;
- }
+ // Just debug convenience.
_backendUri = GetCurrentBackendUri();
_blindingRsaPubKey = GetBlindingRsaPubKey();
-
- await ToFileAsync();
}
/// <inheritdoc />
@@ -265,22 +275,5 @@ namespace WalletWasabi.Gui
{
if (FilePath == null) throw new NotSupportedException($"{nameof(FilePath)} is not set. Use {nameof(SetFilePath)} to set it.");
}
-
- public Config Clone()
- {
- var cfg = new Config();
- cfg.FilePath = FilePath;
- cfg.Network = Network;
-
- cfg.MainNetBackendUriV3 = MainNetBackendUriV3;
- cfg.TestNetBackendUriV3 = TestNetBackendUriV3;
- cfg.RegTestBackendUriV3 = RegTestBackendUriV3;
- cfg.MainNetBlindingRsaPubKey = MainNetBlindingRsaPubKey;
- cfg.TestNetBlindingRsaPubKey = TestNetBlindingRsaPubKey;
- cfg.RegTestBlindingRsaPubKey = RegTestBlindingRsaPubKey;
- cfg.TorHost = TorHost;
- cfg.TorSocks5Port = TorSocks5Port;
- return cfg;
- }
}
}

MODIFY
WalletWasabi.Gui/Tabs/SettingsViewModel.cs
WalletWasabi.Gui/Tabs/SettingsViewModel.cs
@@ -6,6 +6,7 @@ using System.Collections.Generic;
using Avalonia.Threading;
using WalletWasabi.Gui.ViewModels.Validation;
using System.Net;
+using System.Threading.Tasks;
namespace WalletWasabi.Gui.Tabs
{
@@ -18,15 +19,20 @@ namespace WalletWasabi.Gui.Tabs
public SettingsViewModel() : base("Settings")
{
- _network = Global.Config.Network.Name == "Main" ? "MainNet" : "TestNet";
- _torHost = Global.Config.TorHost;
- _torPort = Global.Config.TorSocks5Port.HasValue
- ? Global.Config.TorSocks5Port.Value.ToString()
- : string.Empty;
+ var config = new Config(Global.Config.FilePath);
- IsModified = false;
this.WhenAnyValue(x => x.Network, x => x.TorHost, x => x.TorPort).Subscribe(x => Save());
- Initialized = true;
+
+ Dispatcher.UIThread.Post(async () =>
+ {
+ await config.LoadFileAsync();
+
+ Network = config.Network.ToString();
+ TorHost = config.TorHost;
+ TorPort = config.TorSocks5Port.ToString();
+
+ IsModified = await Global.Config.CheckFileChangeAsync();
+ });
}
public IEnumerable<string> Networks
@@ -34,7 +40,7 @@ namespace WalletWasabi.Gui.Tabs
get
{
return new[]{
- "MainNet"
+ "Main"
, "TestNet"
, "RegTest"
};
@@ -67,32 +73,43 @@ namespace WalletWasabi.Gui.Tabs
set { this.RaiseAndSetIfChanged(ref _isModified, value); }
}
- public bool Initialized { get; }
-
private void Save()
{
- if (!Initialized) return;
var isValid = string.IsNullOrEmpty(ValidateTorHost()) &&
string.IsNullOrEmpty(ValidateTorPort());
if (!isValid) return;
+ if (string.IsNullOrWhiteSpace(Network)) return;
- IsModified = true;
-
- var config = Global.Config.Clone();
- config.Network = NBitcoin.Network.GetNetwork(_network);
- config.TorHost = _torHost;
- config.TorSocks5Port = int.TryParse(_torPort, out var port) ? (int?)port : null;
+ var config = new Config(Global.Config.FilePath);
Dispatcher.UIThread.Post(async () =>
{
+ await config.LoadFileAsync();
+
+ var network = NBitcoin.Network.GetNetwork(Network);
+ var torHost = TorHost;
+ var torSocks5Port = int.TryParse(TorPort, out var port) ? (int?)port : null;
+
+ if (config.Network != network || config.TorHost != torHost || config.TorSocks5Port != torSocks5Port)
+ {
+ config.Network = network;
+ config.TorHost = torHost;
+ config.TorSocks5Port = torSocks5Port;
+
await config.ToFileAsync();
+
+ IsModified = await Global.Config.CheckFileChangeAsync();
+ }
});
}
public string ValidateTorHost()
{
- if (!string.IsNullOrWhiteSpace(TorHost))
+ if (string.IsNullOrWhiteSpace(TorHost))
{
+ return string.Empty;
+ }
+
var torHost = TorHost.Trim();
if (Uri.TryCreate(torHost, UriKind.Absolute, out var uri))
{
@@ -102,7 +119,6 @@ namespace WalletWasabi.Gui.Tabs
{
return string.Empty;
}
- }
return "Invalid host.";
}

Refactor backend logic