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 pathObservableItemsSource.cs
295 lines (237 loc) · 6.86 KB
/
ObservableItemsSource.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
using System;
using System.Collections;
using System.Collections.Specialized;
using Foundation;
using UIKit;
namespace Xamarin.Forms.Platform.iOS
{
internal class ObservableItemsSource : IItemsViewSource
{
readonly UICollectionViewController _collectionViewController;
protected readonly UICollectionView CollectionView;
readonly bool _grouped;
readonly int _section;
readonly IEnumerable _itemsSource;
bool _disposed;
public ObservableItemsSource(IEnumerable itemSource, UICollectionViewController collectionViewController, int group = -1)
{
_collectionViewController = collectionViewController;
CollectionView = _collectionViewController.CollectionView;
_section = group < 0 ? 0 : group;
_grouped = group >= 0;
_itemsSource = itemSource;
Count = ItemsCount();
((INotifyCollectionChanged)itemSource).CollectionChanged += CollectionChanged;
}
internal event NotifyCollectionChangedEventHandler CollectionViewUpdating;
internal event NotifyCollectionChangedEventHandler CollectionViewUpdated;
public int Count { get; private set; }
public int Section => _section;
public object this[int index] => ElementAt(index);
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
((INotifyCollectionChanged)_itemsSource).CollectionChanged -= CollectionChanged;
}
_disposed = true;
}
}
public int ItemCountInGroup(nint group)
{
return Count;
}
public object Group(NSIndexPath indexPath)
{
return null;
}
public NSIndexPath GetIndexForItem(object item)
{
for (int n = 0; n < Count; n++)
{
if(ItemComparer.AreEquals(this[n], item))
{
return NSIndexPath.Create(_section, n);
}
}
return NSIndexPath.Create(-1, -1);
}
public int GroupCount => 1;
public int ItemCount => Count;
public object this[NSIndexPath indexPath]
{
get
{
if (indexPath.Section != _section)
{
throw new ArgumentOutOfRangeException(nameof(indexPath));
}
return this[(int)indexPath.Item];
}
}
void CollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
if (Device.IsInvokeRequired)
{
Device.BeginInvokeOnMainThread(() => CollectionChanged(args));
}
else
{
CollectionChanged(args);
}
}
void CollectionChanged(NotifyCollectionChangedEventArgs args)
{
// Force UICollectionView to get the internal accounting straight
if(!CollectionView.Hidden)
CollectionView.NumberOfItemsInSection(_section);
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();
break;
default:
throw new ArgumentOutOfRangeException();
}
}
void Reload()
{
var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
Count = ItemsCount();
OnCollectionViewUpdating(args);
CollectionView.ReloadData();
CollectionView.CollectionViewLayout.InvalidateLayout();
OnCollectionViewUpdated(args);
}
protected virtual NSIndexPath[] CreateIndexesFrom(int startIndex, int count)
{
return IndexPathHelpers.GenerateIndexPathRange(_section, startIndex, count);
}
void Add(NotifyCollectionChangedEventArgs args)
{
var count = args.NewItems.Count;
Count += count;
var startIndex = args.NewStartingIndex > -1 ? args.NewStartingIndex : IndexOf(args.NewItems[0]);
// Queue up the updates to the UICollectionView
Update(() => CollectionView.InsertItems(CreateIndexesFrom(startIndex, count)), args);
}
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 ReloadData()
Reload();
return;
}
// If 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;
Count -= count;
Update(() => CollectionView.DeleteItems(CreateIndexesFrom(startIndex, count)), args);
}
void Replace(NotifyCollectionChangedEventArgs args)
{
var newCount = args.NewItems.Count;
if (newCount == args.OldItems.Count)
{
var startIndex = args.NewStartingIndex > -1 ? args.NewStartingIndex : 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.ReloadItems(CreateIndexesFrom(startIndex, newCount)), args);
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;
if (count == 1)
{
// For a single item, we can use MoveItem and get the animation
var oldPath = NSIndexPath.Create(_section, args.OldStartingIndex);
var newPath = NSIndexPath.Create(_section, args.NewStartingIndex);
Update(() => CollectionView.MoveItem(oldPath, newPath), args);
return;
}
var start = Math.Min(args.OldStartingIndex, args.NewStartingIndex);
var end = Math.Max(args.OldStartingIndex, args.NewStartingIndex) + count;
Update(() => CollectionView.ReloadItems(CreateIndexesFrom(start, end)), args);
}
internal int ItemsCount()
{
if (_itemsSource is IList list)
return list.Count;
int count = 0;
foreach (var item in _itemsSource)
count++;
return count;
}
internal object ElementAt(int index)
{
if (_itemsSource is IList list)
return list[index];
int count = 0;
foreach (var item in _itemsSource)
{
if (count == index)
return item;
count++;
}
return -1;
}
internal int IndexOf(object item)
{
if (_itemsSource is IList list)
return list.IndexOf(item);
int count = 0;
foreach (var i in _itemsSource)
{
if (i == item)
return count;
count++;
}
return -1;
}
void Update(Action update, NotifyCollectionChangedEventArgs args)
{
if (CollectionView.Hidden)
{
return;
}
OnCollectionViewUpdating(args);
update();
OnCollectionViewUpdated(args);
}
void OnCollectionViewUpdating(NotifyCollectionChangedEventArgs args)
{
CollectionViewUpdating?.Invoke(this, args);
}
void OnCollectionViewUpdated(NotifyCollectionChangedEventArgs args)
{
Device.BeginInvokeOnMainThread(() =>
{
CollectionViewUpdated?.Invoke(this, args);
});
}
}
}