Skip to content

Commit 0938fbf

Browse files
committed
Implemented proper sorting capabilities
1 parent 92ac9e2 commit 0938fbf

19 files changed

+120
-88
lines changed

MsGraphSamples.Services/ExtensionMethods.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace MsGraphSamples;
66
public static class ExtensionMethods
77
{
88
public static bool In(this string s, params string[] items) => items.Any(i => i.Trim().Equals(s, StringComparison.InvariantCultureIgnoreCase));
9+
public static bool IsNullOrEmpty(this string? s) => string.IsNullOrEmpty(s);
910

1011
public static int NthIndexOf(this string input, char value, int nth, int startIndex = 0)
1112
{

MsGraphSamples.Services/GraphDataService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public class GraphDataService(GraphServiceClient graphClient) : IGraphDataServic
7070
return collectionResponse.GetNextPageAsync(_graphClient.RequestAdapter);
7171
}
7272

73-
7473
public Task<ApplicationCollectionResponse?> GetApplicationCollectionAsync(string[] select, string? filter = null, string[]? orderBy = null, string? search = null, ushort top = 999)
7574
{
7675
var requestInfo = _graphClient.Applications
@@ -207,6 +206,7 @@ public class GraphDataService(GraphServiceClient graphClient) : IGraphDataServic
207206
LastUrl = WebUtility.UrlDecode(requestInfo.URI.AbsoluteUri);
208207
return _graphClient.RequestAdapter.SendAsync(requestInfo, UserCollectionResponse.CreateFromDiscriminatorValue);
209208
}
209+
210210
public Task<UserCollectionResponse?> GetGroupOwnersAsUserCollectionAsync(string id, string[] select, ushort top = 999)
211211
{
212212
var requestInfo = _graphClient.Groups[id]
@@ -223,7 +223,6 @@ public class GraphDataService(GraphServiceClient graphClient) : IGraphDataServic
223223
return _graphClient.RequestAdapter.SendAsync(requestInfo, UserCollectionResponse.CreateFromDiscriminatorValue);
224224
}
225225

226-
227226
public Task<GroupCollectionResponse?> GetTransitiveMemberOfAsGroupCollectionAsync(string id, string[] select, ushort top = 999)
228227
{
229228
var requestInfo = _graphClient.Users[id]
@@ -262,5 +261,4 @@ public Task WriteExtensionProperty(string propertyName, object propertyValue, st
262261
userRequestBody.AdditionalData[propertyName] = propertyValue;
263262
return _graphClient.Users[userId].PatchAsync(userRequestBody);
264263
}
265-
266264
}

MsGraphSamples.WPF/Helpers/Converters.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
using Microsoft.Graph.Models;
5-
using Microsoft.Kiota.Abstractions.Store;
65
using System.Globalization;
76
using System.Windows.Data;
87

MsGraphSamples.WPF/ViewModels/MainViewModel.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using System.ComponentModel;
45
using System.Diagnostics;
56
using System.Net;
67
using System.Text;
@@ -50,7 +51,7 @@ public partial class MainViewModel(IAuthService authService, IGraphDataService g
5051
public string _select = "id,displayName,mail,userPrincipalName";
5152

5253
[ObservableProperty]
53-
public string? _filter = string.Empty;
54+
public string? _filter;
5455

5556
public string[]? SplittedOrderBy => OrderBy?.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
5657

@@ -101,7 +102,8 @@ public string? Search
101102

102103
#endregion
103104

104-
public async Task Init()
105+
[RelayCommand]
106+
public async Task PageLoaded()
105107
{
106108
var user = await graphDataService.GetUserAsync(["displayName"]);
107109
UserName = user?.DisplayName;
@@ -131,9 +133,9 @@ private async Task DrillDown()
131133

132134
await IsBusyWrapper(async () =>
133135
{
134-
OrderBy = string.Empty;
135-
Filter = string.Empty;
136-
Search = string.Empty;
136+
OrderBy = null;
137+
Filter = null;
138+
Search = null;
137139

138140
return DirectoryObjects switch
139141
{
@@ -165,12 +167,12 @@ private Task LoadNextPage()
165167
}
166168

167169
[RelayCommand]
168-
private Task Sort(DataGridSortingEventArgs? e)
170+
private Task Sort(DataGridSortingEventArgs e)
169171
{
170-
ArgumentNullException.ThrowIfNull(e);
172+
OrderBy = e.Column.SortDirection == null || e.Column.SortDirection == ListSortDirection.Descending
173+
? $"{e.Column.Header} asc"
174+
: $"{e.Column.Header} desc";
171175

172-
OrderBy = (string)e.Column.Header;
173-
e.Handled = true;
174176
return Load();
175177
}
176178

@@ -200,7 +202,6 @@ private void Logout()
200202
App.Current.Shutdown();
201203
}
202204

203-
204205
private async Task IsBusyWrapper(Func<Task<BaseCollectionPaginationCountResponse?>> getDirectoryObjects)
205206
{
206207
IsBusy = true;

MsGraphSamples.WPF/Views/MainWindow.xaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@
3737
d:DataContext="{d:DesignInstance Type=viewmodels:MainViewModel}"
3838
DataContext="{Binding MainVM, Source={StaticResource Locator}}"
3939
Icon="/Assets/MSGraph.png"
40-
Initialized="Window_Initialized"
41-
Loaded="Window_Loaded"
4240
WindowStartupLocation="CenterScreen"
4341
mc:Ignorable="d">
42+
<i:Interaction.Triggers>
43+
<i:EventTrigger EventName="Loaded">
44+
<i:InvokeCommandAction Command="{Binding PageLoadedCommand}" />
45+
</i:EventTrigger>
46+
</i:Interaction.Triggers>
4447

4548
<Grid>
4649
<Grid.Resources>

MsGraphSamples.WPF/Views/MainWindow.xaml.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using MsGraphSamples.WPF.Helpers;
54
using MsGraphSamples.WPF.ViewModels;
5+
using System.ComponentModel;
66
using System.Windows;
77
using System.Windows.Controls;
88
using System.Windows.Input;
@@ -50,6 +50,15 @@ private void ResultsDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGen
5050
if (!e.Cancel)
5151
{
5252
e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
53+
54+
var orderByProperty = ViewModel.OrderBy?.Split(' ')[0];
55+
var direction = ViewModel.OrderBy?.Split(' ').ElementAtOrDefault(1) ?? "asc";
56+
if (e.PropertyName.Equals(orderByProperty, StringComparison.InvariantCultureIgnoreCase))
57+
{
58+
e.Column.SortDirection = direction.Equals("asc", StringComparison.InvariantCultureIgnoreCase)
59+
? ListSortDirection.Ascending
60+
: ListSortDirection.Descending;
61+
}
5362
}
5463
}
5564

@@ -63,14 +72,4 @@ private void ResultsDataGrid_AutoGeneratedColumns(object sender, System.EventArg
6372
p => p.Equals(column.Header.ToString(), StringComparison.OrdinalIgnoreCase));
6473
}
6574
}
66-
67-
private void Window_Loaded(object sender, RoutedEventArgs e)
68-
{
69-
//Dispatcher.InvokeAsync(ViewModel.Init);
70-
}
71-
72-
private void Window_Initialized(object sender, EventArgs e)
73-
{
74-
ViewModel.Init().Await();
75-
}
7675
}

MsGraphSamples.WinUI/App.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ private static ServiceProvider GetServices()
5151
protected override void OnLaunched(LaunchActivatedEventArgs args)
5252
{
5353
var m_window = new MainWindow();
54+
m_window.ExtendsContentIntoTitleBar = true;
5455

5556
// Create a Frame to act as the navigation context and navigate to the first page
5657
var rootFrame = new Frame();
24.7 KB
Loading
10 KB
Loading
5.54 KB
Loading

0 commit comments

Comments
 (0)