Skip to content

Commit 791b429

Browse files
committed
Small improvements.
1 parent 3099417 commit 791b429

File tree

16 files changed

+68
-61
lines changed

16 files changed

+68
-61
lines changed

bin/MataSharp.XML

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/MataSharp.dll

1.5 KB
Binary file not shown.

src/MataSharp/MagisterMessage.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public MagisterMessage CreateForwardMessage()
143143
IDKey = this.IDKey,
144144
IDOrginalReceiver = null,
145145
IDOriginal = null,
146-
Body = "<b>Van:</b> " + this.Sender.Name + "<br><b>Verzonden:</b> " + this.SentDate.DayOfWeekDutch() + " " + this.SentDate.ToString() + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
146+
Body = "<b>Van:</b> " + this.Sender.Description + "<br><b>Verzonden:</b> " + this.SentDate.DayOfWeekDutch() + " " + this.SentDate.ToString() + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
147147
Deleted = false,
148148
_IsRead = true,
149149
Subject = tmpSubject,
@@ -175,7 +175,7 @@ public MagisterMessage CreateForwardMessage(string ContentAdd)
175175
IDKey = this.IDKey,
176176
IDOrginalReceiver = null,
177177
IDOriginal = null,
178-
Body = ContentAdd + "<br><br>---------------<br><b>Van:</b> " + this.Sender.Name + "<br><b>Verzonden:</b> " + this.SentDate.DayOfWeekDutch() + " " + this.SentDate.ToString() + "<br><b>Aan:</b> " + String.Join(", ",this.Recipients.Select(x=>x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
178+
Body = ContentAdd + "<br><br>---------------<br><b>Van:</b> " + this.Sender.Description + "<br><b>Verzonden:</b> " + this.SentDate.DayOfWeekDutch() + " " + this.SentDate.ToString() + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
179179
Deleted = false,
180180
_IsRead = true,
181181
Subject = tmpSubject,
@@ -212,7 +212,7 @@ public MagisterMessage CreateReplyToAllMessage(string ContentAdd)
212212
IDKey = this.IDKey,
213213
IDOrginalReceiver = null,
214214
IDOriginal = null,
215-
Body = ContentAdd + "<br><br>---------------<br><b>Van:</b> " + this.Sender.Name + "<br><b>Verzonden:</b> " + this.SentDate.DayOfWeekDutch() + " " + this.SentDate.ToString() + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
215+
Body = ContentAdd + "<br><br>---------------<br><b>Van:</b> " + this.Sender.Description + "<br><b>Verzonden:</b> " + this.SentDate.DayOfWeekDutch() + " " + this.SentDate.ToString() + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
216216
Deleted = false,
217217
_IsRead = true,
218218
Subject = tmpSubject,
@@ -245,7 +245,7 @@ public MagisterMessage CreateReplyMessage(string ContentAdd)
245245
IDKey = this.IDKey,
246246
IDOrginalReceiver = null,
247247
IDOriginal = null,
248-
Body = ContentAdd + "<br><br>---------------<br><b>Van:</b> " + this.Sender.Name + "<br><b>Verzonden:</b> " + this.SentDate.DayOfWeekDutch() + " " + this.SentDate.ToString() + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
248+
Body = ContentAdd + "<br><br>---------------<br><b>Van:</b> " + this.Sender.Description + "<br><b>Verzonden:</b> " + this.SentDate.DayOfWeekDutch() + " " + this.SentDate.ToString() + "<br><b>Aan:</b> " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "<br><b>Onderwerp:</b> " + this.Subject + "<br><br>\"" + this.Body + "\"<br><br>",
249249
Deleted = false,
250250
_IsRead = true,
251251
Subject = tmpSubject,
@@ -330,7 +330,7 @@ internal MagisterStyleMessage ToMagisterStyle()
330330

331331
public override string ToString()
332332
{
333-
return "From: " + this.Sender.Name + "\nSent: " + this.SentDate.DayOfWeek + " " + this.SentDate.ToString() + "\nTo: " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "\nSubject: " + this.Subject + "\n\n\"" + this.Body + "\"";
333+
return "From: " + this.Sender.Description + "\nSent: " + this.SentDate.DayOfWeek + " " + this.SentDate.ToString() + "\nTo: " + String.Join(", ", this.Recipients.Select(x => x.Name)) + "\nSubject: " + this.Subject + "\n\n\"" + this.Body + "\"";
334334
}
335335

336336
public int CompareTo(MagisterMessage other)

src/MataSharp/MagisterPerson.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace MataSharp
88
{
9-
public partial class MagisterPerson : IComparable<MagisterPerson>, ICloneable
9+
public partial class MagisterPerson : IComparable<MagisterPerson>, ICloneable, IEqualityComparer<MagisterPerson>
1010
{
1111
public uint ID { get; set; }
1212
public object Ref { get; set; } // Even Schoolmaster doesn't know what this is, it's mysterious. Just keep it in case.
@@ -79,6 +79,16 @@ object ICloneable.Clone()
7979
{
8080
return this.Clone();
8181
}
82+
83+
public bool Equals(MagisterPerson x, MagisterPerson y)
84+
{
85+
return x.Equals(y);
86+
}
87+
88+
public int GetHashCode(MagisterPerson obj)
89+
{
90+
return obj.Original.GetHashCode();
91+
}
8292
}
8393

8494
internal partial struct MagisterStylePerson
@@ -155,7 +165,7 @@ public MagisterPerson ToPerson(bool download)
155165
FirstName = tmpFirstName,
156166
NamePrefix = tmpPrefix,
157167
Name = tmpName,
158-
Description = tmpPerson.Omschrijving,
168+
Description = tmpPerson.Omschrijving ?? tmpName,
159169
Group = tmpPerson.Groep,
160170
TeacherCode = tmpPerson.DocentCode,
161171
GroupID = tmpPerson.Type,

src/MataSharp/MagisterSchool.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public class MagisterSchool : IComparable<MagisterSchool>, ICloneable
1717
public string URL { get; set; }
1818

1919
/// <summary>
20-
/// Returns all Magister/Mata schools filterd by the given search filter as a list.
20+
/// Returns all Magister/Mata schools filtered by the given search filter as a list.
2121
/// </summary>
2222
/// <param name="SearchFilter">The search filter to use as string.</param>
2323
/// <returns>List containing MagisterSchool instances</returns>
2424
public static List<MagisterSchool> GetSchools(string SearchFilter)
2525
{
26-
if (string.IsNullOrWhiteSpace(SearchFilter) || SearchFilter.Count() < 3) return new List<MagisterSchool>();
26+
if (string.IsNullOrWhiteSpace(SearchFilter) || SearchFilter.Length < 3) return new List<MagisterSchool>();
2727

2828
string URL = "https://schoolkiezer.magister.net/home/query?filter=" + SearchFilter;
2929

src/MataSharp/Mata.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ public List<MagisterMessageFolder> GetMessageFolders()
112112
List<MagisterMessageFolder> tmplst = new List<MagisterMessageFolder>();
113113
foreach (var MessageFolder in MessageFolders)
114114
{
115-
var tmpType = (MessageFolder)MessageFolder.Id;
116-
117115
tmplst.Add(new MagisterMessageFolder()
118116
{
119117
Name = MessageFolder.Naam,
@@ -123,7 +121,7 @@ public List<MagisterMessageFolder> GetMessageFolders()
123121
Ref = MessageFolder.Ref,
124122
MessagesURI = MessageFolder.BerichtenUri,
125123
Mata = this,
126-
FolderType = tmpType
124+
FolderType = (MessageFolder)MessageFolder.Id
127125
});
128126
}
129127
return tmplst;
@@ -136,7 +134,7 @@ public List<MagisterMessageFolder> GetMessageFolders()
136134
/// <returns>List containing MagisterPerson instances</returns>
137135
public List<MagisterPerson> GetPersons(string SearchFilter)
138136
{
139-
if (string.IsNullOrWhiteSpace(SearchFilter) || SearchFilter.Count() < 3) return new List<MagisterPerson>();
137+
if (string.IsNullOrWhiteSpace(SearchFilter) || SearchFilter.Length < 3) return new List<MagisterPerson>();
140138

141139
if (!_Session.checkedPersons.ContainsKey(SearchFilter))
142140
{

src/MataSharp/MessageList.cs

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ public void RemoveAt(int index)
7474
/// <param name="count">The number of elements to remove.</param>
7575
public void RemoveRange(int index, int count)
7676
{
77-
var enumerator = this.GetSpecificEnumerator();
78-
79-
for (int i = 0; i < count; i++)
80-
enumerator.GetAt(index + i).Delete();
77+
this.GetSpecificEnumerator().GetRange(count, index).ForEach(m => m.Delete());
8178
}
8279

8380
/// <summary>
@@ -88,13 +85,7 @@ public void RemoveRange(int index, int count)
8885
/// <returns>The given range of MagisterMessages as a List</returns>
8986
public List<Message> GetRange(int index, int count)
9087
{
91-
var tmpList = new List<Message>();
92-
var enumerator = this.GetSpecificEnumerator();
93-
94-
for (int i = 0; i < count; i++)
95-
tmpList.Add(enumerator.GetAt(index + i));
96-
97-
return tmpList;
88+
return this.GetSpecificEnumerator().GetRange(count, index);
9889
}
9990

10091
/// <summary>
@@ -123,12 +114,7 @@ public int IndexOf(Message item)
123114
/// <param name="predicate">The predicate the messages must match to.</param>
124115
public void RemoveAll(int max, Predicate<Message> predicate)
125116
{
126-
var enumerator = this.GetSpecificEnumerator();
127-
for(int i = 0; i < max; i++)
128-
{
129-
var message = enumerator.GetAt(i);
130-
if (predicate(message)) message.Delete();
131-
}
117+
this.GetSpecificEnumerator().GetRange(max, 0).Where(m => predicate(m)).ToList().ForEach(m => m.Delete());
132118
}
133119

134120
/// <summary>
@@ -139,14 +125,7 @@ public void RemoveAll(int max, Predicate<Message> predicate)
139125
/// <returns>A List containing the messages that matched the predicate.</returns>
140126
public List<Message> Where(int max, Func<Message,bool> predicate)
141127
{
142-
var enumerator = this.GetSpecificEnumerator();
143-
var tmpList = new List<Message>();
144-
for(int i = 0; i < max; i++)
145-
{
146-
var msg = enumerator.GetAt(i);
147-
if (predicate(msg)) tmpList.Add((Message)msg);
148-
}
149-
return tmpList;
128+
return this.GetSpecificEnumerator().GetRange(max, 0).Where(m => predicate(m)).ToList();
150129
}
151130

152131
/// <summary>
@@ -174,15 +153,7 @@ public Message First(int max, Func<Message,bool> predicate)
174153
/// <returns>The last message on the server that matches the predicate.</returns>
175154
public Message Last(int max, Func<Message,bool> predicate)
176155
{
177-
var enumerator = this.GetSpecificEnumerator();
178-
Message msg = null;
179-
for(int i = 0; i < max; i++)
180-
{
181-
var tmpMsg = enumerator.GetAt(i);
182-
if (predicate(tmpMsg)) msg = tmpMsg;
183-
}
184-
if (msg != null) return msg;
185-
else throw new Exception("No messages found.");
156+
return this.GetSpecificEnumerator().GetRange(max, 0).Last(m => predicate(m));
186157
}
187158

188159
/// <summary>
@@ -242,12 +213,13 @@ public bool Any(int max, Func<Message, bool> predicate)
242213
/// Returns the single message on the server that matches the given predicate. Throws expception when more matching messages or none are found.
243214
/// </summary>
244215
/// <param name="predicate">The predicate that the message must match to.</param>
216+
/// <param name="max">The max ammount of messages to get from the server.</param>
245217
/// <returns>A single MagisterMessage matching the given predicate.</returns>
246-
public Message Single(Func<Message, bool> predicate)
218+
public Message Single(int max, Func<Message, bool> predicate)
247219
{
248220
var enumerator = this.GetSpecificEnumerator();
249221
Message msg = null;
250-
for(int i = 0; i <= 5; i++)
222+
for(int i = 0; i <= max; i++)
251223
{
252224
var tmpMsg = enumerator.GetAt(i);
253225
if (predicate(tmpMsg))
@@ -264,13 +236,14 @@ public Message Single(Func<Message, bool> predicate)
264236
/// Returns the single message on the server that matches the given predicate. Throws expception when more matching messages are found.
265237
/// When no matching messages are found, returns the default value.
266238
/// </summary>
239+
/// <param name="max">The max ammount of messages to get from the server.</param>
267240
/// <param name="predicate">The predicate that the message must match to.</param>
268241
/// <returns>A single MagisterMessage matching the given predicate.</returns>
269-
public Message SingleOrDefault(Func<Message, bool> predicate)
242+
public Message SingleOrDefault(int max, Func<Message, bool> predicate)
270243
{
271244
var enumerator = this.GetSpecificEnumerator();
272245
Message msg = null;
273-
for (int i = 0; i <= 5; i++)
246+
for (int i = 0; i <= max; i++)
274247
{
275248
var tmpMsg = enumerator.GetAt(i);
276249
if (predicate(tmpMsg))
@@ -283,7 +256,7 @@ public Message SingleOrDefault(Func<Message, bool> predicate)
283256
else return default(Message);
284257
}
285258

286-
private class Enumerator<T> : IEnumerator<T> where T : MagisterMessage
259+
private class Enumerator<T> : IEnumerator<T>, IDisposable where T : MagisterMessage
287260
{
288261
private int Next = 0;
289262
private int Skip = -1;
@@ -326,6 +299,24 @@ public T GetAt(int index)
326299
return (T)MessageClean.ToMagisterMessage();
327300
}
328301

302+
public List<T> GetRange(int Ammount, int Skip)
303+
{
304+
string URL = "https://" + Mata.School.URL + "/api/personen/" + this.Mata.UserID + "/communicatie/berichten/mappen/" + Sender.ID + "/berichten?$skip=" + Skip + "&$top=" + Ammount;
305+
306+
string CompactMessagesRAW = _Session.HttpClient.DownloadString(URL);
307+
var CompactMessages = JsonConvert.DeserializeObject<MagisterStyleMessageFolder>(CompactMessagesRAW);
308+
309+
var list = new List<T>();
310+
foreach (var CompactMessage in CompactMessages.Items)
311+
{
312+
URL = "https://" + Mata.School.URL + "/api/personen/" + this.Mata.UserID + "/communicatie/berichten/mappen/" + Sender.ID + "/berichten/" + CompactMessage.Id;
313+
string MessageRAW = _Session.HttpClient.DownloadString(URL);
314+
var MessageClean = JsonConvert.DeserializeObject<MagisterStyleMessage>(MessageRAW);
315+
list.Add((T)MessageClean.ToMagisterMessage());
316+
}
317+
return list;
318+
}
319+
329320
public List<T> GetUnread(uint Ammount, uint Skip = 0)
330321
{
331322
string URL = "https://" + Mata.School.URL + "/api/personen/" + this.Mata.UserID + "/communicatie/berichten/mappen/" + Sender.ID + "/berichten?$skip=" + Skip + "&$top=" + Ammount;
@@ -384,8 +375,10 @@ public void Reset()
384375
Skip = -1;
385376
}
386377

378+
~Enumerator() { this.Dispose(); }
387379
public void Dispose()
388380
{
381+
this.Reset();
389382
this.Mata = null;
390383
this.Sender = null;
391384
GC.Collect();

src/MataSharp/bin/Release/MataSharp.XML

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1.5 KB
Binary file not shown.
4 KB
Binary file not shown.

0 commit comments

Comments
 (0)