Skip to content

Commit d62533a

Browse files
authored
Merge pull request #250 from jeffsieu/sorting-function
Add sorting to PhotoAlbum
2 parents 5f82451 + 3102a18 commit d62533a

File tree

7 files changed

+194
-81
lines changed

7 files changed

+194
-81
lines changed

Files UWP/Enums/SortOption.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Files.Enums
2+
{
3+
public enum SortOption
4+
{
5+
Name,
6+
DateModified,
7+
Size,
8+
FileType
9+
}
10+
}

Files UWP/FilesUWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
<Compile Include="Dialogs\RenameDialog.xaml.cs">
141141
<DependentUpon>RenameDialog.xaml</DependentUpon>
142142
</Compile>
143+
<Compile Include="Enums\SortOption.cs" />
143144
<Compile Include="Enums\ThemeStyle.cs" />
144145
<Compile Include="Enums\TimeStyle.cs" />
145146
<Compile Include="Filesystem\DriveItem.cs" />

Files UWP/Filesystem/ItemViewModel.cs

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using ByteSizeLib;
2-
using Files.Interacts;
2+
using Files.Enums;
33
using Files.Navigation;
44
using Microsoft.Toolkit.Uwp.UI;
55
using Microsoft.UI.Xaml.Controls;
@@ -45,9 +45,43 @@ public class ItemViewModel
4545
private volatile bool _filesRefreshing;
4646
private const int _step = 250;
4747

48+
private SortOption _directorySortOption = SortOption.Name;
49+
private SortDirection _directorySortDirection = SortDirection.Ascending;
50+
51+
public SortOption DirectorySortOption
52+
{
53+
get
54+
{
55+
return _directorySortOption;
56+
}
57+
set
58+
{
59+
if (value != _directorySortOption)
60+
{
61+
_directorySortOption = value;
62+
OrderFiles();
63+
}
64+
}
65+
}
66+
67+
public SortDirection DirectorySortDirection
68+
{
69+
get
70+
{
71+
return _directorySortDirection;
72+
}
73+
set
74+
{
75+
if (value != _directorySortDirection)
76+
{
77+
_directorySortDirection = value;
78+
OrderFiles();
79+
}
80+
}
81+
}
82+
4883
public ItemViewModel()
4984
{
50-
5185
_filesAndFolders = new ObservableCollection<ListedItem>();
5286

5387
FilesAndFolders = new ReadOnlyObservableCollection<ListedItem>(_filesAndFolders);
@@ -250,26 +284,27 @@ public void CancelLoadAndClearFiles()
250284

251285
}
252286

253-
public void OrderFiles(string orderBy, bool ascending)
287+
public void OrderFiles()
254288
{
255-
Func<ListedItem, object> orderFunc;
289+
if (_filesAndFolders.Count == 0)
290+
return;
291+
256292
object orderByNameFunc(ListedItem item) => item.FileName;
257-
switch (orderBy)
293+
Func<ListedItem, object> orderFunc = orderByNameFunc;
294+
switch (DirectorySortOption)
258295
{
259-
case "Name":
296+
case SortOption.Name:
260297
orderFunc = orderByNameFunc;
261298
break;
262-
case "Date":
299+
case SortOption.DateModified:
263300
orderFunc = item => item.FileDateReal;
264301
break;
265-
case "Type":
302+
case SortOption.FileType:
266303
orderFunc = item => item.FileType;
267304
break;
268-
case "Size":
305+
case SortOption.Size:
269306
orderFunc = item => item.FileSizeBytes;
270307
break;
271-
default:
272-
return;
273308
}
274309

275310
// In ascending order, show folders first, then files.
@@ -278,20 +313,20 @@ public void OrderFiles(string orderBy, bool ascending)
278313
IOrderedEnumerable<ListedItem> ordered;
279314
List<ListedItem> orderedList;
280315

281-
if (ascending)
316+
if (DirectorySortDirection == SortDirection.Ascending)
282317
ordered = _filesAndFolders.OrderBy(folderThenFile).ThenBy(orderFunc);
283318
else
284319
{
285-
if (orderBy == "Type")
320+
if (DirectorySortOption == SortOption.FileType)
286321
ordered = _filesAndFolders.OrderBy(folderThenFile).ThenByDescending(orderFunc);
287322
else
288323
ordered = _filesAndFolders.OrderByDescending(folderThenFile).ThenByDescending(orderFunc);
289324
}
290325

291326
// Further order by name if applicable
292-
if (orderBy != "Name")
327+
if (DirectorySortOption != SortOption.Name)
293328
{
294-
if (ascending)
329+
if (DirectorySortDirection == SortDirection.Ascending)
295330
ordered = ordered.ThenBy(orderByNameFunc);
296331
else
297332
ordered = ordered.ThenByDescending(orderByNameFunc);
@@ -487,6 +522,7 @@ public async void AddItemsToCollectionAsync(string path)
487522
(App.selectedTabInstance.accessibleContentFrame.Content as PhotoAlbum).TextState.isVisible = Visibility.Visible;
488523
}
489524
}
525+
OrderFiles();
490526
stopwatch.Stop();
491527
Debug.WriteLine("Loading of items in " + Universal.path + " completed in " + stopwatch.ElapsedMilliseconds + " milliseconds.\n");
492528
App.selectedTabInstance.RefreshButton.IsEnabled = true;

