/
MainWindow.xaml.cs
115 lines (100 loc) · 4.65 KB
/
MainWindow.xaml.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using DevExpress.Data.Filtering;
using DevExpress.Xpf.Data;
using System;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace InfiniteAsyncSourceSample {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
var source = new InfiniteAsyncSource() {
ElementType = typeof(IssueData),
KeyProperty = "Id"
};
Unloaded += (o, e) => {
source.Dispose();
};
source.FetchRows += (o, e) => {
e.Result = FetchRowsAsync(e);
};
source.GetUniqueValues += (o, e) => {
if(e.PropertyName == "Priority") {
var values = Enum.GetValues(typeof(Priority)).Cast<object>().ToArray();
e.Result = Task.FromResult(values);
} else {
throw new InvalidOperationException();
}
};
source.GetTotalSummaries += (o, e) => {
e.Result = GetTotalSummariesAsync(e);
};
grid.ItemsSource = source;
}
static async Task<FetchRowsResult> FetchRowsAsync(FetchRowsAsyncEventArgs e) {
IssueSortOrder sortOrder = GetIssueSortOrder(e);
IssueFilter filter = MakeIssueFilter(e.Filter);
var take = e.Take ?? 30;
var issues = await IssuesService.GetIssuesAsync(
skip: e.Skip,
take: take,
sortOrder: sortOrder,
filter: filter);
return new FetchRowsResult(issues, hasMoreRows: issues.Length == take);
}
static async Task<object[]> GetTotalSummariesAsync(GetSummariesAsyncEventArgs e) {
IssueFilter filter = MakeIssueFilter(e.Filter);
var summaryValues = await IssuesService.GetSummariesAsync(filter);
return e.Summaries.Select(x => {
if(x.SummaryType == SummaryType.Count)
return (object)summaryValues.Count;
if(x.SummaryType == SummaryType.Max && x.PropertyName == "Created")
return summaryValues.LastCreated;
throw new InvalidOperationException();
}).ToArray();
}
static IssueSortOrder GetIssueSortOrder(FetchRowsAsyncEventArgs e) {
if(e.SortOrder.Length > 0) {
var sort = e.SortOrder.Single();
if(sort.PropertyName == "Created") {
if(sort.Direction != ListSortDirection.Descending)
throw new InvalidOperationException();
return IssueSortOrder.CreatedDescending;
}
if(sort.PropertyName == "Votes") {
return sort.Direction == ListSortDirection.Ascending
? IssueSortOrder.VotesAscending
: IssueSortOrder.VotesDescending;
}
}
return IssueSortOrder.Default;
}
static IssueFilter MakeIssueFilter(CriteriaOperator filter) {
return filter.Match(
binary: (propertyName, value, type) => {
if(propertyName == "Votes" && type == BinaryOperatorType.GreaterOrEqual)
return new IssueFilter(minVotes: (int)value);
if(propertyName == "Priority" && type == BinaryOperatorType.Equal)
return new IssueFilter(priority: (Priority)value);
if(propertyName == "Created") {
if(type == BinaryOperatorType.GreaterOrEqual)
return new IssueFilter(createdFrom: (DateTime)value);
if(type == BinaryOperatorType.Less)
return new IssueFilter(createdTo: (DateTime)value);
}
throw new InvalidOperationException();
},
and: filters => {
return new IssueFilter(
createdFrom: filters.Select(x => x.CreatedFrom).SingleOrDefault(x => x != null),
createdTo: filters.Select(x => x.CreatedTo).SingleOrDefault(x => x != null),
minVotes: filters.Select(x => x.MinVotes).SingleOrDefault(x => x != null),
priority: filters.Select(x => x.Priority).SingleOrDefault(x => x != null)
);
},
@null: default(IssueFilter)
);
}
}
}