-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortingOptions.cs
92 lines (83 loc) · 2.68 KB
/
SortingOptions.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace DirectSFTP
{
public enum SortingStyle
{
Ascending,
Descending
}
public class SortingOptions<T>
{
private ContentPage page;
public SortingStyle SortingStyle { get; private set; }
public string CurSorting { get; private set; }
public Dictionary<string,Tuple<Func<T,IComparable>,MenuFlyoutItem>> SortOptions { get; private set; }
private MenuBarItem menuItem;
public EventHandler ChangedOption;
public SortingOptions(ContentPage page) {
this.page = page;
SortingStyle = SortingStyle.Ascending;
SortOptions = new();
CurSorting = null;
menuItem = new MenuBarItem()
{
Text = "Sort by"
};
}
public void Show()
{
page.MenuBarItems.Add(menuItem);
}
public void AddSortOption(Func<T,IComparable> keySelector, string optionName)
{
MenuFlyoutItem newItem = null;
newItem = new()
{
Text = optionName,
Command = new Command(() => {
if (CurSorting != null) {
SortOptions[CurSorting].Item2.Text = CurSorting; // remove selected symbol
}
if (CurSorting == optionName)
{
if (SortingStyle == SortingStyle.Ascending) SortingStyle = SortingStyle.Descending;
else SortingStyle = SortingStyle.Ascending;
}
newItem.Text = optionName + " " + GetSelectedSymbol();
CurSorting = optionName;
ChangedOption?.Invoke(this, EventArgs.Empty);
})
};
SortOptions.Add(optionName, new(keySelector,newItem));
menuItem.Add(newItem);
}
public IOrderedEnumerable<T> Sort(IEnumerable<T> items)
{
if (SortingStyle == SortingStyle.Ascending)
{
return items.OrderBy(SortOptions[CurSorting].Item1);
}
else
{
return items.OrderByDescending(SortOptions[CurSorting].Item1);
}
}
private char GetSelectedSymbol()
{
if (SortingStyle == SortingStyle.Ascending)
{
return '▲';
}
else
{
return '▼';
}
}
}
}