-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportUserNameStore.cs
More file actions
87 lines (76 loc) · 2.75 KB
/
ImportUserNameStore.cs
File metadata and controls
87 lines (76 loc) · 2.75 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
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
namespace TimeTask
{
public class ImportUserNameStore
{
private readonly string _filePath;
public ImportUserNameStore()
{
string appDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "TimeTask");
Directory.CreateDirectory(appDataPath);
_filePath = Path.Combine(appDataPath, "import_user_profile.json");
}
public List<string> GetAliases()
{
var profile = Load();
return profile?.Aliases?.Where(a => !string.IsNullOrWhiteSpace(a)).Distinct(StringComparer.OrdinalIgnoreCase).ToList()
?? new List<string>();
}
public List<string> GetKnownNames()
{
var profile = Load();
return profile?.KnownNames?.Where(a => !string.IsNullOrWhiteSpace(a)).Distinct(StringComparer.OrdinalIgnoreCase).ToList()
?? new List<string>();
}
public void SaveAliases(List<string> aliases, bool remember)
{
if (!remember) return;
var clean = aliases?.Where(a => !string.IsNullOrWhiteSpace(a)).Select(a => a.Trim()).Distinct(StringComparer.OrdinalIgnoreCase).ToList()
?? new List<string>();
var profile = Load() ?? new ImportUserNameProfile();
profile.Aliases = clean;
foreach (var name in clean)
{
if (!profile.KnownNames.Contains(name, StringComparer.OrdinalIgnoreCase))
{
profile.KnownNames.Add(name);
}
}
Save(profile);
}
private ImportUserNameProfile Load()
{
try
{
if (!File.Exists(_filePath)) return new ImportUserNameProfile();
string json = File.ReadAllText(_filePath);
var profile = JsonSerializer.Deserialize<ImportUserNameProfile>(json);
return profile ?? new ImportUserNameProfile();
}
catch
{
return new ImportUserNameProfile();
}
}
private void Save(ImportUserNameProfile profile)
{
try
{
var options = new JsonSerializerOptions { WriteIndented = true };
File.WriteAllText(_filePath, JsonSerializer.Serialize(profile, options));
}
catch
{
}
}
}
public class ImportUserNameProfile
{
public List<string> Aliases { get; set; } = new List<string>();
public List<string> KnownNames { get; set; } = new List<string>();
}
}