|
1 | 1 | using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Linq; |
| 4 | +using System.Threading; |
3 | 5 | using System.Threading.Tasks; |
4 | 6 |
|
5 | 7 | namespace DotVVM.Framework.Controls |
6 | 8 | { |
7 | 9 | public static class GridViewDataSetExtensions |
8 | 10 | { |
9 | | - |
| 11 | + /// <summary> |
| 12 | + /// Loads dataset from the specified <paramref name="queryable" />: Applies filtering, sorting and paging options, and collects the results. If <see cref="PagingOptions"/> is used, the total number of items (after applying filtering) is retrieved and stored in the PagingOptions property. |
| 13 | + /// </summary> |
10 | 14 | public static void LoadFromQueryable<T>(this IGridViewDataSet<T> dataSet, IQueryable<T> queryable) |
11 | 15 | { |
12 | 16 | if (dataSet.FilteringOptions is not IApplyToQueryable filteringOptions) |
@@ -35,30 +39,85 @@ public static void LoadFromQueryable<T>(this IGridViewDataSet<T> dataSet, IQuery |
35 | 39 | dataSet.IsRefreshRequired = false; |
36 | 40 | } |
37 | 41 |
|
| 42 | +#if NET6_0_OR_GREATER |
| 43 | + /// <summary> |
| 44 | + /// Loads dataset from the specified <paramref name="queryable" />: Applies filtering, sorting and paging options, and collects the results using IAsyncEnumerable. If <see cref="PagingOptions"/> is used, the total number of items (after applying filtering) is retrieved and stored in the PagingOptions property. |
| 45 | + /// </summary> |
| 46 | + /// <exception cref="ArgumentException">The specified IQueryable does not support async enumeration.</exception> |
| 47 | + public static async Task LoadFromQueryableAsync<T>(this IGridViewDataSet<T> dataSet, IQueryable<T> queryable, CancellationToken cancellationToken = default) |
| 48 | + { |
| 49 | + if (dataSet.FilteringOptions is not IApplyToQueryable filteringOptions) |
| 50 | + { |
| 51 | + throw new ArgumentException($"The FilteringOptions of {dataSet.GetType()} must implement IApplyToQueryable!"); |
| 52 | + } |
| 53 | + if (dataSet.SortingOptions is not IApplyToQueryable sortingOptions) |
| 54 | + { |
| 55 | + throw new ArgumentException($"The SortingOptions of {dataSet.GetType()} must implement IApplyToQueryable!"); |
| 56 | + } |
| 57 | + if (dataSet.PagingOptions is not IApplyToQueryable pagingOptions) |
| 58 | + { |
| 59 | + throw new ArgumentException($"The PagingOptions of {dataSet.GetType()} must implement IApplyToQueryable!"); |
| 60 | + } |
| 61 | + |
| 62 | + var filtered = filteringOptions.ApplyToQueryable(queryable); |
| 63 | + var sorted = sortingOptions.ApplyToQueryable(filtered); |
| 64 | + var paged = pagingOptions.ApplyToQueryable(sorted); |
| 65 | + if (paged is not IAsyncEnumerable<T> asyncPaged) |
| 66 | + { |
| 67 | + throw new ArgumentException($"The specified IQueryable ({queryable.GetType().FullName}), does not support async enumeration. Please use the LoadFromQueryable method.", nameof(queryable)); |
| 68 | + } |
| 69 | + |
| 70 | + var result = new List<T>(); |
| 71 | + await foreach (var item in asyncPaged.WithCancellation(cancellationToken)) |
| 72 | + { |
| 73 | + result.Add(item); |
| 74 | + } |
| 75 | + dataSet.Items = result; |
| 76 | + |
| 77 | + if (pagingOptions is IPagingOptionsLoadingPostProcessor pagingOptionsLoadingPostProcessor) |
| 78 | + { |
| 79 | + cancellationToken.ThrowIfCancellationRequested(); |
| 80 | + await pagingOptionsLoadingPostProcessor.ProcessLoadedItemsAsync(filtered, result, cancellationToken); |
| 81 | + } |
| 82 | + |
| 83 | + dataSet.IsRefreshRequired = false; |
| 84 | + } |
| 85 | +#endif |
| 86 | + |
| 87 | + /// <summary> Sets <see cref="IPagingOptions"/> to the first page, and sets the <see cref="IRefreshableGridViewDataSet.IsRefreshRequired"/> property to true. </summary> |
| 88 | + /// <remarks> To reload the dataset, you must then call <see cref="LoadFromQueryable{T}(IGridViewDataSet{T}, IQueryable{T})"/> or a similar method. </remarks> |
38 | 89 | public static void GoToFirstPageAndRefresh(this IPageableGridViewDataSet<IPagingFirstPageCapability> dataSet) |
39 | 90 | { |
40 | 91 | dataSet.PagingOptions.GoToFirstPage(); |
41 | 92 | (dataSet as IRefreshableGridViewDataSet)?.RequestRefresh(); |
42 | 93 | } |
43 | 94 |
|
| 95 | + /// <summary> Sets <see cref="IPagingOptions"/> to the last page, and sets the <see cref="IRefreshableGridViewDataSet.IsRefreshRequired"/> property to true. </summary> |
| 96 | + /// <remarks> To reload the dataset, you must then call <see cref="LoadFromQueryable{T}(IGridViewDataSet{T}, IQueryable{T})"/> or a similar method. </remarks> |
44 | 97 | public static void GoToLastPageAndRefresh(this IPageableGridViewDataSet<IPagingLastPageCapability> dataSet) |
45 | 98 | { |
46 | 99 | dataSet.PagingOptions.GoToLastPage(); |
47 | 100 | (dataSet as IRefreshableGridViewDataSet)?.RequestRefresh(); |
48 | 101 | } |
49 | 102 |
|
| 103 | + /// <summary> Sets <see cref="IPagingOptions"/> to the previous page, and sets the <see cref="IRefreshableGridViewDataSet.IsRefreshRequired"/> property to true. </summary> |
| 104 | + /// <remarks> To reload the dataset, you must then call <see cref="LoadFromQueryable{T}(IGridViewDataSet{T}, IQueryable{T})"/> or a similar method. </remarks> |
50 | 105 | public static void GoToPreviousPageAndRefresh(this IPageableGridViewDataSet<IPagingPreviousPageCapability> dataSet) |
51 | 106 | { |
52 | 107 | dataSet.PagingOptions.GoToPreviousPage(); |
53 | 108 | (dataSet as IRefreshableGridViewDataSet)?.RequestRefresh(); |
54 | 109 | } |
55 | 110 |
|
| 111 | + /// <summary> Sets <see cref="IPagingOptions"/> to the next page, and sets the <see cref="IRefreshableGridViewDataSet.IsRefreshRequired"/> property to true. </summary> |
| 112 | + /// <remarks> To reload the dataset, you must then call <see cref="LoadFromQueryable{T}(IGridViewDataSet{T}, IQueryable{T})"/> or a similar method. </remarks> |
56 | 113 | public static void GoToNextPageAndRefresh(this IPageableGridViewDataSet<IPagingNextPageCapability> dataSet) |
57 | 114 | { |
58 | 115 | dataSet.PagingOptions.GoToNextPage(); |
59 | 116 | (dataSet as IRefreshableGridViewDataSet)?.RequestRefresh(); |
60 | 117 | } |
61 | 118 |
|
| 119 | + /// <summary> Sets <see cref="IPagingOptions"/> to the page number <paramref name="pageIndex"/> (indexed from 0), and sets the <see cref="IRefreshableGridViewDataSet.IsRefreshRequired"/> property to true. </summary> |
| 120 | + /// <remarks> To reload the dataset, you must then call <see cref="LoadFromQueryable{T}(IGridViewDataSet{T}, IQueryable{T})"/> or a similar method. </remarks> |
62 | 121 | public static void GoToPageAndRefresh(this IPageableGridViewDataSet<IPagingPageIndexCapability> dataSet, int pageIndex) |
63 | 122 | { |
64 | 123 | dataSet.PagingOptions.GoToPage(pageIndex); |
|
0 commit comments