-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponseNotification.cs
377 lines (328 loc) · 12.1 KB
/
ResponseNotification.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace LocalNotifications
{
// Delegate for handling notifications
public delegate void NotificationDelegate();
// List of custom ResponseNotification objects with additional events
public class ResponseNotifications : List<ResponseNotification>
{
// Events for when an item is deleted or changed
public event NotificationDelegate ItemDeleted;
public event NotificationDelegate ItemChanged;
protected virtual void OnItemChanged()
{
ItemChanged?.Invoke();
}
protected virtual void OnItemDeleted()
{
ItemDeleted?.Invoke();
}
// Overridden RemoveAt method with event handling for item deletion
public new void RemoveAt(int index)
{
base.RemoveAt(index);
OnItemDeleted();
}
// Custom constructor for ResponseNotifications class
public ResponseNotifications() { }
// Custom constructor that sets the base list with provided data
public ResponseNotifications(List<ResponseNotification> baseList) { SetList(baseList); }
// Overridden Add method with event handling for item addition
public new void Add(string Key, string Val)
{
// Check for duplicate entries and adjust Uid if needed
ResponseNotification tempResponse = dupeCheck(Key, Val);
if (tempResponse != null)
{
tempResponse.PropertyChanged += NotificationResponse_PropertyChanged;
}
base.Add(tempResponse);
}
// Serialize the list to a List<object> format
public List<object> GetList()
{
List<object> list = new List<object>();
foreach (ResponseNotification item in this)
{
list.Add(item.Serialize());
}
return list;
}
// Helper class to wrap DateTime values for notifications
public class TimeWrapper
{
private DateTime dateTime;
public TimeWrapper(DateTime dateTime)
{
this.dateTime = dateTime;
}
public TimeWrapper()
{
this.dateTime = DateTime.MinValue;
}
public DateTime Get()
{
return dateTime;
}
public DateTime Set(DateTime dateTime)
{
this.dateTime = dateTime;
return dateTime;
}
public DateTime SetNow()
{
this.dateTime = DateTime.Now;
return dateTime;
}
}
// Group notifications based on their RepeatInterval property
public Dictionary<RepeatInterval, Tuple<List<ResponseNotification>, TimeWrapper>> GroupByInterval()
{
Dictionary<RepeatInterval, Tuple<List<ResponseNotification>, TimeWrapper>> notificationDictionary = new Dictionary<RepeatInterval, Tuple<List<ResponseNotification>, TimeWrapper>>();
foreach (ResponseNotification notification in this)
{
RepeatInterval interval = notification.NotifyInterval;
if (!notificationDictionary.ContainsKey(interval))
{
notificationDictionary[interval] = Tuple.Create(new List<ResponseNotification> { }, new TimeWrapper());
}
notificationDictionary[interval].Item1.Add(notification);
}
return notificationDictionary;
}
// Clear the list and set it with the provided data
public void SetList(List<ResponseNotification> inList)
{
base.Clear();
foreach (ResponseNotification resNot in inList)
{
resNot.PropertyChanged += NotificationResponse_PropertyChanged;
base.Add(resNot);
}
}
// Event handler for property changes in ResponseNotification objects
private void NotificationResponse_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
OnItemChanged();
}
// Check for duplicate entries and adjust Uid if needed
private ResponseNotification dupeCheck(string Key, string Val)
{
ResponseNotification responseNotification = new ResponseNotification(Key, Val);
if (this.Exists(x => x.Id == responseNotification.Id))
{
int dupeCount = this.Count(x => x.Id == responseNotification.Id);
responseNotification.SetUid(responseNotification.Id + $" ({dupeCount})");
}
return responseNotification;
}
}
// Enumeration for repeat intervals of notifications
public enum RepeatInterval
{
NoRepeat = 0,
Every5Min = 5,
Every15Min = 15,
Every30Min = 30,
Every1Hr = 60
}
// Base class for notification data
public class RootResponseNotification
{
public string Key { get; set; }
public string Val { get; set; }
public string Id { get; set; }
public string Uid { get; set; }
public RepeatInterval NotifyInterval { get; set; } = RepeatInterval.NoRepeat;
public string DisplayName { get; set; }
public string NotificationTitle { get; set; } = "";
public string NotificationMessage { get; set; } = "";
}
// Custom ResponseNotification class inheriting from RootResponseNotification
public class ResponseNotification : RootResponseNotification
{
private string _Key;
private string _Val;
// Overridden Key and Val properties with private backing fields
public new string Key
{
get { return _Key; }
}
public new string Val
{
get { return _Val; }
}
private string _Id;
// Overridden Id property with custom logic for setting the value
public new string Id
{
get { return _Id; }
set
{
if (_Id == null)
{
_Id = $"{Key.Trim()} : {Val}";
}
else
{
_Id = value;
}
}
}
private string _Uid = null;
// Overridden Uid property with custom logic for setting the value
public new string Uid
{
get
{
if (_Uid == null)
{
return Id;
}
else
{
return _Uid;
}
}
}
private string _DisplayName = null;
// Overridden DisplayName property with custom logic for setting the value
public new string DisplayName
{
get
{
if (_DisplayName == null)
{
return Id;
}
else
{
return _DisplayName;
}
}
}
// Calculate the repeat interval in milliseconds for the notification
public int RepeatMs
{
get { return (int)this.NotifyInterval * 60 * 1000; }
}
// Event handler for property changes in ResponseNotification objects
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
// Get the display string for the RepeatInterval property
public string GetIntervalDisplay()
{
switch (this.NotifyInterval)
{
case RepeatInterval.NoRepeat:
return "Does not repeat";
case RepeatInterval.Every5Min:
return "Every 5 Minutes";
case RepeatInterval.Every15Min:
return "Every 15 Minutes";
case RepeatInterval.Every30Min:
return "Every 30 Minutes";
case RepeatInterval.Every1Hr:
return "Every Hour";
default:
return "ERROR";
}
}
// Consume data from the base class and set properties in the derived class
public ResponseNotification ConsumeRoot(RootResponseNotification root)
{
Type parentType = typeof(RootResponseNotification);
Type childType = typeof(ResponseNotification);
// Get all public properties of the parent class
PropertyInfo[] properties = parentType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
// Copy the property values from the parent instance to the child instance
foreach (PropertyInfo property in properties)
{
object value = property.GetValue(root);
PropertyInfo childProperty = childType.GetProperty(property.Name);
if (childProperty != null && childProperty.CanWrite)
{
// The property has a public setter, so set the value directly
childProperty.SetValue(this, value);
}
else
{
// The property doesn't have a public setter, so try to set the value through a private property
FieldInfo privateField = childType.GetField("_" + property.Name, BindingFlags.NonPublic | BindingFlags.Instance);
if (privateField != null)
{
privateField.SetValue(this, value);
}
}
}
return this;
}
// Serialize the ResponseNotification object to the base class format
public RootResponseNotification Serialize()
{
return this;
}
// Default constructor
public ResponseNotification() { }
// Constructor with Key and Val parameters
public ResponseNotification(string Key, string Val)
{
this._Key = Key;
this._Val = Val;
this.Id = $"{Key.Trim()} : {Val}";
}
// Set the NotifyInterval property and trigger property changed event
public void SetInterval(RepeatInterval interval)
{
this.NotifyInterval = interval;
OnPropertyChanged(nameof(NotifyInterval));
}
public void SetFields(string displayName = "", string notificationTitle = "", string notificationMessage = "")
{
if (displayName != "")
{
SetDisplayName(displayName);
}
if (notificationTitle != "")
{
SetNotificationTitle(notificationTitle);
}
if (notificationMessage != "")
{
SetNotificationMessage(notificationMessage);
}
OnPropertyChanged("NotifyFields");
}
// Set the DisplayName property and trigger property changed event
private void SetDisplayName(string Name)
{
this._DisplayName = Name;
//OnPropertyChanged(nameof(DisplayName));
}
// Set the NotificationTitle property and trigger property changed event
private void SetNotificationTitle(string Title)
{
this.NotificationTitle = Title;
//OnPropertyChanged(nameof(NotificationTitle));
}
// Set the NotificationMessage property and trigger property changed event
private void SetNotificationMessage(string Message)
{
this.NotificationMessage = Message;
//OnPropertyChanged(nameof(NotificationMessage));
}
// Set the Uid property and trigger property changed event
public void SetUid(string Uid)
{
this._Uid = Uid;
OnPropertyChanged(nameof(_Uid));
}
}
}