forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactiveCollectionViewSource.cs
More file actions
316 lines (281 loc) · 13.9 KB
/
ReactiveCollectionViewSource.cs
File metadata and controls
316 lines (281 loc) · 13.9 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
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Threading;
using Splat;
#if UNIFIED
using Foundation;
using UIKit;
using NSAction = System.Action;
#else
using MonoTouch.Foundation;
using MonoTouch.UIKit;
#endif
namespace ReactiveUI
{
public class CollectionViewSectionInformation<TSource> : ISectionInformation<TSource, UICollectionView, UICollectionViewCell>
{
public IReactiveNotifyCollectionChanged<TSource> Collection { get; protected set; }
public Action<UICollectionViewCell> InitializeCellAction { get; protected set; }
public Func<object, NSString> CellKeySelector { get; protected set; }
}
public class CollectionViewSectionInformation<TSource, TCell> : CollectionViewSectionInformation<TSource>
where TCell : UICollectionViewCell
{
public CollectionViewSectionInformation(IReactiveNotifyCollectionChanged<TSource> collection, Func<object, NSString> cellKeySelector, Action<TCell> initializeCellAction = null)
{
Collection = collection;
CellKeySelector = cellKeySelector;
if (initializeCellAction != null) {
InitializeCellAction = cell => initializeCellAction((TCell)cell);
}
}
public CollectionViewSectionInformation(IReactiveNotifyCollectionChanged<TSource> collection, NSString cellKey, Action<TCell> initializeCellAction = null)
: this(collection, _ => cellKey, initializeCellAction)
{
}
}
class UICollectionViewAdapter : IUICollViewAdapter<UICollectionView, UICollectionViewCell>
{
readonly UICollectionView view;
internal UICollectionViewAdapter(UICollectionView view) { this.view = view; }
public void ReloadData() { view.ReloadData(); }
public void PerformBatchUpdates(Action updates, Action completion) { view.PerformBatchUpdates(new NSAction(updates), (completed) => completion()); }
public void InsertItems(NSIndexPath[] paths) { view.InsertItems(paths); }
public void DeleteItems(NSIndexPath[] paths) { view.DeleteItems(paths); }
public void ReloadItems(NSIndexPath[] paths) { view.ReloadItems(paths); }
public void MoveItem(NSIndexPath path, NSIndexPath newPath) { view.MoveItem(path, newPath); }
public UICollectionViewCell DequeueReusableCell(NSString cellKey, NSIndexPath path)
{
return (UICollectionViewCell)view.DequeueReusableCell(cellKey, path);
}
}
/// <summary>
/// ReactiveCollectionViewSource is a Collection View Source that is
/// connected to a ReactiveList that automatically updates the View based
/// on the contents of the list. The collection changes are buffered and
/// View items are animated in and out as items are added.
/// </summary>
public class ReactiveCollectionViewSource<TSource> : UICollectionViewSource, IEnableLogger, IDisposable, IReactiveNotifyPropertyChanged<ReactiveCollectionViewSource<TSource>>, IHandleObservableErrors, IReactiveObject
{
readonly CommonReactiveSource<TSource, UICollectionView, UICollectionViewCell, CollectionViewSectionInformation<TSource>> commonSource;
readonly Subject<object> elementSelected = new Subject<object>();
public ReactiveCollectionViewSource(UICollectionView collectionView, IReactiveNotifyCollectionChanged<TSource> collection, NSString cellKey, Action<UICollectionViewCell> initializeCellAction = null)
: this(collectionView) {
this.Data = new[] { new CollectionViewSectionInformation<TSource, UICollectionViewCell>(collection, cellKey, initializeCellAction) };
}
[Obsolete("Please bind your view model to the Data property.")]
public ReactiveCollectionViewSource(UICollectionView collectionView, IReadOnlyList<CollectionViewSectionInformation<TSource>> sectionInformation)
: this(collectionView) {
this.Data = sectionInformation;
}
public ReactiveCollectionViewSource(UICollectionView collectionView) {
setupRxObj();
var adapter = new UICollectionViewAdapter(collectionView);
this.commonSource = new CommonReactiveSource<TSource, UICollectionView, UICollectionViewCell, CollectionViewSectionInformation<TSource>>(adapter);
}
/// <summary>
/// Gets or sets the data that should be displayed by this
/// <see cref="ReactiveCollectionViewSource"/>. You should
/// probably bind your view model to this property.
/// If the list implements <see cref="IReactiveNotifyCollectionChanged"/>,
/// then the source will react to changes to the contents of the list as well.
/// </summary>
/// <value>The data.</value>
public IReadOnlyList<CollectionViewSectionInformation<TSource>> Data
{
get { return commonSource.SectionInfo; }
set {
if (commonSource.SectionInfo == value) return;
this.raisePropertyChanging("Data");
commonSource.SectionInfo = value;
this.raisePropertyChanged("Data");
}
}
/// <summary>
/// Gets an IObservable that is a hook to <see cref="ItemSelected"/> calls.
/// </summary>
public IObservable<object> ElementSelected {
get { return elementSelected; }
}
public IObservable<IEnumerable<NotifyCollectionChangedEventArgs>> DidPerformUpdates {
get { return commonSource.DidPerformUpdates; }
}
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
return commonSource.GetCell(indexPath);
}
#if UNIFIED
public override nint NumberOfSections(UICollectionView collectionView)
#else
public override int NumberOfSections(UICollectionView collectionView)
#endif
{
return commonSource.NumberOfSections();
}
#if UNIFIED
public override nint GetItemsCount(UICollectionView collectionView, nint section)
#else
public override int GetItemsCount(UICollectionView collectionView, int section)
#endif
{
return commonSource.RowsInSection((int)section);
}
public override void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
elementSelected.OnNext(commonSource.ItemAt(indexPath));
}
public object ItemAt(NSIndexPath indexPath)
{
return commonSource.ItemAt(indexPath);
}
protected override void Dispose(bool disposing)
{
if (disposing) commonSource.Dispose();
base.Dispose(disposing);
}
public event PropertyChangingEventHandler PropertyChanging {
add { PropertyChangingEventManager.AddHandler(this, value); }
remove { PropertyChangingEventManager.RemoveHandler(this, value); }
}
void IReactiveObject.RaisePropertyChanging(PropertyChangingEventArgs args)
{
PropertyChangingEventManager.DeliverEvent(this, args);
}
public event PropertyChangedEventHandler PropertyChanged {
add { PropertyChangedEventManager.AddHandler(this, value); }
remove { PropertyChangedEventManager.RemoveHandler(this, value); }
}
void IReactiveObject.RaisePropertyChanged(PropertyChangedEventArgs args)
{
PropertyChangedEventManager.DeliverEvent(this, args);
}
/// <summary>
/// Represents an Observable that fires *before* a property is about to
/// be changed.
/// </summary>
public IObservable<IReactivePropertyChangedEventArgs<ReactiveCollectionViewSource<TSource>>> Changing {
get { return this.getChangingObservable(); }
}
/// <summary>
/// Represents an Observable that fires *after* a property has changed.
/// </summary>
public IObservable<IReactivePropertyChangedEventArgs<ReactiveCollectionViewSource<TSource>>> Changed {
get { return this.getChangedObservable(); }
}
public IObservable<Exception> ThrownExceptions { get { return this.getThrownExceptionsObservable(); } }
void setupRxObj()
{
}
/// <summary>
/// When this method is called, an object will not fire change
/// notifications (neither traditional nor Observable notifications)
/// until the return value is disposed.
/// </summary>
/// <returns>An object that, when disposed, reenables change
/// notifications.</returns>
public IDisposable SuppressChangeNotifications()
{
return this.suppressChangeNotifications();
}
}
/// <summary>
/// Extension methods for <see cref="ReactiveCollectionViewSource"/>.
/// </summary>
public static class ReactiveCollectionViewSourceExtensions
{
/// <summary>
/// <para>Extension method that binds an observable of a list of collection
/// sections as the source of a <see cref="UICollectionView"/>.</para>
/// <para>If your <see cref="IReadOnlyList"/> is also an instance of
/// <see cref="IReactiveNotifyCollectionChanged"/>, then this method
/// will silently update the bindings whenever it changes as well.
/// Otherwise, it will just log a message.</para>
/// </summary>
/// <returns>The <see cref="IDisposable"/> used to dispose this binding.</returns>
/// <param name="sectionsObservable">Sections observable.</param>
/// <param name="collectionView">Collection view.</param>
/// <param name="initSource">Optionally initializes some property of
/// the <see cref="ReactiveCollectionViewSource"/>.</param>
/// <typeparam name="TCell">Type of the <see cref="UICollectionViewCell"/>.</typeparam>
public static IDisposable BindTo<TSource, TCell>(
this IObservable<IReadOnlyList<CollectionViewSectionInformation<TSource, TCell>>> sectionsObservable,
UICollectionView collectionView,
Func<ReactiveCollectionViewSource<TSource>, IDisposable> initSource = null)
where TCell : UICollectionViewCell
{
var source = new ReactiveCollectionViewSource<TSource>(collectionView);
if (initSource != null) initSource(source);
var bind = sectionsObservable.BindTo(source, x => x.Data);
collectionView.Source = source;
return new CompositeDisposable(bind, source);
}
/// <summary>
/// Extension method that binds an observable of a collection
/// as the source of a <see cref="UICollectionView"/>.
/// </summary>
/// <returns>The <see cref="IDisposable"/> used to dispose this binding.</returns>
/// <param name="sourceObservable">Source collection observable.</param>
/// <param name="collectionView">Collection view.</param>
/// <param name="cellKey">Cell key.</param>
/// <param name="initializeCellAction">Initialize cell action.</param>
/// <param name="initSource">Optionally initializes some property of
/// the <see cref="ReactiveCollectionViewSource"/>.</param>
/// <typeparam name="TCell">Type of the <see cref="UICollectionViewCell"/>.</typeparam>
public static IDisposable BindTo<TSource,TCell>(
this IObservable<IReactiveNotifyCollectionChanged<TSource>> sourceObservable,
UICollectionView collectionView,
NSString cellKey,
Action<TCell> initializeCellAction = null,
Func<ReactiveCollectionViewSource<TSource>, IDisposable> initSource = null)
where TCell : UICollectionViewCell
{
return sourceObservable
.Select(
src => new[]
{
new CollectionViewSectionInformation<TSource, TCell>(
src,
cellKey,
initializeCellAction)
})
.BindTo(collectionView, initSource);
}
/// <summary>
/// Extension method that binds an observable of a collection
/// as the source of a <see cref="UICollectionView"/>. Also registers
/// the given class with an unspecified cellKey (you should probably
/// not specify any other cellKeys).
/// </summary>
/// <returns>The <see cref="IDisposable"/> used to dispose this binding.</returns>
/// <param name="sourceObservable">Source collection observable.</param>
/// <param name="collectionView">Collection view.</param>
/// <param name="initializeCellAction">Initialize cell action.</param>
/// <param name="initSource">Optionally initializes some property of
/// the <see cref="ReactiveCollectionViewSource"/>.</param>
/// <typeparam name="TCell">Type of the <see cref="UICollectionViewCell"/>.</typeparam>
public static IDisposable BindTo<TSource, TCell>(
this IObservable<IReactiveNotifyCollectionChanged<TSource>> sourceObservable,
UICollectionView collectionView,
Action<TCell> initializeCellAction = null,
Func<ReactiveCollectionViewSource<TSource>, IDisposable> initSource = null)
where TCell : UICollectionViewCell
{
var type = typeof(TCell);
var cellKey = new NSString(type.ToString());
collectionView.RegisterClassForCell(type, new NSString(cellKey));
return sourceObservable
.BindTo(collectionView, cellKey, initializeCellAction, initSource);
}
}
}