Files UWP/GenericFileBrowser.xaml.cs

Lines changed: 84 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
using Microsoft.Toolkit.Uwp.UI.Controls;
1+
using Microsoft.Toolkit.Uwp.UI;
2+
using Microsoft.Toolkit.Uwp.UI.Controls;
23
using System;
34
using System.ComponentModel;
45
using Windows.ApplicationModel.DataTransfer;
56
using Windows.Storage;
67
using Windows.UI.Xaml;
78
using Windows.UI.Xaml.Controls;
89
using Windows.UI.Xaml.Navigation;
10+
using Files.Enums;
911
using Files.Filesystem;
10-
using Files.Navigation;
1112
using Files.Interacts;
1213
using System.Diagnostics;
1314
using Windows.UI.Core;
@@ -21,7 +22,6 @@ public sealed partial class GenericFileBrowser : Page, INotifyPropertyChanged
2122
public TextBlock emptyTextGFB;
2223
public TextBlock textBlock;
2324
public DataGrid data;
24-
public DataGridColumn sortedColumn;
2525
public MenuFlyout context;
2626
public MenuFlyout emptySpaceContext;
2727
public MenuFlyout HeaderContextMenu;
@@ -33,14 +33,74 @@ public sealed partial class GenericFileBrowser : Page, INotifyPropertyChanged
3333
public Flyout CopiedFlyout;
3434
public Grid grid;
3535
public ProgressBar progressBar;
36+
private DataGridColumn _sortedColumn;
3637
ItemViewModel viewModelInstance;
3738
ProHome tabInstance;
38-
bool isSortedAscending;
3939

4040
public event PropertyChangedEventHandler PropertyChanged;
4141

4242
public EmptyFolderTextState TextState { get; set; } = new EmptyFolderTextState();
4343

44+
public DataGridColumn SortedColumn
45+
{
46+
get
47+
{
48+
return _sortedColumn;
49+
}
50+
set
51+
{
52+
if (value == nameColumn)
53+
viewModelInstance.DirectorySortOption = SortOption.Name;
54+
else if (value == dateColumn)
55+
viewModelInstance.DirectorySortOption = SortOption.DateModified;
56+
else if (value == typeColumn)
57+
viewModelInstance.DirectorySortOption = SortOption.FileType;
58+
else if (value == sizeColumn)
59+
viewModelInstance.DirectorySortOption = SortOption.Size;
60+
else
61+
viewModelInstance.DirectorySortOption = SortOption.Name;
62+
63+
if (value != _sortedColumn)
64+
{
65+
// Remove arrow on previous sorted column
66+
if (_sortedColumn != null)
67+
_sortedColumn.SortDirection = null;
68+
value.SortDirection = IsSortedAscending ? DataGridSortDirection.Ascending : DataGridSortDirection.Descending;
69+
}
70+
_sortedColumn = value;
71+
}
72+
}
73+
74+
private bool IsSortedByName { get { return SortedColumn == nameColumn; } set { if (value) SortedColumn = nameColumn; } }
75+
private bool IsSortedByDate { get { return SortedColumn == dateColumn; } set { if (value) SortedColumn = dateColumn; } }
76+
private bool IsSortedByType { get { return SortedColumn == typeColumn; } set { if (value) SortedColumn = typeColumn; } }
77+
private bool IsSortedBySize { get { return SortedColumn == sizeColumn; } set { if (value) SortedColumn = sizeColumn; } }
78+
79+
private bool IsSortedAscending
80+
{
81+
get
82+
{
83+
return viewModelInstance.DirectorySortDirection == SortDirection.Ascending;
84+
}
85+
set
86+
{
87+
viewModelInstance.DirectorySortDirection = value ? SortDirection.Ascending : SortDirection.Descending;
88+
_sortedColumn.SortDirection = value ? DataGridSortDirection.Ascending : DataGridSortDirection.Descending;
89+
}
90+
}
91+
92+
private bool IsSortedDescending
93+
{
94+
get
95+
{
96+
return !IsSortedAscending;
97+
}
98+
set
99+
{
100+
IsSortedAscending = !value;
101+
}
102+
}
103+
44104
public GenericFileBrowser()
45105
{
46106
this.InitializeComponent();
@@ -49,9 +109,6 @@ public GenericFileBrowser()
49109
progressBar = progBar;
50110
progressBar.Visibility = Visibility.Collapsed;
51111
data = AllView;
52-
sortedColumn = nameColumn;
53-
sortedColumn.SortDirection = DataGridSortDirection.Ascending;
54-
isSortedAscending = true;
55112
context = RightClickContextMenu;
56113
HeaderContextMenu = HeaderRightClickMenu;
57114
grid = RootGrid;
@@ -84,6 +141,22 @@ public GenericFileBrowser()
84141
NewTextDocument.Click += tabInstance.instanceInteraction.NewTextDocument_Click;
85142
PropertiesItem.Click += tabInstance.ShowPropertiesButton_Click;
86143
OpenInNewWindowItem.Click += tabInstance.instanceInteraction.OpenInNewWindowItem_Click;
144+
145+
switch (viewModelInstance.DirectorySortOption)
146+
{
147+
case SortOption.Name:
148+
SortedColumn = nameColumn;
149+
break;
150+
case SortOption.DateModified:
151+
SortedColumn = dateColumn;
152+
break;
153+
case SortOption.FileType:
154+
SortedColumn = typeColumn;
155+
break;
156+
case SortOption.Size:
157+
SortedColumn = nameColumn;
158+
break;
159+
}
87160
}
88161

