-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathRangeObservableCollection.cs
More file actions
418 lines (339 loc) · 12.2 KB
/
Copy pathRangeObservableCollection.cs
File metadata and controls
418 lines (339 loc) · 12.2 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//
// Copyright (c) Fela Ameghino 2015-2026
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
namespace Telegram.Collections
{
// Named for the diff collection when it was the only one; SelectedItemsBinder type-checks it,
// so it belongs to whichever type actually carries ReplaceWithT.
public interface IRangeObservableCollection : IList
{
void ReplaceWithT(IEnumerable collection);
}
public partial class RangeObservableCollection<T>
: SuppressObservableCollection<T>
, IRangeObservableCollection
, IList<T>
{
public RangeObservableCollection()
{
}
public RangeObservableCollection(IEnumerable<T> collection)
: base(collection)
{
}
public RangeObservableCollection(List<T> list)
: base(list)
{
}
public virtual void AddRange(IEnumerable<T> collection)
{
AddRangeInternal(collection);
}
public virtual void InsertRange(int startIndex, IEnumerable<T> collection)
{
if (startIndex < 0 || startIndex > Count)
{
throw new ArgumentOutOfRangeException(nameof(startIndex));
}
AddRangeInternal(collection, startIndex);
}
public virtual void RemoveRange(IEnumerable<T> collection)
{
if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}
CheckReentrancy();
var changedItems = new List<T>(collection);
for (var i = 0; i < changedItems.Count; i++)
{
if (!Items.Remove(changedItems[i]))
{
changedItems.RemoveAt(i);
i--;
}
}
if (changedItems.Count == 0)
{
return;
}
RaiseChangeNotificationEvents(NotifyCollectionChangedAction.Remove, changedItems);
}
public virtual void RemoveRange(int startIndex, int count)
{
if (startIndex < 0 || startIndex >= Count)
{
throw new ArgumentOutOfRangeException(nameof(startIndex));
}
if (startIndex + count > Count)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
CheckReentrancy();
var changedItems = new List<T>(count);
for (int i = startIndex; i < startIndex + count; i++)
{
changedItems.Add(Items[i]);
}
RemoveRangeInternal(startIndex, count, changedItems);
}
public virtual void ReplaceRange(IEnumerable<T> collection)
{
if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}
CheckReentrancy();
var previouslyEmpty = Items.Count == 0;
Items.Clear();
AddArrangeCore(collection as List<T> ?? new List<T>(collection));
var currentlyEmpty = Items.Count == 0;
if (previouslyEmpty && currentlyEmpty)
{
return;
}
RaiseChangeNotificationEvents(NotifyCollectionChangedAction.Reset);
}
protected virtual void AddRangeInternal(IEnumerable<T> collection, int startIndex = -1)
{
if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}
CheckReentrancy();
var eventStartIndex = startIndex < 0 ? Items.Count : startIndex;
var changedItems = collection as List<T> ?? new List<T>(collection);
if (!AddArrangeCore(changedItems, startIndex))
{
return;
}
RaiseChangeNotificationEvents(NotifyCollectionChangedAction.Add, changedItems, eventStartIndex);
}
protected virtual void RemoveRangeInternal(int startIndex, int count, List<T> changedItems)
{
if (startIndex < 0 || startIndex >= Count)
{
throw new ArgumentOutOfRangeException(nameof(startIndex));
}
if (startIndex + count > Count)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
CheckReentrancy();
for (int i = startIndex + count - 1; i >= startIndex; i--)
{
Items.RemoveAt(i);
}
RaiseChangeNotificationEvents(NotifyCollectionChangedAction.Remove, changedItems, startIndex);
}
// Writes straight into Items rather than going through Insert: the base would raise
// Count and Item[] for every single one, and suppression does not cover those.
private bool AddArrangeCore(List<T> collection, int startIndex = -1)
{
var itemsAdded = false;
var index = startIndex;
foreach (var item in collection)
{
if (startIndex >= 0)
{
Items.Insert(index, item);
}
else
{
Items.Add(item);
}
itemsAdded = true;
index++;
}
return itemsAdded;
}
protected virtual void RaiseChangeNotificationEvents(NotifyCollectionChangedAction action, List<T> changedItems = null, int startingIndex = -1)
{
OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
if (changedItems == null)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action));
}
else
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, changedItems: changedItems, startingIndex: startingIndex));
}
}
#region Mvx
// Everything below moved over from MvxObservableCollection, which used to sit
// between this class and its callers. Its AddRange and RemoveRange are gone rather
// than moved: they hid the ones above, so which of the two ran depended on the
// compile-time type of the reference.
public void AddRangeT(IEnumerable items)
{
AddRange(items.Cast<T>());
}
// Suffixed rather than overloaded: a List<T> satisfies both IList and
// IEnumerable<T>, so sharing the name would make every such call ambiguous.
public void InsertRangeT(int startIndex, IList items)
{
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
InsertRange(startIndex, items.Cast<T>());
}
public void ReplaceWithT(IEnumerable items)
{
ReplaceWith(items.Cast<T>());
}
/// <summary>
/// Replaces the current <see cref="DiffObservableCollection{T}"/> instance items with the ones specified in the items collection, raising a single <see cref="NotifyCollectionChangedAction.Reset"/> event.
/// </summary>
/// <param name="items">The collection from which the items are copied.</param>
/// <exception cref="ArgumentNullException">The items list is null.</exception>
public void ReplaceWith(IEnumerable<T> items)
{
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
using (SuppressEvents())
{
Clear();
AddRange(items);
}
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
/// <summary>
/// Switches the current <see cref="DiffObservableCollection{T}"/> instance items with the ones specified in the items collection, raising the minimum required change events.
/// </summary>
/// <param name="items">The collection from which the items are copied.</param>
/// <exception cref="ArgumentNullException">The items list is null.</exception>
public void SwitchTo(IEnumerable<T> items)
{
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
var itemIndex = 0;
var count = Count;
foreach (var item in items)
{
if (itemIndex >= count)
{
Add(item);
}
else if (!Equals(this[itemIndex], item))
{
this[itemIndex] = item;
}
itemIndex++;
}
while (count > itemIndex)
{
RemoveAt(--count);
}
}
public void Change(int index)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, this[index], index));
}
public void Clear(bool suppress)
{
using (SuppressEvents())
{
Clear();
}
}
public void Reset()
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public virtual bool Set<P>(ref P storage, P value, [CallerMemberName] string propertyName = null)
{
if (Equals(storage, value))
{
return false;
}
storage = value;
RaisePropertyChanged(propertyName);
return true;
}
public virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
{
return;
}
try
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
catch { }
}
#endregion
}
public class SuppressObservableCollection<T> : ObservableCollection<T>
{
public SuppressObservableCollection()
{
}
public SuppressObservableCollection(IEnumerable<T> collection)
: base(collection)
{
}
public SuppressObservableCollection(List<T> list)
: base(list)
{
}
protected readonly struct SuppressEventsDisposable : IDisposable
{
private readonly SuppressObservableCollection<T> _collection;
public SuppressEventsDisposable(SuppressObservableCollection<T> collection)
{
_collection = collection;
++collection._suppressEvents;
}
public void Dispose()
{
--_collection._suppressEvents;
}
}
private int _suppressEvents;
protected SuppressEventsDisposable SuppressEvents()
{
return new SuppressEventsDisposable(this);
}
public bool EventsAreSuppressed
{
get { return _suppressEvents > 0; }
}
public void Dispose()
{
_suppressEvents = int.MaxValue;
}
/// <summary>
/// Raises the CollectionChanged event with the provided event data.
/// </summary>
/// <param name="e">The event data to report in the event.</param>
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (!EventsAreSuppressed)
{
base.OnCollectionChanged(e);
}
}
public void RaiseCollectionChanged(NotifyCollectionChangedEventArgs args)
{
OnCollectionChanged(args);
}
}
}