-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSettings.cs
65 lines (52 loc) · 1.64 KB
/
Settings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.Json;
namespace DesktopClock
{
public class Settings
{
private const string FOLDER_NAME = "DesktopClock";
private const string FILE_NAME = "settings.json";
private static readonly string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), FOLDER_NAME);
private static readonly string filePath = Path.Combine(folderPath, FILE_NAME);
public int ScreenIndex { get; set; } = 0;
public ClockPosition ClockPosition { get; set; } = ClockPosition.BottomRight;
public Settings() { }
public static Settings Load()
{
// Load file if it exists
if (File.Exists(filePath))
return JsonSerializer.Deserialize<Settings>(File.ReadAllText(filePath)) ?? new Settings();
return new Settings();
}
public void Save()
{
// Make sure the directory exists
Directory.CreateDirectory(folderPath);
// Write to file
File.WriteAllText(filePath, JsonSerializer.Serialize(this));
}
}
public enum ClockPosition
{
[Description("Top Left")]
TopLeft = 0,
[Description("Top Right")]
TopRight = 1,
[Description("Bottom Right")]
BottomRight = 2,
[Description("Bottom Left")]
BottomLeft = 3
}
public static class ClockPositionExtension
{
public static string GetDescription(this ClockPosition clockPosition)
{
var descriptionAttributes = typeof(ClockPosition).GetField(clockPosition.ToString())?.GetCustomAttributes<DescriptionAttribute>(false);
return descriptionAttributes?.FirstOrDefault()?.Description ?? clockPosition.ToString();
}
}
}