Skip to content

Commit 6b7330a

Browse files
authored
Remember the sort option and direction across multiple sessions (#1439)
2 parents 0a3e614 + 513bf26 commit 6b7330a

File tree

5 files changed

+75
-75
lines changed

5 files changed

+75
-75
lines changed

Files/Enums/SortOption.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Files.Enums
22
{
3-
public enum SortOption
3+
public enum SortOption : byte
44
{
55
Name,
66
DateModified,

Files/Interacts/Interaction.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,10 @@ private async void OpenSelectedItems(bool displayApplicationPicker)
380380
//We can have many sort entries
381381
SortEntry sortEntry = new SortEntry()
382382
{
383-
AscendingOrder = CurrentInstance.FilesystemViewModel.DirectorySortDirection == Microsoft.Toolkit.Uwp.UI.SortDirection.Ascending,
383+
AscendingOrder = AppSettings.DirectorySortDirection == Microsoft.Toolkit.Uwp.UI.SortDirection.Ascending,
384384
};
385385

386-
var sortOption = CurrentInstance.FilesystemViewModel.DirectorySortOption;
386+
var sortOption = AppSettings.DirectorySortOption;
387387

388388
switch (sortOption)
389389
{

Files/View Models/ItemViewModel.cs

+30-62
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public class ItemViewModel : INotifyPropertyChanged, IDisposable
5252

5353
private string _jumpString = "";
5454
private readonly DispatcherTimer jumpTimer = new DispatcherTimer();
55-
private SortOption _directorySortOption = SortOption.Name;
56-
private SortDirection _directorySortDirection = SortDirection.Ascending;
5755

5856
private string _customPath;
5957

@@ -161,111 +159,82 @@ public bool IsFolderEmptyTextDisplayed
161159
}
162160
}
163161

164-
public SortOption DirectorySortOption
162+
public void UpdateSortOptionStatus()
165163
{
166-
get
167-
{
168-
return _directorySortOption;
169-
}
170-
set
171-
{
172-
if (value != _directorySortOption)
173-
{
174-
_directorySortOption = value;
175-
NotifyPropertyChanged("DirectorySortOption");
176-
NotifyPropertyChanged("IsSortedByName");
177-
NotifyPropertyChanged("IsSortedByDate");
178-
NotifyPropertyChanged("IsSortedByType");
179-
NotifyPropertyChanged("IsSortedBySize");
180-
OrderFiles();
181-
}
182-
}
164+
NotifyPropertyChanged("IsSortedByName");
165+
NotifyPropertyChanged("IsSortedByDate");
166+
NotifyPropertyChanged("IsSortedByType");
167+
NotifyPropertyChanged("IsSortedBySize");
168+
OrderFiles();
183169
}
184170

185-
public SortDirection DirectorySortDirection
171+
public void UpdateSortDirectionStatus()
186172
{
187-
get
188-
{
189-
return _directorySortDirection;
190-
}
191-
set
192-
{
193-
if (value != _directorySortDirection)
194-
{
195-
_directorySortDirection = value;
196-
NotifyPropertyChanged("DirectorySortDirection");
197-
NotifyPropertyChanged("IsSortedAscending");
198-
NotifyPropertyChanged("IsSortedDescending");
199-
OrderFiles();
200-
}
201-
}
173+
NotifyPropertyChanged("IsSortedAscending");
174+
NotifyPropertyChanged("IsSortedDescending");
175+
OrderFiles();
202176
}
203177

204178
public bool IsSortedByName
205179
{
206-
get => DirectorySortOption == SortOption.Name;
180+
get => AppSettings.DirectorySortOption == SortOption.Name;
207181
set
208182
{
209183
if (value)
210184
{
211-
DirectorySortOption = SortOption.Name;
185+
AppSettings.DirectorySortOption = SortOption.Name;
212186
NotifyPropertyChanged("IsSortedByName");
213-
NotifyPropertyChanged("DirectorySortOption");
214187
}
215188
}
216189
}
217190

218191
public bool IsSortedByDate
219192
{
220-
get => DirectorySortOption == SortOption.DateModified;
193+
get => AppSettings.DirectorySortOption == SortOption.DateModified;
221194
set
222195
{
223196
if (value)
224197
{
225-
DirectorySortOption = SortOption.DateModified;
198+
AppSettings.DirectorySortOption = SortOption.DateModified;
226199
NotifyPropertyChanged("IsSortedByDate");
227-
NotifyPropertyChanged("DirectorySortOption");
228200
}
229201
}
230202
}
231203

232204
public bool IsSortedByType
233205
{
234-
get => DirectorySortOption == SortOption.FileType;
206+
get => AppSettings.DirectorySortOption == SortOption.FileType;
235207
set
236208
{
237209
if (value)
238210
{
239-
DirectorySortOption = SortOption.FileType;
211+
AppSettings.DirectorySortOption = SortOption.FileType;
240212
NotifyPropertyChanged("IsSortedByType");
241-
NotifyPropertyChanged("DirectorySortOption");
242213
}
243214
}
244215
}
245216

246217
public bool IsSortedBySize
247218
{
248-
get => DirectorySortOption == SortOption.Size;
219+
get => AppSettings.DirectorySortOption == SortOption.Size;
249220
set
250221
{
251222
if (value)
252223
{
253-
DirectorySortOption = SortOption.Size;
224+
AppSettings.DirectorySortOption = SortOption.Size;
254225
NotifyPropertyChanged("IsSortedBySize");
255-
NotifyPropertyChanged("DirectorySortOption");
256226
}
257227
}
258228
}
259229

260230
public bool IsSortedAscending
261231
{
262-
get => DirectorySortDirection == SortDirection.Ascending;
232+
get => AppSettings.DirectorySortDirection == SortDirection.Ascending;
263233
set
264234
{
265-
DirectorySortDirection = value ? SortDirection.Ascending : SortDirection.Descending;
235+
AppSettings.DirectorySortDirection = value ? SortDirection.Ascending : SortDirection.Descending;
266236
NotifyPropertyChanged("IsSortedAscending");
267237
NotifyPropertyChanged("IsSortedDescending");
268-
NotifyPropertyChanged("DirectorySortDirection");
269238
}
270239
}
271240

@@ -274,10 +243,9 @@ public bool IsSortedDescending
274243
get => !IsSortedAscending;
275244
set
276245
{
277-
DirectorySortDirection = value ? SortDirection.Descending : SortDirection.Ascending;
246+
AppSettings.DirectorySortDirection = value ? SortDirection.Descending : SortDirection.Ascending;
278247
NotifyPropertyChanged("IsSortedAscending");
279248
NotifyPropertyChanged("IsSortedDescending");
280-
NotifyPropertyChanged("DirectorySortDirection");
281249
}
282250
}
283251

@@ -393,7 +361,7 @@ public void OrderFiles()
393361
static object orderByNameFunc(ListedItem item) => item.ItemName;
394362
Func<ListedItem, object> orderFunc = orderByNameFunc;
395363
NaturalStringComparer naturalStringComparer = new NaturalStringComparer();
396-
switch (DirectorySortOption)
364+
switch (AppSettings.DirectorySortOption)
397365
{
398366
case SortOption.Name:
399367
orderFunc = orderByNameFunc;
@@ -418,35 +386,35 @@ public void OrderFiles()
418386
IOrderedEnumerable<ListedItem> ordered;
419387
List<ListedItem> orderedList;
420388

421-
if (DirectorySortDirection == SortDirection.Ascending)
389+
if (AppSettings.DirectorySortDirection == SortDirection.Ascending)
422390
{
423-
if (DirectorySortOption == SortOption.Name)
391+
if (AppSettings.DirectorySortOption == SortOption.Name)
424392
ordered = _filesAndFolders.OrderBy(folderThenFileAsync).ThenBy(orderFunc, naturalStringComparer);
425393
else
426394
ordered = _filesAndFolders.OrderBy(folderThenFileAsync).ThenBy(orderFunc);
427395
}
428396
else
429397
{
430-
if (DirectorySortOption == SortOption.FileType)
398+
if (AppSettings.DirectorySortOption == SortOption.FileType)
431399
{
432-
if (DirectorySortOption == SortOption.Name)
400+
if (AppSettings.DirectorySortOption == SortOption.Name)
433401
ordered = _filesAndFolders.OrderBy(folderThenFileAsync).ThenByDescending(orderFunc, naturalStringComparer);
434402
else
435403
ordered = _filesAndFolders.OrderBy(folderThenFileAsync).ThenByDescending(orderFunc);
436404
}
437405
else
438406
{
439-
if (DirectorySortOption == SortOption.Name)
407+
if (AppSettings.DirectorySortOption == SortOption.Name)
440408
ordered = _filesAndFolders.OrderByDescending(folderThenFileAsync).ThenByDescending(orderFunc, naturalStringComparer);
441409
else
442410
ordered = _filesAndFolders.OrderByDescending(folderThenFileAsync).ThenByDescending(orderFunc);
443411
}
444412
}
445413

446414
// Further order by name if applicable
447-
if (DirectorySortOption != SortOption.Name)
415+
if (AppSettings.DirectorySortOption != SortOption.Name)
448416
{
449-
if (DirectorySortDirection == SortDirection.Ascending)
417+
if (AppSettings.DirectorySortDirection == SortDirection.Ascending)
450418
ordered = ordered.ThenBy(orderByNameFunc, naturalStringComparer);
451419
else
452420
ordered = ordered.ThenByDescending(orderByNameFunc, naturalStringComparer);
@@ -1480,4 +1448,4 @@ public void Dispose()
14801448
CloseWatcher();
14811449
}
14821450
}
1483-
}
1451+
}

