Repository navigation
Expand file tree
/
Copy pathVirtualiser.cs
More file actions
83 lines (64 loc) · 2.85 KB
/
Copy pathVirtualiser.cs
File metadata and controls
83 lines (64 loc) · 2.85 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
using System.Linq;
using DynamicData.Internal;
using DynamicData.Operators;
namespace DynamicData
{
internal sealed class Virtualiser<TObject,TKey>
{
#region Fields
private readonly FilteredIndexCalculator<TObject, TKey> _changedCalculator = new FilteredIndexCalculator<TObject, TKey>();
private IKeyValueCollection<TObject, TKey> _all =new KeyValueCollection<TObject, TKey>();
private IKeyValueCollection<TObject, TKey> _current = new KeyValueCollection<TObject, TKey>();
private IVirtualRequest _parameters;
private bool _isLoaded;
#endregion
#region Construction
public Virtualiser(VirtualRequest request=null)
{
_parameters = request ?? new VirtualRequest();
}
#endregion
#region Virtualisation
/// <summary>
/// Virtualises using specified parameters. Returns null if there are no changed
/// </summary>
/// <param name="parameters">The parameters.</param>
/// <returns></returns>
public IVirtualChangeSet<TObject, TKey> Virtualise(IVirtualRequest parameters)
{
if (parameters==null || parameters.StartIndex < 0 || parameters.Size < 1)
{
return null;
}
if (parameters.Size == _parameters.Size && parameters.StartIndex == _parameters.StartIndex)
return null;
_parameters = parameters;
return Virtualise();
}
public IVirtualChangeSet<TObject, TKey> Update(ISortedChangeSet<TObject, TKey> updates)
{
_isLoaded = true;
_all = updates.SortedItems;
return Virtualise( updates);
}
private IVirtualChangeSet<TObject, TKey> Virtualise(ISortedChangeSet<TObject, TKey> updates = null)
{
if (_isLoaded == false) return null;
var previous = _current;
var virualised = _all.Skip(_parameters.StartIndex)
.Take(_parameters.Size)
.ToList();
_current = new KeyValueCollection<TObject, TKey>(virualised, _all.Comparer, updates?.SortedItems.SortReason ?? SortReason.DataChanged, _all.Optimisations);
////check for changes within the current virtualised page. Notify if there have been changes or if the overall count has changed
var notifications = _changedCalculator.Calculate(_current, previous, updates);
if (notifications.Count == 0 && (previous.Count != _current.Count))
{
return null;
}
var response = new VirtualResponse(_parameters.Size, _parameters.StartIndex, _all.Count);
return new VirtualChangeSet<TObject, TKey>(notifications, _current, response);
}
#endregion
}
}