Skip to content

Commit 45db380

Browse files
committed
move json serializing out of UI code
1 parent d2168a8 commit 45db380

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

ArcExplorer/Models/ApplicationSettings.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using Newtonsoft.Json;
22
using Newtonsoft.Json.Converters;
3+
using System;
34

45
namespace ArcExplorer.Models
56
{
67
public sealed class ApplicationSettings
78
{
8-
public static ApplicationSettings Instance { get; } = FromJson("ApplicationPreferences.json");
9+
private const string preferencesFilePath = "ApplicationPreferences.json";
10+
public static ApplicationSettings Instance { get; } = FromJson(preferencesFilePath);
911

1012
public enum VisualTheme
1113
{
@@ -23,7 +25,6 @@ public enum IntegerDisplayFormat
2325
[JsonConverter(typeof(StringEnumConverter))]
2426
public VisualTheme Theme { get; set; } = VisualTheme.Dark;
2527

26-
2728
[JsonConverter(typeof(StringEnumConverter))]
2829
public IntegerDisplayFormat DisplayFormat { get; set; } = IntegerDisplayFormat.Decimal;
2930

@@ -43,5 +44,11 @@ private static ApplicationSettings FromJson(string path)
4344

4445
return JsonConvert.DeserializeObject<ApplicationSettings>(System.IO.File.ReadAllText(path));
4546
}
47+
48+
internal void SaveToFile()
49+
{
50+
var json = JsonConvert.SerializeObject(this);
51+
System.IO.File.WriteAllText(preferencesFilePath, json);
52+
}
4653
}
4754
}

ArcExplorer/Views/PreferencesWindow.axaml.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ private void PreferencesWindow_Closing(object? sender, System.ComponentModel.Can
3030
e.Cancel = true;
3131
Hide();
3232

33-
// TODO: This is probably not the best place for this.
34-
var json = JsonConvert.SerializeObject(ApplicationSettings.Instance);
35-
System.IO.File.WriteAllText("ApplicationPreferences.json", json);
33+
ApplicationSettings.Instance.SaveToFile();
3634
}
3735

3836
public async void OpenFolderClick()

0 commit comments

Comments
 (0)