Repository navigation
Expand file tree
/
Copy pathMutableFilter.cs
More file actions
142 lines (113 loc) · 4.07 KB
/
Copy pathMutableFilter.cs
File metadata and controls
142 lines (113 loc) · 4.07 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using DynamicData.Annotations;
using DynamicData.Controllers;
using DynamicData.Kernel;
namespace DynamicData.Internal
{
//TODO: Implement seperate ClearAndReplace and CalculateDiffSet filters??
internal class MutableFilter<T>
{
private readonly List<ItemWithMatch> _allWithMatch = new List<ItemWithMatch>();
private readonly List<T> _all = new List<T>();
private readonly ChangeAwareList<T> _filtered = new ChangeAwareList<T>();
private readonly IObservable<IChangeSet<T>> _source;
private readonly FilterController<T> _controller;
private readonly FilterPolicy _filterPolicy;
private Func<T, bool> _predicate=t=>false;
public MutableFilter([NotNull] IObservable<IChangeSet<T>> source, [NotNull] FilterController<T> controller, FilterPolicy filterPolicy= FilterPolicy.ClearAndReplace)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (controller == null) throw new ArgumentNullException(nameof(controller));
_source = source;
_controller = controller;
_filterPolicy = filterPolicy;
}
public IObservable<IChangeSet<T>> Run()
{
return Observable.Create<IChangeSet<T>>(observer =>
{
var locker = new object();
var requery = _controller.FilterChanged.Synchronize(locker);
var reevaluate = _controller.EvaluateChanged.Synchronize(locker);
//requery wehn controller either fires changed or requery event
var refresher = requery.Merge(reevaluate)
.Select(predicate =>
{
Requery(predicate);
return _filtered.CaptureChanges();
});
var shared = _source.Synchronize(locker).Publish();
//take current filter state of all items
IDisposable updateall;
if (_filterPolicy == FilterPolicy.ClearAndReplace)
{
updateall = shared.Synchronize(locker)
.Subscribe(_all.Clone);
}
else
{
updateall = shared.Synchronize(locker)
.Transform(t => new ItemWithMatch(t, _predicate(t)))
.Subscribe(_allWithMatch.Clone);
}
//filter result list
var filter = shared.Synchronize(locker)
.Select(changes =>
{
_filtered.Filter(changes, _predicate);
return _filtered.CaptureChanges();
});
var subscriber = refresher.Merge(filter).NotEmpty().SubscribeSafe(observer);
return new CompositeDisposable(updateall, subscriber, shared.Connect());
});
}
//TODO: Need to account for re-evaluate (as it is not mutually excluse to clear and replace)
private void Requery(Func<T, bool> predicate)
{
_predicate = predicate;
if (_filterPolicy == FilterPolicy.ClearAndReplace)
{
_filtered.Clear();
_filtered.AddRange(_all.Where(_predicate));
return;
}
var newState = _allWithMatch.Select(item =>
{
var match = _predicate(item.Item);
var wasMatch = item.IsMatch;
//reflect filtered state
if (item.IsMatch != match) item.IsMatch = match;
return new
{
Item = item,
IsMatch = match,
WasMatch = wasMatch
};
}).ToList();
//reflect items which are no longer matched
//TODO:can we determine whether removes are remove all? i.e. Then we can send a 'Clear' reason message (or Remove range?)
var noLongerMatched = newState.Where(state => !state.IsMatch && state.WasMatch).Select(state => state.Item);
noLongerMatched.ForEach(state =>
{
_filtered.Remove(state.Item);
});
//reflect new matches in the list
var newMatched = newState.Where(state => state.IsMatch && !state.WasMatch).Select(state => state.Item.Item);
_filtered.AddRange(newMatched);
}
private class ItemWithMatch
{
public T Item { get; }
public bool IsMatch { get; set; }
public ItemWithMatch(T item, bool isMatch)
{
Item = item;
IsMatch = isMatch;
}
}
}
}