Skip to content

Commit

Permalink
Load settings synchronized and save asynchronized.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyfish committed Jul 3, 2024
1 parent 1561253 commit c76d14e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
4 changes: 2 additions & 2 deletions App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public override void Initialize()
AvaloniaXamlLoader.Load(this);
}

public override async void OnFrameworkInitializationCompleted()
public override void OnFrameworkInitializationCompleted()
{
await AppSettings.LoadSettings();
AppSettings.Load();
if (Settings.Locale == "")
Settings.Locale = CultureInfo.CurrentCulture.Name;

Expand Down
23 changes: 19 additions & 4 deletions AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,34 @@ public class AppSettings

private static readonly JsonFile<AppSettings> _settingsFile = new(SettingsFile);

public static async Task LoadSettings()
public static void Load()
{
var settings = await _settingsFile.Load();
var settings = _settingsFile.Load();
if (settings != null)
Settings = settings;
}

public static async Task SaveSettings()
public static async Task LoadAsync()
{
var settings = await _settingsFile.LoadAsync();
if (settings != null)
Settings = settings;
}

public static void Save()
{
if (Design.IsDesignMode)
return;

_settingsFile.Save(Settings);
}

public static async Task SaveAsync()
{
if (Design.IsDesignMode)
return;

await _settingsFile.Save(Settings);
await _settingsFile.SaveAsync(Settings);
}

public string TriggerKey { get; set; } = "Ctrl";
Expand Down
2 changes: 1 addition & 1 deletion MainWindow/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ private async Task CheckAndSaveSettings()
Settings.StratagemSets.Add(GetKeyStratagemString());
foreach (var item in StratagemSets)
Settings.StratagemSets.Add(item);
await AppSettings.SaveSettings();
await AppSettings.SaveAsync();

SettingsChanged = false;
}
Expand Down
23 changes: 21 additions & 2 deletions Tools/JsonFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ public JsonFile(string filePath)
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
};

public async Task<T?> Load()
public T? Load()
{
if (!File.Exists(FilePath))
return null;

using var fileStream = File.OpenRead(FilePath);
return JsonSerializer.Deserialize<T>(fileStream, JsonSerializerOptions);
}

public async Task<T?> LoadAsync()
{
if (!File.Exists(FilePath))
return null;
Expand All @@ -28,7 +37,17 @@ public JsonFile(string filePath)
return await JsonSerializer.DeserializeAsync<T>(fileStream, JsonSerializerOptions);
}

public async Task Save(T obj)
public void Save(T obj)
{
var dir = Path.GetDirectoryName(FilePath);
if (dir != null && !Directory.Exists(dir))
Directory.CreateDirectory(dir);

using var fileStream = File.Create(FilePath);
JsonSerializer.Serialize(fileStream, obj, JsonSerializerOptions);
}

public async Task SaveAsync(T obj)
{
var dir = Path.GetDirectoryName(FilePath);
if (dir != null && !Directory.Exists(dir))
Expand Down

0 comments on commit c76d14e

Please sign in to comment.