Repository navigation
Expand file tree
/
Copy pathObservableCollectionEx.cs
More file actions
167 lines (149 loc) · 7.86 KB
/
Copy pathObservableCollectionEx.cs
File metadata and controls
167 lines (149 loc) · 7.86 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
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
namespace DynamicData.Binding
{
/// <summary>
/// Extensions to convert an observable collection into a dynamic stream
/// </summary>
public static class ObservableCollectionEx
{
/// <summary>
/// Convert an observable collection into an observable change set
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <param name="source">The source.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">source</exception>
public static IObservable<IChangeSet<T>> ToObservableChangeSet<T>(this ObservableCollection<T> source)
{
return Observable.Create<IChangeSet<T>>
(
observer =>
{
Func<ChangeSet<T>> initialChangeSet = () =>
{
var initial = new Change<T>(ListChangeReason.AddRange, source.ToList());
return new ChangeSet<T>() {initial};
};
//populate local cache, otherwise there is no way to deal with a reset
var cloneOfList = new SourceList<T>();
var sourceUpdates = Observable
.FromEventPattern<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
h => source.CollectionChanged += h,
h => source.CollectionChanged -= h)
.Select
(
args =>
{
var changes = args.EventArgs;
switch (changes.Action)
{
case NotifyCollectionChangedAction.Add:
return changes.NewItems.OfType<T>()
.Select((t, index) => new Change<T>(ListChangeReason.Add, t, index + changes.NewStartingIndex));
case NotifyCollectionChangedAction.Remove:
return changes.OldItems.OfType<T>()
.Select((t, index) => new Change<T>(ListChangeReason.Remove, t, index + changes.OldStartingIndex));
case NotifyCollectionChangedAction.Replace:
{
return changes.NewItems.OfType<T>()
.Select((t, idx) =>
{
var old = changes.OldItems[idx];
return new Change<T>(ListChangeReason.Replace, t, (T) old, idx, idx);
});
}
case NotifyCollectionChangedAction.Reset:
{
var cleared = new Change<T>(ListChangeReason.Clear,cloneOfList.Items.ToList(),0);
var clearedChangeSet = new ChangeSet<T>() {cleared};
return clearedChangeSet.Concat(initialChangeSet());
}
default:
return null;
}
})
.Where(updates => updates != null)
.Select(updates => (IChangeSet<T>) new ChangeSet<T>(updates));
var initialChanges = initialChangeSet();
var cacheLoader = Observable.Return(initialChanges).Concat(sourceUpdates).PopulateInto(cloneOfList);
var subscriber = cloneOfList.Connect().SubscribeSafe(observer);
return new CompositeDisposable(cacheLoader, subscriber, cloneOfList);
});
}
/// <summary>
/// Convert an observable collection into a dynamic stream of change sets
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="keySelector">The key selector.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">source
/// or
/// keySelector</exception>
public static IObservable<IChangeSet<TObject, TKey>> ToObservableChangeSet<TObject, TKey>(this ObservableCollection<TObject> source, Func<TObject, TKey> keySelector)
{
if (source == null) throw new ArgumentNullException("source");
if (keySelector == null) throw new ArgumentNullException("keySelector");
return Observable.Create<IChangeSet<TObject, TKey>>
(
observer =>
{
//populate local cache, otherwise there is no way to deal with a reset
var cloneOfList = new SourceCache<TObject, TKey>(keySelector);
Func<ChangeSet<TObject, TKey>> initialChangeSet = () =>
{
var items = source.Select(t => new Change<TObject, TKey>(ChangeReason.Add, keySelector(t), t));
return new ChangeSet<TObject, TKey>(items);
};
var sourceUpdates = Observable.FromEventPattern<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
h => source.CollectionChanged += h,
h => source.CollectionChanged -= h)
.Select
(
args =>
{
var changes = args.EventArgs;
switch (changes.Action)
{
case NotifyCollectionChangedAction.Add:
return changes.NewItems.OfType<TObject>()
.Select(t => new Change<TObject, TKey>(ChangeReason.Add, keySelector(t), t));
case NotifyCollectionChangedAction.Remove:
return changes.OldItems.OfType<TObject>()
.Select(t => new Change<TObject, TKey>(ChangeReason.Remove, keySelector(t), t));
case NotifyCollectionChangedAction.Replace:
{
return changes.NewItems.OfType<TObject>()
.Select((t, idx) =>
{
var old = changes.OldItems[idx];
return new Change<TObject, TKey>(ChangeReason.Update, keySelector(t), t, (TObject)old);
});
}
case NotifyCollectionChangedAction.Reset:
{
//Clear all from the cache and reload
var removes = cloneOfList.KeyValues.Select(t => new Change<TObject, TKey>(ChangeReason.Remove, t.Key, t.Value)).ToArray();
return removes.Concat(initialChangeSet());
}
default:
return null;
}
})
.Where(updates => updates != null)
.Select(updates => (IChangeSet<TObject, TKey>)new ChangeSet<TObject, TKey>(updates));
var initialChanges = initialChangeSet();
var cacheLoader=Observable.Return(initialChanges).Concat(sourceUpdates).PopulateInto(cloneOfList);
var subscriber = cloneOfList.Connect().SubscribeSafe(observer);
return new CompositeDisposable(cacheLoader, subscriber, cloneOfList);
}
);
}
}
}