89162
private void AddItem_Click(object sender, RoutedEventArgs e)
@@ -339,64 +412,16 @@ private void AllView_Sorting(object sender, DataGridColumnEventArgs e)
339412
{
340413
if (e.Column == iconColumn)
341414
return;
342-
if (sortedColumn == e.Column)
343-
isSortedAscending = e.Column.SortDirection == DataGridSortDirection.Descending;
415+
if (SortedColumn == e.Column)
416+
IsSortedAscending = !IsSortedAscending;
344417
else
345418
{
346-
isSortedAscending = true;
419+
IsSortedAscending = true;
420+
SortedColumn = e.Column;
347421
}
348422

349423
NotifyPropertyChanged("IsSortedAscending");
350424
NotifyPropertyChanged("IsSortedDescending");
351-
352-
SortBy(e.Column);
353-
}
354-
355-
private void SortBy(DataGridColumn column)
356-
{
357-
var selectedItems = data.SelectedItems;
358-
359-
viewModelInstance.OrderFiles(column.Tag.ToString(), isSortedAscending);
360-
361-
// Remove arrow on previous sorted column
362-
sortedColumn.SortDirection = null;
363-
column.SortDirection = isSortedAscending ? DataGridSortDirection.Ascending : DataGridSortDirection.Descending;
364-
sortedColumn = column;
365-
366-
if (selectedItems.Count == 1)
367-
{
368-
data.SelectedItem = selectedItems[0];
369-
}
370-
}
371-
372-
private bool IsSortedByName { get { return sortedColumn == nameColumn; } set { if (value) SortBy(nameColumn); } }
373-
private bool IsSortedByDate { get { return sortedColumn == dateColumn; } set { if (value) SortBy(dateColumn); } }
374-
private bool IsSortedByType { get { return sortedColumn == typeColumn; } set { if (value) SortBy(typeColumn); } }
375-
private bool IsSortedBySize { get { return sortedColumn == sizeColumn; } set { if (value) SortBy(sizeColumn); } }
376-
private bool IsSortedAscending
377-
{
378-
get
379-
{
380-
return isSortedAscending;
381-
}
382-
set
383-
{
384-
isSortedAscending = value;
385-
if (value) SortBy(sortedColumn);
386-
}
387-
}
388-
389-
private bool IsSortedDescending
390-
{
391-
get
392-
{
393-
return !isSortedAscending;
394-
}
395-
set
396-
{
397-
isSortedAscending = !value;
398-
if (value) SortBy(sortedColumn);
399-
}
400425
}
401426

402427
private void NotifyPropertyChanged(string info)

Files UWP/Interacts/Interaction.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ public async void List_ItemClick(object sender, DoubleTappedRoutedEventArgs e)
4343
{
4444
if (App.selectedTabInstance.accessibleContentFrame.SourcePageType == typeof(GenericFileBrowser))
4545
{
46-
var index = (tabInstance.accessibleContentFrame.Content as GenericFileBrowser).data.SelectedIndex;
47-
if (index > -1)
46+
var clickedOnItem = (tabInstance.accessibleContentFrame.Content as GenericFileBrowser).data.SelectedItem as ListedItem;
47+
if (clickedOnItem != null)
4848
{
49-
var clickedOnItem = tabInstance.instanceViewModel.FilesAndFolders[index];
5049
// Access MRU List
5150
var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList;
5251

@@ -141,11 +140,10 @@ public async void List_ItemClick(object sender, DoubleTappedRoutedEventArgs e)
141140
}
142141
else if (App.selectedTabInstance.accessibleContentFrame.SourcePageType == typeof(PhotoAlbum))
143142
{
144-
var index = (tabInstance.accessibleContentFrame.Content as PhotoAlbum).gv.SelectedIndex;
143+
var clickedOnItem = (tabInstance.accessibleContentFrame.Content as PhotoAlbum).gv.SelectedItem as ListedItem;
145144
var CurrentInstance = App.selectedTabInstance;
146-
if (index > -1)
145+
if (clickedOnItem != null)
147146
{
148-
var clickedOnItem = tabInstance.instanceViewModel.FilesAndFolders[index];
149147
// Access MRU List
150148
var mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList;
151149

0 commit comments

Comments
 (0)