Skip to content

Commit

Permalink
The settings loading method is divided into synchronous and asynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
TTLC198 committed Dec 7, 2023
1 parent ad1a326 commit b79dc78
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
25 changes: 24 additions & 1 deletion HSMonitor/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,33 @@ public async Task Reset()
SettingsReset?.Invoke(this, EventArgs.Empty);
}

public async Task Load()
public void Load()
{
try
{
if (!Directory.Exists(App.SettingsDirPath))
Directory.CreateDirectory(App.SettingsDirPath);
if (!File.Exists(ConfigurationPath))
Settings.JsonToFile(ConfigurationPath);

var json = File.ReadAllText(ConfigurationPath);
Settings = JsonSerializer.Deserialize<ApplicationSettings>(json) ?? throw new InvalidOperationException();
Settings.IsAutoStartEnabled = _autoStartSwitch.IsSet;
Settings.LastSelectedPort ??= SerialPort.GetPortNames().FirstOrDefault() ?? "COM1";
SettingsLoaded?.Invoke(this, EventArgs.Empty);
}
catch (Exception exception)
{
_logger.Error(exception);
}
}

public async Task LoadAsync()
{
try
{
if (!Directory.Exists(App.SettingsDirPath))
Directory.CreateDirectory(App.SettingsDirPath);
if (!File.Exists(ConfigurationPath))
Settings.JsonToFile(ConfigurationPath);

Expand Down
2 changes: 1 addition & 1 deletion HSMonitor/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public async void OnViewFullyLoaded()

public async void ShowSettings()
{
await _settingsService.Load();
await _settingsService.LoadAsync();
await _dialogManager.ShowDialogAsync(_viewModelFactory.CreateSettingsViewModel());
}

Expand Down
2 changes: 1 addition & 1 deletion HSMonitor/ViewModels/Settings/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async void Save()

public async void Cancel()
{
await _settingsService.Load();
await _settingsService.LoadAsync();
Close(false);
}
}

0 comments on commit b79dc78

Please sign in to comment.