@@ -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 ( ) ;
0 commit comments