Repository navigation
Expand file tree
/
Copy pathSortController.cs
More file actions
100 lines (87 loc) · 3.21 KB
/
Copy pathSortController.cs
File metadata and controls
100 lines (87 loc) · 3.21 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
using System;
using System.Collections.Generic;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
namespace DynamicData.Controllers
{
/// <summary>
/// Enables dynamic inline sorting
/// </summary>
/// <typeparam name="T"></typeparam>
public sealed class SortController<T> : IDisposable
{
private readonly ISubject<IComparer<T> > _sortSubject = new ReplaySubject<IComparer<T>>(1);
private readonly ISubject<Unit> _resortSubject = new Subject<Unit>();
private IComparer<T> _defaultSort;
private IComparer<T> _currentSort;
/// <summary>
/// Initializes a new instance of the <see cref="SortController{T}"/> class.
/// </summary>
/// <param name="defaultSort">The default sort.</param>
/// <exception cref="System.ArgumentNullException">defaultSort</exception>
public SortController(IComparer<T> defaultSort)
{
if (defaultSort == null) throw new ArgumentNullException("defaultSort");
SetDefaultSort(defaultSort);
}
/// <summary>
/// Sets the default sort.
/// </summary>
/// <param name="defaultSort">The default sort.</param>
/// <exception cref="System.ArgumentNullException">defaultSort</exception>
public void SetDefaultSort(IComparer<T> defaultSort)
{
if (defaultSort == null) throw new ArgumentNullException("defaultSort");
_defaultSort = defaultSort;
Change(defaultSort);
}
/// <summary>
/// Changes the sort back to the default comparer
/// </summary>
public void Reset()
{
Change(_defaultSort);
}
/// <summary>
/// Reapplies the current sort. Useful when a sorting on properties or methods which can dynamically change.
/// </summary>
public void Resort()
{
_resortSubject.OnNext(Unit.Default);
}
/// <summary>
/// Changes the sort comparer.
/// </summary>
/// <param name="comparer">The comparer.</param>
/// <exception cref="System.ArgumentNullException">comparer</exception>
public void Change(IComparer<T> comparer)
{
if (comparer == null) throw new ArgumentNullException("comparer");
_currentSort = comparer;
_sortSubject.OnNext(_currentSort);
}
/// <summary>
/// Observable which is fired when the sort comparer is changed
/// </summary>
/// <value>
/// The changed.
/// </value>
public IObservable<IComparer<T>> ComparerChanged => _sortSubject.AsObservable();
/// <summary>
/// Observable which is fired when the sort comparer is changed
/// </summary>
/// <value>
/// The changed.
/// </value>
public IObservable<Unit> SortAgain => _resortSubject.AsObservable();
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
_sortSubject.OnCompleted();
_resortSubject.OnCompleted();
}
}
}