-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathObservableList.cs
410 lines (339 loc) · 9.57 KB
/
ObservableList.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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
// ReSharper disable once CheckNamespace
namespace GameLovers
{
/// <summary>
/// A list with the possibility to observe changes to it's elements defined <see cref="ObservableUpdateType"/> rules
/// </summary>
public interface IObservableListReader : IEnumerable
{
/// <summary>
/// Requests the list element count
/// </summary>
int Count { get; }
}
/// <inheritdoc cref="IObservableListReader"/>
/// <remarks>
/// Read only observable list interface
/// </remarks>
public interface IObservableListReader<T> : IObservableListReader, IEnumerable<T>
{
/// <summary>
/// Looks up and return the data that is associated with the given <paramref name="index"/>
/// </summary>
T this[int index] { get; }
/// <summary>
/// Requests this list as a <see cref="IReadOnlyList{T}"/>
/// </summary>
IReadOnlyList<T> ReadOnlyList { get; }
/// <inheritdoc cref="List{T}.Contains"/>
bool Contains(T value);
/// <inheritdoc cref="List{T}.IndexOf(T)"/>
int IndexOf(T value);
/// <summary>
/// Observes to this list changes with the given <paramref name="onUpdate"/>
/// </summary>
void Observe(Action<int, T, T, ObservableUpdateType> onUpdate);
/// <summary>
/// Observes this list with the given <paramref name="onUpdate"/> when any data changes and invokes it with the given <paramref name="index"/>
/// </summary>
void InvokeObserve(int index, Action<int, T, T, ObservableUpdateType> onUpdate);
/// <summary>
/// Stops observing this dictionary with the given <paramref name="onUpdate"/> of any data changes
/// </summary>
void StopObserving(Action<int, T, T, ObservableUpdateType> onUpdate);
/// <summary>
/// Stops observing this dictionary changes from all the given <paramref name="subscriber"/> calls.
/// If the given <paramref name="subscriber"/> is null then will stop observing from everything.
/// </summary>
void StopObservingAll(object subscriber = null);
}
/// <inheritdoc />
public interface IObservableList<T> : IObservableListReader<T>
{
/// <summary>
/// Changes the given <paramref name="index"/> in the list. If the data does not exist it will be added.
/// It will notify any observer listing to its data
/// </summary>
new T this[int index] { get; set; }
/// <inheritdoc cref="List{T}.Add"/>
void Add(T data);
/// <inheritdoc cref="List{T}.Remove"/>
bool Remove(T data);
/// <inheritdoc cref="List{T}.RemoveAt"/>
void RemoveAt(int index);
/// <inheritdoc cref="List{T}.Clear"/>
void Clear();
/// <remarks>
/// It invokes any update method that is observing to the given <paramref name="index"/> on this list
/// </remarks>
void InvokeUpdate(int index);
}
/// <inheritdoc />
/// <remarks>
/// This interface resolves between 2 lists with different types of values
/// </remarks>
public interface IObservableResolverListReader<T, TOrigin> : IObservableListReader<T>
{
/// <summary>
/// The Original List that is being resolved across the entire interface
/// </summary>
IReadOnlyList<TOrigin> OriginList { get; }
}
/// <inheritdoc />
/// <remarks>
/// This interface resolves between 2 lists with different types of values
/// </remarks>
public interface IObservableResolverList<T, TOrigin> :
IObservableResolverListReader<T, TOrigin>,
IObservableList<T>
{
/// <summary>
/// Updates the value in the origin list corresponding to the specified index.
/// </summary>
/// <param name="index">The index of the value to update in the origin list.</param>
/// <param name="value">The new value to set in the origin list.</param>
void UpdateOrigin(TOrigin value, int index);
/// <inheritdoc cref="List{T}.Add"/>
/// <remarks>
/// Add's the value to the origin list
/// </remarks>
void AddOrigin(TOrigin value);
/// <inheritdoc cref="List{T}.Remove"/>
/// <remarks>
/// Remove's the value to the origin list
/// </remarks>
bool RemoveOrigin(TOrigin value);
/// <inheritdoc cref="List{T}.Clear"/>
/// <remarks>
/// Clear's to the origin list
/// </remarks>
void ClearOrigin();
}
/// <inheritdoc />
public class ObservableList<T> : IObservableList<T>
{
private readonly IList<Action<int, T, T, ObservableUpdateType>> _updateActions = new List<Action<int, T, T, ObservableUpdateType>>();
/// <inheritdoc cref="IObservableList{T}.this" />
public T this[int index]
{
get => List[index];
set
{
var previousValue = List[index];
List[index] = value;
InvokeUpdate(index, previousValue);
}
}
/// <inheritdoc />
public int Count => List.Count;
/// <inheritdoc />
public IReadOnlyList<T> ReadOnlyList => new List<T>(List);
protected virtual List<T> List { get; }
protected ObservableList() { }
public ObservableList(IList<T> list)
{
List = list as List<T> ?? list.ToList();
}
/// <inheritdoc cref="List{T}.GetEnumerator"/>
public List<T>.Enumerator GetEnumerator()
{
return List.GetEnumerator();
}
/// <inheritdoc />
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return List.GetEnumerator();
}
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator()
{
return List.GetEnumerator();
}
/// <inheritdoc />
public bool Contains(T value)
{
return List.Contains(value);
}
/// <inheritdoc />
public int IndexOf(T value)
{
return List.IndexOf(value);
}
/// <inheritdoc />
public virtual void Add(T data)
{
List.Add(data);
for (var i = 0; i < _updateActions.Count; i++)
{
_updateActions[i](List.Count - 1, default, data, ObservableUpdateType.Added);
}
}
/// <inheritdoc />
public bool Remove(T data)
{
var idx = List.IndexOf(data);
if (idx >= 0)
{
RemoveAt(idx);
return true;
}
return false;
}
/// <inheritdoc />
public virtual void RemoveAt(int index)
{
var data = List[index];
List.RemoveAt(index);
for (var i = _updateActions.Count - 1; i > -1; i--)
{
var action = _updateActions[i];
action(index, data, default, ObservableUpdateType.Removed);
// Shift the index if an action was unsubscribed
i = AdjustIndex(i, action);
}
}
/// <inheritdoc />
public virtual void Clear()
{
// Create a copy in case that one of the callbacks modifies the list (Ex: removing a subscriber)
var copy = _updateActions.ToList();
for (var i = copy.Count - 1; i > -1; i--)
{
for (var j = 0; j < List.Count; j++)
{
copy[i](j, List[j], default, ObservableUpdateType.Removed);
}
}
List.Clear();
}
/// <inheritdoc />
public void InvokeUpdate(int index)
{
InvokeUpdate(index, List[index]);
}
/// <inheritdoc />
public void Observe(Action<int, T, T, ObservableUpdateType> onUpdate)
{
_updateActions.Add(onUpdate);
}
/// <inheritdoc />
public void InvokeObserve(int index, Action<int, T, T, ObservableUpdateType> onUpdate)
{
Observe(onUpdate);
InvokeUpdate(index);
}
/// <inheritdoc />
public void StopObserving(Action<int, T, T, ObservableUpdateType> onUpdate)
{
_updateActions.Remove(onUpdate);
}
/// <inheritdoc />
public void StopObservingAll(object subscriber = null)
{
if (subscriber == null)
{
_updateActions.Clear();
return;
}
for (var i = _updateActions.Count - 1; i > -1; i--)
{
if (_updateActions[i].Target == subscriber)
{
_updateActions.RemoveAt(i);
}
}
}
protected void InvokeUpdate(int index, T previousValue)
{
var data = List[index];
for (var i = 0; i < _updateActions.Count; i++)
{
_updateActions[i](index, previousValue, data, ObservableUpdateType.Updated);
}
}
private int AdjustIndex(int index, Action<int, T, T, ObservableUpdateType> action)
{
if (index < _updateActions.Count && _updateActions[index] == action)
{
return index;
}
for (var i = index - 1; i > -1; i--)
{
if (_updateActions[i] == action)
{
return i;
}
}
return index + 1;
}
}
/// <inheritdoc />
public class ObservableResolverList<T, TOrigin> : ObservableList<T>, IObservableResolverList<T, TOrigin>
{
private readonly IList<TOrigin> _originList;
private readonly Func<TOrigin, T> _fromOrignResolver;
private readonly Func<T, TOrigin> _toOrignResolver;
/// <inheritdoc />
public IReadOnlyList<TOrigin> OriginList => new List<TOrigin>(_originList);
public ObservableResolverList(IList<TOrigin> originList,
Func<TOrigin, T> fromOrignResolver,
Func<T, TOrigin> toOrignResolver) :
base(new List<T>(originList.Count))
{
_originList = originList;
_fromOrignResolver = fromOrignResolver;
_toOrignResolver = toOrignResolver;
for (var i = 0; i < originList.Count; i++)
{
List.Add(fromOrignResolver(originList[i]));
}
}
/// <inheritdoc />
public override void Add(T data)
{
_originList.Add(_toOrignResolver(data));
base.Add(data);
}
/// <inheritdoc />
public override void RemoveAt(int index)
{
_originList.RemoveAt(index);
base.RemoveAt(index);
}
/// <inheritdoc />
public override void Clear()
{
_originList.Clear();
base.Clear();
}
/// <inheritdoc />
public void UpdateOrigin(TOrigin value, int index)
{
_originList[index] = value;
List[index] = _fromOrignResolver(value);
}
/// <inheritdoc />
public void AddOrigin(TOrigin value)
{
_originList.Add(value);
List.Add(_fromOrignResolver(value));
}
/// <inheritdoc />
public bool RemoveOrigin(TOrigin value)
{
_originList.Remove(value);
return base.Remove(_fromOrignResolver(value));
}
/// <inheritdoc />
public void ClearOrigin()
{
_originList.Clear();
base.Clear();
}
}
}