Skip to content
This repository was archived by the owner on Jul 19, 2021. It is now read-only.

Commit ce1d92d

Browse files
author
Maximilian Krauss
committed
Persistent setting storage
1 parent bd3f933 commit ce1d92d

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

Winfy.Core/JsonPersister.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.IO;
3+
using System.Timers;
4+
5+
namespace Winfy.Core {
6+
public sealed class JsonPersister<T> : IDisposable where T : class {
7+
8+
private T _Instance;
9+
private readonly Timer _Persistor;
10+
private readonly object _SyncLock;
11+
12+
public JsonPersister(string path) {
13+
Path = path;
14+
_SyncLock = new object();
15+
_Persistor = new Timer(new TimeSpan(0, 0, 1, 0).TotalMilliseconds) {AutoReset = true, Enabled = true};
16+
_Persistor.Elapsed += (o, e) => Persist();
17+
}
18+
19+
public string Path { get; private set; }
20+
21+
public T Instance {
22+
get {
23+
return _Instance ?? (_Instance = File.Exists(Path)
24+
? Serializer.DeserializeFromJson<T>(Path)
25+
: Activator.CreateInstance<T>());
26+
}
27+
}
28+
29+
public void Persist() {
30+
lock (_SyncLock) {
31+
Serializer.SerializeToJson(_Instance, Path);
32+
}
33+
}
34+
35+
public void Dispose() {
36+
_Persistor.Dispose();
37+
Persist();
38+
}
39+
}
40+
}

Winfy.Core/Serializer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public static T DeserializeFromXml<T>(byte[] data) {
3535
}
3636

3737
public static void SerializeToJson<T>(T instance, string filename) {
38+
if(string.IsNullOrEmpty(filename))
39+
throw new ArgumentException("filename");
40+
41+
if (!Directory.Exists(Path.GetDirectoryName(filename)))
42+
Directory.CreateDirectory(Path.GetDirectoryName(filename));
43+
3844
File.WriteAllBytes(filename, SerializeToJson(instance));
3945
}
4046
public static byte[] SerializeToJson<T>(T instance) {

Winfy.Core/Winfy.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
<Compile Include="ILog.cs" />
8080
<Compile Include="ISpotifyController.cs" />
8181
<Compile Include="IUsageTrackerService.cs" />
82+
<Compile Include="JsonPersister.cs" />
8283
<Compile Include="LastFmPOCOs.cs" />
8384
<Compile Include="LocalUsageTrackerService.cs" />
8485
<Compile Include="ProductionLogger.cs" />

Winfy/AppBootstrapper.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public sealed class AppBootstrapper : TinyBootstrapper<ShellViewModel> {
1515

1616
private AppSettings _Settings;
1717
private AppContracts _Contracts;
18-
private string _SettingsPath;
18+
private JsonPersister<AppSettings> _SettingsPersistor;
1919

2020
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e) {
2121
base.OnStartup(sender, e);
@@ -29,12 +29,11 @@ protected override void Configure() {
2929
base.Configure();
3030

3131
_Contracts = new AppContracts();
32-
_SettingsPath = Path.Combine(_Contracts.SettingsLocation, _Contracts.SettingsFilename);
33-
_Settings = File.Exists(_SettingsPath)
34-
? Serializer.DeserializeFromJson<AppSettings>(_SettingsPath)
35-
: new AppSettings();
3632

3733

34+
_SettingsPersistor = new JsonPersister<AppSettings>(Path.Combine(_Contracts.SettingsLocation, _Contracts.SettingsFilename));
35+
_Settings = _SettingsPersistor.Instance;
36+
3837
Container.Register<AppContracts>(_Contracts);
3938
Container.Register<AppSettings>(_Settings);
4039
Container.Register<Core.ILog>(new ProductionLogger());
@@ -53,7 +52,7 @@ protected override void Configure() {
5352

5453
protected override void OnExit(object sender, EventArgs e) {
5554
base.OnExit(sender, e);
56-
Serializer.SerializeToJson(_Settings, _SettingsPath);
55+
_SettingsPersistor.Dispose();
5756
}
5857
}
5958
}

Winfy/changelog.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "yay 2.0",
55
"changes": [
66
"Major version jump to 2.0. Chrome style, whoop whoop :)",
7-
"New: Winfy now utilizes the Spotify local API for better cover image support"
7+
"New: Winfy utilizes the Spotify local API for better cover image support",
8+
"Improved: Auto-save for app settings every minute"
89
]
910
},
1011
{

0 commit comments

Comments
 (0)