-
Notifications
You must be signed in to change notification settings - Fork 0
/
Service.cs
125 lines (102 loc) · 5.06 KB
/
Service.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace PPTXcreator
{
public class Service
{
public DateTime Datetime { get; set; }
[JsonPropertyName("Voorganger")]
public PastorInfo Pastor { get; set; } = new PastorInfo();
[JsonPropertyName("Collecten")]
public CollectionsInfo Collections { get; set; } = new CollectionsInfo();
public string Organist { get; set; } = "organist";
[JsonIgnore] // static objects are already ignored but this makes it more clear
public static JsonElement[] JsonCache { get; private set; }
/// <summary>
/// Get an array of services from the JSON file at <see cref="Settings.PathServicesJson"/>
/// and save it to <see cref="JsonCache"/>
/// </summary>
public static void UpdateJsonCache()
{
// Load the file contents
if (!Program.TryGetFileContents(Settings.Instance.PathServicesJson, out string fileContent)) return;
// Parse the file contents
try
{
JsonDocumentOptions options = new JsonDocumentOptions()
{
AllowTrailingCommas = true,
CommentHandling = JsonCommentHandling.Skip
};
// Parse the json string and order the JsonElements by the Datetime property
using (JsonDocument document = JsonDocument.Parse(fileContent, options))
{
JsonCache = document.RootElement.EnumerateArray()
.OrderBy(element => element.GetProperty("Datetime").GetDateTime())
.Select(element => element.Clone()).ToArray();
}
}
catch (Exception ex) when (ex is JsonException || ex is InvalidOperationException)
{
Dialogs.GenericWarning($"'{Settings.Instance.PathServicesJson}'" +
$" heeft niet de juiste structuur.\n\nDe volgende foutmelding werd gegeven: {ex.Message}");
}
// Forcing the GC should be avoided, but the previous JsonCache contents refuse to be disposed.
// This method isn't likely to run often anyway.
GC.Collect();
}
/// <summary>
/// Get the service object for the service at <paramref name="dateTime"/>
/// </summary>
public static Service GetCurrent(DateTime dateTime)
{
Service current = new Service();
if (JsonCache == null) return current;
// Get the JsonElement which matches dateTime from JsonCache
JsonElement currentElement = (from JsonElement service in JsonCache
where service.GetProperty("Datetime").GetDateTime() == dateTime
select service).FirstOrDefault();
if (currentElement.ValueKind != JsonValueKind.Undefined)
current = JsonSerializer.Deserialize<Service>(currentElement.GetRawText());
return current;
}
/// <summary>
/// Get the service object for the first service after <paramref name="dateTime"/>
/// </summary>
public static Service GetNext(DateTime dateTime)
{
Service next = new Service();
if (JsonCache == null) return next;
// Get the first JsonElement later than dateTime from JsonCache
JsonElement nextElement = (from JsonElement service in JsonCache
where service.GetProperty("Datetime").GetDateTime() > dateTime
select service).FirstOrDefault(); // JsonCache is ordered
if (nextElement.ValueKind != JsonValueKind.Undefined)
next = JsonSerializer.Deserialize<Service>(nextElement.GetRawText());
return next;
}
public static DateTime GetPreviousDatetime(DateTime current)
{
if (JsonCache == null) return DateTime.MinValue;
List<DateTime> dateTimes = (from JsonElement service in JsonCache
let dateTime = service.GetProperty("Datetime").GetDateTime()
where dateTime < current
select dateTime).ToList();
if (dateTimes.Count == 0) return DateTime.MinValue;
else return dateTimes.Last();
}
public static DateTime GetNextDatetime(DateTime current)
{
if (JsonCache == null) return DateTime.MinValue;
List<DateTime> dateTimes = (from JsonElement service in JsonCache
let dateTime = service.GetProperty("Datetime").GetDateTime()
where dateTime > current
select dateTime).ToList();
if (dateTimes.Count == 0) return DateTime.MinValue;
else return dateTimes.First();
}
}
}