Repository navigation
Expand file tree
/
Copy pathFilterController.cs
More file actions
99 lines (88 loc) · 3.1 KB
/
Copy pathFilterController.cs
File metadata and controls
99 lines (88 loc) · 3.1 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
using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
namespace DynamicData.Controllers
{
/// <summary>
/// Enables dynamic filtering of the stream
/// </summary>
/// <typeparam name="T"></typeparam>
public sealed class FilterController<T> : IDisposable
{
private readonly ISubject<Func<T, bool>> _filterSubject = new ReplaySubject<Func<T, bool>>(1);
private readonly ISubject<Func<T, bool>> _reevaluteSubject = new Subject<Func<T, bool>>();
private Func<T, bool> _filter;
/// <summary>
/// Initializes a new instance of the <see cref="FilterController{T}"/> class.
/// </summary>
/// <param name="defaultFilter">The default filter.</param>
public FilterController(Func<T, bool> defaultFilter = null)
{
_filter = defaultFilter ?? (t => true);
_filterSubject.OnNext(_filter);
}
/// <summary>
/// Change the current filter.
/// </summary>
/// <param name="filter">The filter. Set to null to include all items</param>
public void Change(Func<T, bool> filter = null)
{
_filter = filter ?? (t => true);
_filterSubject.OnNext(filter);
}
/// <summary>
/// Changes the filter to include all items
/// </summary>
public void ChangeToIncludeAll()
{
_filter = t => true;
_filterSubject.OnNext(_filter);
}
/// <summary>
/// Changes the filter to include all items
/// </summary>
public void ChangeToExcludeAll()
{
_filter = t => false;
_filterSubject.OnNext(_filter);
}
/// <summary>
/// Reevaluates all items.
/// </summary>
public void Reevaluate()
{
_filterSubject.OnNext(_filter);
}
/// <summary>
/// Evaluates the filter for items specified by the item selector.
/// </summary>
/// <param name="itemSelector">The item selector.</param>
public void Reevaluate(Func<T, bool> itemSelector)
{
if (itemSelector == null) throw new ArgumentNullException("itemSelector");
_reevaluteSubject.OnNext(itemSelector);
}
/// <summary>
/// Observable which is fired when the filter is changed
/// </summary>
/// <value>
/// The filter changed.
/// </value>
public IObservable<Func<T, bool>> FilterChanged => _filterSubject.AsObservable();
/// <summary>
/// Observable which is fired when the re-evaluate is invoked
/// </summary>
/// <value>
/// The evaluate changed.
/// </value>
public IObservable<Func<T, bool>> EvaluateChanged => _reevaluteSubject.AsObservable();
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
_filterSubject.OnCompleted();
_reevaluteSubject.OnCompleted();
}
}
}