Files/View Models/SettingsViewModel.cs

+34-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using GalaSoft.MvvmLight;
99
using GalaSoft.MvvmLight.Command;
1010
using Microsoft.AppCenter.Analytics;
11+
using Microsoft.Toolkit.Uwp.UI;
1112
using Microsoft.Toolkit.Uwp.UI.Controls.TextToolbarSymbols;
1213
using System;
1314
using System.Collections.Generic;
@@ -43,7 +44,6 @@ public SettingsViewModel()
4344
DetectRecycleBinPreference();
4445
DetectQuickLook();
4546
DetectGridViewSize();
46-
4747
DrivesManager = new DrivesManager();
4848

4949
//DetectWSLDistros();
@@ -59,8 +59,8 @@ public SettingsViewModel()
5959
Analytics.TrackEvent("ShowConfirmDeleteDialog " + ShowConfirmDeleteDialog.ToString());
6060
Analytics.TrackEvent("AcrylicSidebar " + AcrylicEnabled.ToString());
6161
Analytics.TrackEvent("ShowFileOwner " + ShowFileOwner.ToString());
62-
6362
// Load the supported languages
63+
6464
var supportedLang = ApplicationLanguages.ManifestLanguages;
6565
DefaultLanguages = new ObservableCollection<DefaultLanguageModel> { new DefaultLanguageModel(null) };
6666
foreach (var lang in supportedLang)
@@ -86,6 +86,38 @@ public DefaultLanguageModel DefaultLanguage
8686
}
8787
}
8888

