forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactiveTableViewSource.cs
More file actions
464 lines (413 loc) · 18.5 KB
/
ReactiveTableViewSource.cs
File metadata and controls
464 lines (413 loc) · 18.5 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
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 ReactiveUI;
using Splat;
#if UNIFIED
using Foundation;
using UIKit;
#else
using MonoTouch.Foundation;
using MonoTouch.UIKit;
#endif
namespace ReactiveUI
{
public class TableSectionInformation<TSource> : ISectionInformation<TSource, UITableView, UITableViewCell>
{
public IReactiveNotifyCollectionChanged<TSource> Collection { get; protected set; }
public Action<UITableViewCell> InitializeCellAction { get; protected set; }
public Func<object, NSString> CellKeySelector { get; protected set; }
public float SizeHint { get; protected set; }
/// <summary>
/// Gets or sets the header of this section.
/// </summary>
/// <value>The header, or null if a header shouldn't be used.</value>
public TableSectionHeader Header { get; set; }
/// <summary>
/// Gets or sets the footer of this section.
/// </summary>
/// <value>The footer, or null if a footer shouldn't be used.</value>
public TableSectionHeader Footer { get; set; }
}
public class TableSectionInformation<TSource, TCell> : TableSectionInformation<TSource>
where TCell : UITableViewCell
{
public TableSectionInformation(IReactiveNotifyCollectionChanged<TSource> collection, Func<object, NSString> cellKeySelector, float sizeHint, Action<TCell> initializeCellAction = null)
{
Collection = collection;
SizeHint = sizeHint;
CellKeySelector = cellKeySelector;
if (initializeCellAction != null)
InitializeCellAction = cell => initializeCellAction((TCell)cell);
}
public TableSectionInformation(IReactiveNotifyCollectionChanged<TSource> collection, NSString cellKey, float sizeHint, Action<TCell> initializeCellAction = null)
: this(collection, _ => cellKey, sizeHint, initializeCellAction)
{
}
}
class UITableViewAdapter : IUICollViewAdapter<UITableView, UITableViewCell>
{
readonly UITableView view;
internal UITableViewAdapter(UITableView view) { this.view = view; }
public void ReloadData() { view.ReloadData(); }
public void PerformBatchUpdates(Action updates, Action completion)
{
view.BeginUpdates();
try {
updates();
} finally {
view.EndUpdates();
completion();
}
}
public void InsertItems(NSIndexPath[] paths) { view.InsertRows(paths, UITableViewRowAnimation.Automatic); }
public void DeleteItems(NSIndexPath[] paths) { view.DeleteRows(paths, UITableViewRowAnimation.Automatic); }
public void ReloadItems(NSIndexPath[] paths) { view.ReloadRows(paths, UITableViewRowAnimation.Automatic); }
public void MoveItem(NSIndexPath path, NSIndexPath newPath) { view.MoveRow(path, newPath); }
public UITableViewCell DequeueReusableCell(NSString cellKey, NSIndexPath path)
{
return view.DequeueReusableCell(cellKey, path);
}
}
/// <summary>
/// A header or footer of a table section.
/// </summary>
public class TableSectionHeader
{
/// <summary>
/// Gets the function that creates the <see cref="UIView"/>
/// used as header for this section. Overrides Title
/// </summary>
public Func<UIView> View { get; protected set; }
/// <summary>
/// Gets the height of the header.
/// </summary>
public float Height { get; protected set; }
/// <summary>
/// Gets the title for the section header, only used if View is null.
/// </summary>
public string Title { get; protected set; }
/// <summary>
/// Initializes a new instance of the <see cref="TableSectionHeader"/>
/// struct.
/// </summary>
/// <param name="view">Function that creates header's <see cref="UIView"/>.</param>
/// <param name="height">Height of the header.</param>
public TableSectionHeader (Func<UIView> view, float height)
{
this.View = view;
this.Height = height;
}
/// <summary>
/// Initializes a new instance of the <see cref="ReactiveUI.Cocoa.TableSectionHeader"/> class.
/// </summary>
/// <param name="title">Title to use.</param>
public TableSectionHeader (string title)
{
this.Title = title;
}
}
/// <summary>
/// ReactiveTableViewSource is a Table 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 ReactiveTableViewSource<TSource> : UITableViewSource, IEnableLogger, IDisposable, IReactiveNotifyPropertyChanged<ReactiveTableViewSource<TSource>>, IHandleObservableErrors, IReactiveObject
{
readonly CommonReactiveSource<TSource, UITableView, UITableViewCell, TableSectionInformation<TSource>> commonSource;
readonly Subject<object> elementSelected = new Subject<object>();
public ReactiveTableViewSource(UITableView tableView, IReactiveNotifyCollectionChanged<TSource> collection, NSString cellKey, float sizeHint, Action<UITableViewCell> initializeCellAction = null)
: this(tableView) {
this.Data = new[] { new TableSectionInformation<TSource, UITableViewCell>(collection, cellKey, sizeHint, initializeCellAction)};
}
[Obsolete("Please bind your view model to the Data property.")]
public ReactiveTableViewSource(UITableView tableView, IReadOnlyList<TableSectionInformation<TSource>> sectionInformation)
: this(tableView) {
this.Data = sectionInformation;
}
public ReactiveTableViewSource(UITableView tableView) {
setupRxObj();
var adapter = new UITableViewAdapter(tableView);
this.commonSource = new CommonReactiveSource<TSource, UITableView, UITableViewCell, TableSectionInformation<TSource>>(adapter);
}
/// <summary>
/// Gets or sets the data that should be displayed by this
/// <see cref="ReactiveTableViewSource"/>. 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<TableSectionInformation<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="RowSelected"/> calls.
/// </summary>
public IObservable<object> ElementSelected {
get { return elementSelected; }
}
public IObservable<IEnumerable<NotifyCollectionChangedEventArgs>> DidPerformUpdates {
get { return commonSource.DidPerformUpdates; }
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
return commonSource.GetCell(indexPath);
}
#if UNIFIED
public override nint NumberOfSections(UITableView tableView)
#else
public override int NumberOfSections(UITableView tableView)
#endif
{
return commonSource.NumberOfSections();
}
#if UNIFIED
public override nint RowsInSection(UITableView tableview, nint section)
#else
public override int RowsInSection(UITableView tableview, int section)
#endif
{
return commonSource.RowsInSection((int)section);
}
public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath)
{
return false;
}
public override bool CanMoveRow(UITableView tableView, NSIndexPath indexPath)
{
return false;
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
elementSelected.OnNext(commonSource.ItemAt(indexPath));
}
protected override void Dispose(bool disposing)
{
if (disposing) commonSource.Dispose();
base.Dispose(disposing);
}
#if UNIFIED
public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
#else
public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
#endif
{
return commonSource.SectionInfo[indexPath.Section].SizeHint;
}
#if UNIFIED
public override nfloat GetHeightForHeader(UITableView tableView, nint section)
#else
public override float GetHeightForHeader(UITableView tableView, int section)
#endif
{
var header = commonSource.SectionInfo[(int)section].Header;
// NB: -1 is a magic # that causes iOS to use the regular height. go figure.
return header == null || header.View == null ? -1 : header.Height;
}
#if UNIFIED
public override nfloat GetHeightForFooter(UITableView tableView, nint section)
#else
public override float GetHeightForFooter(UITableView tableView, int section)
#endif
{
var footer = commonSource.SectionInfo[(int)section].Footer;
return footer == null || footer.View == null ? -1 : footer.Height;
}
#if UNIFIED
public override string TitleForHeader(UITableView tableView, nint section)
#else
public override string TitleForHeader(UITableView tableView, int section)
#endif
{
var header = commonSource.SectionInfo[(int)section].Header;
return header == null || header.Title == null ? null : header.Title;
}
#if UNIFIED
public override string TitleForFooter(UITableView tableView, nint section)
#else
public override string TitleForFooter(UITableView tableView, int section)
#endif
{
var footer = commonSource.SectionInfo[(int)section].Footer;
return footer == null || footer.Title == null ? null : footer.Title;
}
#if UNIFIED
public override UIView GetViewForHeader(UITableView tableView, nint section)
#else
public override UIView GetViewForHeader(UITableView tableView, int section)
#endif
{
var header = commonSource.SectionInfo[(int)section].Header;
return header == null || header.View == null ? null : header.View.Invoke();
}
#if UNIFIED
public override UIView GetViewForFooter(UITableView tableView, nint section)
#else
public override UIView GetViewForFooter(UITableView tableView, int section)
#endif
{
var footer = commonSource.SectionInfo[(int)section].Footer;
return footer == null || footer.View == null ? null : footer.View.Invoke();
}
public object ItemAt(NSIndexPath indexPath)
{
return commonSource.ItemAt(indexPath);
}
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<ReactiveTableViewSource<TSource>>> Changing {
get { return this.getChangingObservable(); }
}
/// <summary>
/// Represents an Observable that fires *after* a property has changed.
/// </summary>
public IObservable<IReactivePropertyChangedEventArgs<ReactiveTableViewSource<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="ReactiveTableViewSource"/>.
/// </summary>
public static class ReactiveTableViewSourceExtensions
{
/// <summary>
/// <para>Extension method that binds an observable of a list of table
/// sections as the source of a <see cref="UITableView"/>.</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="tableView">Table view.</param>
/// <param name="initSource">Optionally initializes some property of
/// the <see cref="ReactiveTableViewSource"/>.</param>
/// <typeparam name="TCell">Type of the <see cref="UITableViewCell"/>.</typeparam>
public static IDisposable BindTo<TSource, TCell>(
this IObservable<IReadOnlyList<TableSectionInformation<TSource, TCell>>> sectionsObservable,
UITableView tableView,
Func<ReactiveTableViewSource<TSource>, IDisposable> initSource = null)
where TCell : UITableViewCell
{
var source = new ReactiveTableViewSource<TSource>(tableView);
if (initSource != null) initSource(source);
var bind = sectionsObservable.BindTo(source, x => x.Data);
tableView.Source = source;
return new CompositeDisposable(bind, source);
}
/// <summary>
/// Extension method that binds an observable of a collection
/// as the source of a <see cref="UITableView"/>.
/// </summary>
/// <returns>The <see cref="IDisposable"/> used to dispose this binding.</returns>
/// <param name="sourceObservable">Source collection observable.</param>
/// <param name="tableView">Table view.</param>
/// <param name="cellKey">Cell key.</param>
/// <param name="sizeHint">Size hint.</param>
/// <param name="initializeCellAction">Initialize cell action.</param>
/// <param name="initSource">Optionally initializes some property of
/// the <see cref="ReactiveTableViewSource"/>.</param>
/// <typeparam name="TCell">Type of the <see cref="UITableViewCell"/>.</typeparam>
public static IDisposable BindTo<TSource, TCell>(
this IObservable<IReactiveNotifyCollectionChanged<TSource>> sourceObservable,
UITableView tableView,
NSString cellKey,
float sizeHint,
Action<TCell> initializeCellAction = null,
Func<ReactiveTableViewSource<TSource>, IDisposable> initSource = null)
where TCell : UITableViewCell
{
return sourceObservable
.Select(src => new[] {
new TableSectionInformation<TSource, TCell>(
src,
cellKey,
sizeHint,
initializeCellAction),
})
.BindTo(tableView, initSource);
}
/// <summary>
/// Extension method that binds an observable of a collection
/// as the source of a <see cref="UITableView"/>. 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="tableView">Table view.</param>
/// <param name="sizeHint">Size hint.</param>
/// <param name="initializeCellAction">Initialize cell action.</param>
/// <param name="initSource">Optionally initializes some property of
/// the <see cref="ReactiveTableViewSource"/>.</param>
/// <typeparam name="TCell">Type of the <see cref="UITableViewCell"/>.</typeparam>
public static IDisposable BindTo<TSource, TCell>(
this IObservable<IReactiveNotifyCollectionChanged<TSource>> sourceObservable,
UITableView tableView,
float sizeHint,
Action<TCell> initializeCellAction = null,
Func<ReactiveTableViewSource<TSource>, IDisposable> initSource = null)
where TCell : UITableViewCell
{
var type = typeof(TCell);
var cellKey = new NSString(type.ToString());
tableView.RegisterClassForCellReuse(type, new NSString(cellKey));
return sourceObservable
.BindTo(tableView, cellKey, sizeHint, initializeCellAction, initSource);
}
}
}