-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSelectionManager.cs
60 lines (54 loc) · 1.98 KB
/
SelectionManager.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
// Filename: SelectionManager.cs
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
public static class SelectionManager
{
private static readonly string jsonFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CodeAggregator", "selections.json");
public static RootSelection LoadSelections()
{
if (File.Exists(jsonFilePath))
{
var json = File.ReadAllText(jsonFilePath);
return JsonConvert.DeserializeObject<RootSelection>(json) ?? new RootSelection();
}
return new RootSelection();
}
public static void SaveSelections(RootSelection rootSelection)
{
var directory = Path.GetDirectoryName(jsonFilePath);
if (directory != null && !Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var json = Newtonsoft.Json.JsonConvert.SerializeObject(rootSelection, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(jsonFilePath, json);
}
public static FolderSelection GetFolderSelection(string sourceFolder)
{
var rootSelection = LoadSelections();
var selection = rootSelection.Folders.Find(f => f.SourceFolder == sourceFolder);
if (selection == null)
{
selection = new FolderSelection { SourceFolder = sourceFolder };
rootSelection.Folders.Add(selection);
SaveSelections(rootSelection);
}
return selection;
}
public static void UpdateFolderSelection(FolderSelection updatedSelection)
{
var rootSelection = LoadSelections();
var index = rootSelection.Folders.FindIndex(f => f.SourceFolder == updatedSelection.SourceFolder);
if (index != -1)
{
rootSelection.Folders[index] = updatedSelection;
}
else
{
rootSelection.Folders.Add(updatedSelection);
}
SaveSelections(rootSelection);
}
}