-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppSettings.cs
More file actions
75 lines (67 loc) · 2.34 KB
/
Copy pathAppSettings.cs
File metadata and controls
75 lines (67 loc) · 2.34 KB
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
66
67
68
69
70
71
72
73
74
75
using System.IO;
using System.Text.Json;
namespace DeadDailyDose;
/// <summary>
/// Persists API key and optional last show to a JSON file in AppData.
/// </summary>
public static class AppSettings
{
private static readonly JsonSerializerOptions Options = new() { WriteIndented = true };
private static string SettingsPath =>
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"DeadDailyDose",
"settings.json");
/// <summary>Setlist.fm API key (empty until user sets it).</summary>
public static string SetlistFmApiKey
{
get => Load().SetlistFmApiKey;
set { var s = Load(); s.SetlistFmApiKey = value ?? string.Empty; Save(s); }
}
/// <summary>Last loaded show identifier for optional persistence.</summary>
public static string LastShowIdentifier
{
get => Load().LastShowIdentifier;
set { var s = Load(); s.LastShowIdentifier = value ?? string.Empty; Save(s); }
}
/// <summary>Last selected artist name for persistence.</summary>
public static string LastArtistName
{
get => Load().LastArtistName;
set { var s = Load(); s.LastArtistName = value ?? string.Empty; Save(s); }
}
private static SettingsData Load()
{
try
{
var dir = Path.GetDirectoryName(SettingsPath);
if (!string.IsNullOrEmpty(dir) && Directory.Exists(dir))
{
var json = File.ReadAllText(SettingsPath);
var data = JsonSerializer.Deserialize<SettingsData>(json);
if (data != null) return data;
}
}
catch { /* use defaults */ }
return new SettingsData();
}
private static void Save(SettingsData data)
{
try
{
var dir = Path.GetDirectoryName(SettingsPath);
if (!string.IsNullOrEmpty(dir))
{
Directory.CreateDirectory(dir);
File.WriteAllText(SettingsPath, JsonSerializer.Serialize(data, Options));
}
}
catch { /* ignore */ }
}
private class SettingsData
{
public string SetlistFmApiKey { get; set; } = string.Empty;
public string LastShowIdentifier { get; set; } = string.Empty;
public string LastArtistName { get; set; } = string.Empty;
}
}