89+
public SortOption DirectorySortOption
90+
{
91+
get => (SortOption)SortOptionByte;
92+
set
93+
{
94+
SortOptionByte = (byte)value;
95+
App.CurrentInstance?.FilesystemViewModel?.UpdateSortOptionStatus();
96+
}
97+
}
98+
99+
public SortDirection DirectorySortDirection
100+
{
101+
get => (SortDirection)SortDirectionByte;
102+
set
103+
{
104+
SortDirectionByte = (byte)value;
105+
App.CurrentInstance?.FilesystemViewModel?.UpdateSortDirectionStatus();
106+
}
107+
}
108+
109+
private byte SortOptionByte
110+
{
111+
get => Get((byte)0);
112+
set => Set(value);
113+
}
114+
115+
private byte SortDirectionByte
116+
{
117+
get => Get((byte)0);
118+
set => Set(value);
119+
}
120+
89121
public async void DetectQuickLook()
90122
{
91123
// Detect QuickLook

Files/Views/LayoutModes/GenericFileBrowser.xaml.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ public DataGridColumn SortedColumn
3434
set
3535
{
3636
if (value == nameColumn)
37-
App.CurrentInstance.FilesystemViewModel.DirectorySortOption = SortOption.Name;
37+
AppSettings.DirectorySortOption = SortOption.Name;
3838
else if (value == dateColumn)
39-
App.CurrentInstance.FilesystemViewModel.DirectorySortOption = SortOption.DateModified;
39+
AppSettings.DirectorySortOption = SortOption.DateModified;
4040
else if (value == typeColumn)
41-
App.CurrentInstance.FilesystemViewModel.DirectorySortOption = SortOption.FileType;
41+
AppSettings.DirectorySortOption = SortOption.FileType;
4242
else if (value == sizeColumn)
43-
App.CurrentInstance.FilesystemViewModel.DirectorySortOption = SortOption.Size;
43+
AppSettings.DirectorySortOption = SortOption.Size;
4444
else
45-
App.CurrentInstance.FilesystemViewModel.DirectorySortOption = SortOption.Name;
45+
AppSettings.DirectorySortOption = SortOption.Name;
4646

4747
if (value != _sortedColumn)
4848
{
4949
// Remove arrow on previous sorted column
5050
if (_sortedColumn != null)
5151
_sortedColumn.SortDirection = null;
5252
}
53-
value.SortDirection = App.CurrentInstance.FilesystemViewModel.DirectorySortDirection == SortDirection.Ascending ? DataGridSortDirection.Ascending : DataGridSortDirection.Descending;
53+
value.SortDirection = AppSettings.DirectorySortDirection == SortDirection.Ascending ? DataGridSortDirection.Ascending : DataGridSortDirection.Descending;
5454
_sortedColumn = value;
5555
}
5656
}
@@ -59,7 +59,7 @@ public GenericFileBrowser()
5959
{
6060
InitializeComponent();
6161
base.BaseLayoutItemContextFlyout = this.BaseLayoutItemContextFlyout;
62-
switch (App.CurrentInstance.FilesystemViewModel.DirectorySortOption)
62+
switch (AppSettings.DirectorySortOption)
6363
{
6464
case SortOption.Name:
6565
SortedColumn = nameColumn;
@@ -192,7 +192,7 @@ private async void ViewModel_PropertyChanged(object sender, PropertyChangedEvent
192192
{
193193
if (e.PropertyName == "DirectorySortOption")
194194
{
195-
switch (App.CurrentInstance.FilesystemViewModel.DirectorySortOption)
195+
switch (AppSettings.DirectorySortOption)
196196
{
197197
case SortOption.Name:
198198
SortedColumn = nameColumn;

0 commit comments

Comments
 (0)