This repository was archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathObservableGroupedSource.cs
365 lines (303 loc) · 8.37 KB
/
ObservableGroupedSource.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using Foundation;
using UIKit;
namespace Xamarin.Forms.Platform.iOS
{
internal class ObservableGroupedSource : IItemsViewSource
{
readonly UICollectionView _collectionView;
readonly UICollectionViewController _collectionViewController;
readonly IList _groupSource;
bool _disposed;
List<ObservableItemsSource> _groups = new List<ObservableItemsSource>();
public ObservableGroupedSource(IEnumerable groupSource, UICollectionViewController collectionViewController)
{
_collectionViewController = collectionViewController;
_collectionView = _collectionViewController.CollectionView;
_groupSource = groupSource as IList ?? new ListSource(groupSource);
if (_groupSource is INotifyCollectionChanged incc)
{
incc.CollectionChanged += CollectionChanged;
}
ResetGroupTracking();
}
public object this[NSIndexPath indexPath]
{
get
{
return GetGroupItemAt(indexPath.Section, (int)indexPath.Item);
}
}
public int GroupCount => _groupSource.Count;
public int ItemCount
{
get
{
var total = 0;
for (int n = 0; n < _groupSource.Count; n++)
{
total += GetGroupCount(n);
}
return total;
}
}
public NSIndexPath GetIndexForItem(object item)
{
for (int i = 0; i < _groupSource.Count; i++)
{
var j = IndexInGroup(item, _groupSource[i]);
if (j == -1)
{
continue;
}
return NSIndexPath.Create(i, j);
}
return NSIndexPath.Create(-1, -1);
}
public object Group(NSIndexPath indexPath)
{
return _groupSource[indexPath.Section];
}
public int ItemCountInGroup(nint group)
{
return GetGroupCount((int)group);
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
_disposed = true;
if (disposing)
{
ClearGroupTracking();
if (_groupSource is INotifyCollectionChanged incc)
{
incc.CollectionChanged -= CollectionChanged;
}
}
}
void ClearGroupTracking()
{
for (int n = _groups.Count - 1; n >= 0; n--)
{
_groups[n].Dispose();
_groups.RemoveAt(n);
}
}
void ResetGroupTracking()
{
ClearGroupTracking();
for (int n = 0; n < _groupSource.Count; n++)
{
if (_groupSource[n] is INotifyCollectionChanged && _groupSource[n] is IEnumerable list)
{
_groups.Add(new ObservableItemsSource(list, _collectionViewController, n));
}
}
}
void CollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
if (Device.IsInvokeRequired)
{
Device.BeginInvokeOnMainThread(() => CollectionChanged(args));
}
else
{
CollectionChanged(args);
}
}
void CollectionChanged(NotifyCollectionChangedEventArgs args)
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
Add(args);
break;
case NotifyCollectionChangedAction.Remove:
Remove(args);
break;
case NotifyCollectionChangedAction.Replace:
Replace(args);
break;
case NotifyCollectionChangedAction.Move:
Move(args);
break;
case NotifyCollectionChangedAction.Reset:
Reload(true);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
void Reload(bool collectionWasReset = false)
{
ResetGroupTracking();
_collectionView.ReloadData();
// I'm trying to modify as little as possible
// due to possible unpredictable consequences including perf. degradation
// https://github.com/xamarin/Xamarin.Forms/issues/13268
if (collectionWasReset)
{
_collectionView.LayoutIfNeeded();
}
_collectionView.CollectionViewLayout.InvalidateLayout();
}
NSIndexSet CreateIndexSetFrom(int startIndex, int count)
{
return NSIndexSet.FromNSRange(new NSRange(startIndex, count));
}
bool NotLoadedYet()
{
// If the UICollectionView hasn't actually been loaded, then calling InsertSections or DeleteSections is
// going to crash or get in an unusable state; instead, ReloadData should be used
return !_collectionViewController.IsViewLoaded || _collectionViewController.View.Window == null;
}
void Add(NotifyCollectionChangedEventArgs args)
{
if (ReloadRequired())
{
Reload();
return;
}
var startIndex = args.NewStartingIndex > -1 ? args.NewStartingIndex : _groupSource.IndexOf(args.NewItems[0]);
var count = args.NewItems.Count;
// Adding a group will change the section index for all subsequent groups, so the easiest thing to do
// is to reset all the group tracking to get it up-to-date
ResetGroupTracking();
// Queue up the updates to the UICollectionView
Update(() => _collectionView.InsertSections(CreateIndexSetFrom(startIndex, count)));
}
void Remove(NotifyCollectionChangedEventArgs args)
{
var startIndex = args.OldStartingIndex;
if (startIndex < 0)
{
// INCC implementation isn't giving us enough information to know where the removed items were in the
// collection. So the best we can do is a complete reload
Reload();
return;
}
if (ReloadRequired())
{
Reload();
return;
}
// Removing a group will change the section index for all subsequent groups, so the easiest thing to do
// is to reset all the group tracking to get it up-to-date
ResetGroupTracking();
// Since we have a start index, we can be more clever about removing the item(s) (and get the nifty animations)
var count = args.OldItems.Count;
// Queue up the updates to the UICollectionView
Update(() => _collectionView.DeleteSections(CreateIndexSetFrom(startIndex, count)));
}
void Replace(NotifyCollectionChangedEventArgs args)
{
var newCount = args.NewItems.Count;
if (newCount == args.OldItems.Count)
{
ResetGroupTracking();
var startIndex = args.NewStartingIndex > -1 ? args.NewStartingIndex : _groupSource.IndexOf(args.NewItems[0]);
// We are replacing one set of items with a set of equal size; we can do a simple item range update
Update(() => _collectionView.ReloadSections(CreateIndexSetFrom(startIndex, newCount)));
return;
}
// The original and replacement sets are of unequal size; this means that everything currently in view will
// have to be updated. So we just have to use ReloadData and let the UICollectionView update everything
Reload();
}
void Move(NotifyCollectionChangedEventArgs args)
{
var count = args.NewItems.Count;
ResetGroupTracking();
if (count == 1)
{
// For a single item, we can use MoveSection and get the animation
Update(() => _collectionView.MoveSection(args.OldStartingIndex, args.NewStartingIndex));
return;
}
var start = Math.Min(args.OldStartingIndex, args.NewStartingIndex);
var end = Math.Max(args.OldStartingIndex, args.NewStartingIndex) + count;
Update(() => _collectionView.ReloadSections(CreateIndexSetFrom(start, end)));
}
int GetGroupCount(int groupIndex)
{
switch (_groupSource[groupIndex])
{
case IList list:
return list.Count;
case IEnumerable enumerable:
var count = 0;
var enumerator = enumerable.GetEnumerator();
while (enumerator.MoveNext())
{
count += 1;
}
return count;
}
return 0;
}
object GetGroupItemAt(int groupIndex, int index)
{
switch (_groupSource[groupIndex])
{
case IList list:
return list[index];
case IEnumerable enumerable:
var count = -1;
var enumerator = enumerable.GetEnumerator();
do
{
enumerator.MoveNext();
count += 1;
}
while (count < index);
return enumerator.Current;
}
return null;
}
int IndexInGroup(object item, object group)
{
switch (group)
{
case IList list:
return list.IndexOf(item);
case IEnumerable enumerable:
var enumerator = enumerable.GetEnumerator();
var index = 0;
while (enumerator.MoveNext())
{
if (enumerator.Current == item)
{
return index;
}
}
return -1;
}
return -1;
}
bool ReloadRequired()
{
// If the UICollectionView has never been loaded, or doesn't yet have any sections, any insert/delete operations
// are gonna crash hard. We'll need to reload the data instead.
return NotLoadedYet()
|| _collectionView.NumberOfSections() == 0;
}
void Update(Action update)
{
if (_collectionView.Hidden)
{
return;
}
update();
}
}
}