Skip to content

Commit

Permalink
move json serializing out of UI code
Browse files Browse the repository at this point in the history
  • Loading branch information
ScanMountGoat committed Jan 9, 2021
1 parent d2168a8 commit 45db380
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 9 additions & 2 deletions ArcExplorer/Models/ApplicationSettings.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;

namespace ArcExplorer.Models
{
public sealed class ApplicationSettings
{
public static ApplicationSettings Instance { get; } = FromJson("ApplicationPreferences.json");
private const string preferencesFilePath = "ApplicationPreferences.json";
public static ApplicationSettings Instance { get; } = FromJson(preferencesFilePath);

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


[JsonConverter(typeof(StringEnumConverter))]
public IntegerDisplayFormat DisplayFormat { get; set; } = IntegerDisplayFormat.Decimal;

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

return JsonConvert.DeserializeObject<ApplicationSettings>(System.IO.File.ReadAllText(path));
}

internal void SaveToFile()
{
var json = JsonConvert.SerializeObject(this);
System.IO.File.WriteAllText(preferencesFilePath, json);
}
}
}
4 changes: 1 addition & 3 deletions ArcExplorer/Views/PreferencesWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ private void PreferencesWindow_Closing(object? sender, System.ComponentModel.Can
e.Cancel = true;
Hide();

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

public async void OpenFolderClick()
Expand Down

0 comments on commit 45db380

Please sign in to comment.