Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added support for changing groups direction #11154

Merged
merged 7 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Files.App/BaseLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
using Windows.System;
using static Files.App.Helpers.PathNormalization;
using DispatcherQueueTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
using SortDirection = Files.Shared.Enums.SortDirection;

namespace Files.App
{
Expand Down Expand Up @@ -377,6 +378,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
IsItemSelected = false;
FolderSettings!.LayoutModeChangeRequested += BaseFolderSettings_LayoutModeChangeRequested;
FolderSettings.GroupOptionPreferenceUpdated += FolderSettings_GroupOptionPreferenceUpdated;
FolderSettings.GroupDirectionPreferenceUpdated += FolderSettings_GroupDirectionPreferenceUpdated;
ParentShellPageInstance.FilesystemViewModel.EmptyTextType = EmptyTextType.None;
ParentShellPageInstance.ToolbarViewModel.UpdateSortAndGroupOptions();
ParentShellPageInstance.ToolbarViewModel.CanRefresh = true;
Expand Down Expand Up @@ -474,7 +476,13 @@ public void SetSelectedItemsOnNavigation()

private CancellationTokenSource? groupingCancellationToken;

private async void FolderSettings_GroupOptionPreferenceUpdated(object? sender, GroupOption e)
private void FolderSettings_GroupOptionPreferenceUpdated(object? sender, GroupOption e)
=> GroupPreferenceUpdated();

private void FolderSettings_GroupDirectionPreferenceUpdated(object? sender, SortDirection e)
=> GroupPreferenceUpdated();

private async void GroupPreferenceUpdated()
{
// Two or more of these running at the same time will cause a crash, so cancel the previous one before beginning
groupingCancellationToken?.Cancel();
Expand All @@ -492,6 +500,7 @@ protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
CharacterReceived -= Page_CharacterReceived;
FolderSettings!.LayoutModeChangeRequested -= BaseFolderSettings_LayoutModeChangeRequested;
FolderSettings.GroupOptionPreferenceUpdated -= FolderSettings_GroupOptionPreferenceUpdated;
FolderSettings.GroupDirectionPreferenceUpdated -= FolderSettings_GroupDirectionPreferenceUpdated;
ItemContextMenuFlyout.Opening -= ItemContextFlyout_Opening;
BaseContextMenuFlyout.Opening -= BaseContextFlyout_Opening;

Expand Down
34 changes: 34 additions & 0 deletions src/Files.App/Helpers/ContextFlyoutItemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,40 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseLayoutMenuItems(Curren
ItemType = ItemType.Toggle,
ShowItem = currentInstanceViewModel.IsPageTypeLibrary,
},
new ContextMenuFlyoutItemViewModel()
{
ItemType = ItemType.Separator,
ShowInRecycleBin = true,
ShowInSearchPage = true,
ShowInFtpPage = true,
ShowInZipPage = true,
},
new ContextMenuFlyoutItemViewModel()
{
Text = "Ascending".GetLocalizedResource(),
IsChecked = currentInstanceViewModel.FolderSettings.DirectoryGroupDirection == SortDirection.Ascending,
IsEnabled = currentInstanceViewModel.FolderSettings.DirectoryGroupOption != GroupOption.None,
ShowInRecycleBin = true,
ShowInSearchPage = true,
ShowInFtpPage = true,
ShowInZipPage = true,
Command = currentInstanceViewModel.FolderSettings.ChangeGroupDirectionCommand,
CommandParameter = SortDirection.Ascending,
ItemType = ItemType.Toggle,
},
new ContextMenuFlyoutItemViewModel()
{
Text = "Descending".GetLocalizedResource(),
IsChecked = currentInstanceViewModel.FolderSettings.DirectoryGroupDirection == SortDirection.Descending,
IsEnabled = currentInstanceViewModel.FolderSettings.DirectoryGroupOption != GroupOption.None,
ShowInRecycleBin = true,
ShowInSearchPage = true,
ShowInFtpPage = true,
ShowInZipPage = true,
Command = currentInstanceViewModel.FolderSettings.ChangeGroupDirectionCommand,
CommandParameter = SortDirection.Descending,
ItemType = ItemType.Toggle,
},
}
},
new ContextMenuFlyoutItemViewModel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ public static (string key, string text, string range, int index) GetGroupSizeInf
if (size > sizeGp.size)
{
var rangeStr = i > 0 ? $"{sizeGp.sizeText} - {sizeGroups[i - 1].sizeText}" : $"{sizeGp.sizeText} +";
return (sizeGp.size.ToString(), sizeGp.text, rangeStr, i + 1); //i +1 is so that other groups always show below "unspecified"
return (sizeGp.size.ToString(), sizeGp.text, rangeStr, sizeGroups.Length - i);
}
lastSizeStr = sizeGp.sizeText;
}

return ("0", "ItemSizeText_Tiny".GetLocalizedResource(), $"{"0 B".ConvertSizeAbbreviation()} - {lastSizeStr}", sizeGroups.Length + 1);
return ("0", "ItemSizeText_Tiny".GetLocalizedResource(), $"{"0 B".ConvertSizeAbbreviation()} - {lastSizeStr}", 0);
}

public static string GetGroupSizeKey(long size)
Expand Down Expand Up @@ -175,4 +175,4 @@ public interface IGroupableItem
{
public string Key { get; set; }
}
}
}
4 changes: 4 additions & 0 deletions src/Files.App/Helpers/LayoutPreferences/LayoutPreferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class LayoutPreferences
public SortDirection DirectorySortDirection;
public bool SortDirectoriesAlongsideFiles;
public GroupOption DirectoryGroupOption;
public SortDirection DirectoryGroupDirection;
public FolderLayoutModes LayoutMode;
public int GridViewSize;
public bool IsAdaptiveLayoutOverridden;
Expand All @@ -31,6 +32,7 @@ public LayoutPreferences()
DirectorySortOption = UserSettingsService.FoldersSettingsService.DefaultSortOption;
DirectoryGroupOption = UserSettingsService.FoldersSettingsService.DefaultGroupOption;
DirectorySortDirection = UserSettingsService.LayoutSettingsService.DefaultDirectorySortDirection;
DirectoryGroupDirection = UserSettingsService.LayoutSettingsService.DefaultDirectoryGroupDirection;
SortDirectoriesAlongsideFiles = UserSettingsService.LayoutSettingsService.DefaultSortDirectoriesAlongsideFiles;
IsAdaptiveLayoutOverridden = defaultLayout is not FolderLayoutModes.Adaptive;

Expand Down Expand Up @@ -71,6 +73,7 @@ public override bool Equals(object? obj)
prefs.DirectoryGroupOption == DirectoryGroupOption &&
prefs.DirectorySortOption == DirectorySortOption &&
prefs.DirectorySortDirection == DirectorySortDirection &&
prefs.DirectoryGroupDirection == DirectoryGroupDirection &&
prefs.SortDirectoriesAlongsideFiles == SortDirectoriesAlongsideFiles &&
prefs.IsAdaptiveLayoutOverridden == IsAdaptiveLayoutOverridden &&
prefs.ColumnsViewModel.Equals(ColumnsViewModel));
Expand All @@ -85,6 +88,7 @@ public override int GetHashCode()
hashCode = (hashCode * 397) ^ DirectoryGroupOption.GetHashCode();
hashCode = (hashCode * 397) ^ DirectorySortOption.GetHashCode();
hashCode = (hashCode * 397) ^ DirectorySortDirection.GetHashCode();
hashCode = (hashCode * 397) ^ DirectoryGroupDirection.GetHashCode();
hashCode = (hashCode * 397) ^ SortDirectoriesAlongsideFiles.GetHashCode();
hashCode = (hashCode * 397) ^ IsAdaptiveLayoutOverridden.GetHashCode();
hashCode = (hashCode * 397) ^ ColumnsViewModel.GetHashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public ITimeSpanLabel ToTimeSpanLabel(DateTimeOffset offset)

return 0 switch
{
_ when now.Date == time.Date => new Label("Today", "\ue184", 0),
_ when y.Date == time.Date => new Label("ItemTimeText_Yesterday", "\ue161", 1),
_ when diff.Days < 7 && w.Year == time.Year && GetWeekOfYear(w) == GetWeekOfYear(time) => new Label("ItemTimeText_ThisWeek", "\uE162", 2),
_ when diff.Days < 14 && w.Year == time.Year && GetWeekOfYear(w) == GetWeekOfYear(time) => new Label("ItemTimeText_LastWeek", "\uE162", 3),
_ when now.Year == time.Year && now.Month == time.Month => new Label("ItemTimeText_ThisMonth", "\ue163", 4),
_ when now.AddMonths(-1).Year == time.Year && now.AddMonths(-1).Month == time.Month => new Label("ItemTimeText_LastMonth", "\ue163", 5),
_ when now.Year == time.Year => new Label("ItemTimeText_ThisYear", "\ue163", 5),
_ => new Label("ItemTimeText_Older", "\uEC92", 6),
_ when now.Date == time.Date => new Label("Today", "\ue184", 7),
_ when y.Date == time.Date => new Label("ItemTimeText_Yesterday", "\ue161", 6),
_ when diff.Days < 7 && w.Year == time.Year && GetWeekOfYear(w) == GetWeekOfYear(time) => new Label("ItemTimeText_ThisWeek", "\uE162", 5),
_ when diff.Days < 14 && w.Year == time.Year && GetWeekOfYear(w) == GetWeekOfYear(time) => new Label("ItemTimeText_LastWeek", "\uE162", 4),
_ when now.Year == time.Year && now.Month == time.Month => new Label("ItemTimeText_ThisMonth", "\ue163", 3),
_ when now.AddMonths(-1).Year == time.Year && now.AddMonths(-1).Month == time.Month => new Label("ItemTimeText_LastMonth", "\ue163", 2),
_ when now.Year == time.Year => new Label("ItemTimeText_ThisYear", "\ue163", 1),
_ => new Label("ItemTimeText_Older", "\uEC92", 0),
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public SortDirection DefaultDirectorySortDirection
set => Set((long)value);
}

public SortDirection DefaultDirectoryGroupDirection
{
get => (SortDirection)Get((long)SortDirection.Ascending);
set => Set((long)value);
}

public bool DefaultSortDirectoriesAlongsideFiles
{
get => Get(false);
Expand Down
15 changes: 12 additions & 3 deletions src/Files.App/UserControls/InnerNavigationToolbar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@
IsChecked="{x:Bind ViewModel.IsSortedByDateDeleted, Mode=TwoWay}"
IsEnabled="{x:Bind ViewModel.InstanceViewModel.IsPageTypeRecycleBin, Mode=OneWay}"
Text="{helpers:ResourceString Name=DateDeleted}" />
<MenuFlyoutSeparator />
<ToggleMenuFlyoutItem IsChecked="{x:Bind ViewModel.IsSortedAscending, Mode=TwoWay}" Text="{helpers:ResourceString Name=Ascending}" />
<ToggleMenuFlyoutItem IsChecked="{x:Bind ViewModel.IsSortedDescending, Mode=TwoWay}" Text="{helpers:ResourceString Name=Descending}" />
</MenuFlyoutSubItem>
<MenuFlyoutSubItem Text="{helpers:ResourceString Name=NavToolbarGroupByRadioButtons/Text}">
<ToggleMenuFlyoutItem IsChecked="{x:Bind ViewModel.IsGroupedByNone, Mode=TwoWay}" Text="{helpers:ResourceString Name=None}" />
Expand All @@ -518,11 +521,17 @@
IsChecked="{x:Bind ViewModel.IsGroupedByFolderPath, Mode=TwoWay}"
IsEnabled="{x:Bind ViewModel.InstanceViewModel.IsPageTypeLibrary, Mode=OneWay}"
Text="{helpers:ResourceString Name=NavToolbarArrangementOptionFolderPath/Text}" />
<MenuFlyoutSeparator />
<ToggleMenuFlyoutItem
IsChecked="{x:Bind ViewModel.IsGroupedAscending, Mode=TwoWay}"
IsEnabled="{x:Bind ViewModel.IsGroupedByNone, Mode=OneWay, Converter={StaticResource BoolNegationConverter}}"
Text="{helpers:ResourceString Name=Ascending}" />
<ToggleMenuFlyoutItem
IsChecked="{x:Bind ViewModel.IsGroupedDescending, Mode=TwoWay}"
IsEnabled="{x:Bind ViewModel.IsGroupedByNone, Mode=OneWay, Converter={StaticResource BoolNegationConverter}}"
Text="{helpers:ResourceString Name=Descending}" />
</MenuFlyoutSubItem>
<MenuFlyoutSeparator />
<ToggleMenuFlyoutItem IsChecked="{x:Bind ViewModel.IsSortedAscending, Mode=TwoWay}" Text="{helpers:ResourceString Name=Ascending}" />
<ToggleMenuFlyoutItem IsChecked="{x:Bind ViewModel.IsSortedDescending, Mode=TwoWay}" Text="{helpers:ResourceString Name=Descending}" />
<MenuFlyoutSeparator />
<ToggleMenuFlyoutItem IsChecked="{x:Bind ViewModel.AreDirectoriesSortedAlongsideFiles, Mode=TwoWay}" Text="{helpers:ResourceString Name=SettingsListAndSortDirectoriesAlongsideFiles}" />
</MenuFlyout>
</AppBarButton.Flyout>
Expand Down
27 changes: 26 additions & 1 deletion src/Files.App/ViewModels/FolderSettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public FolderSettingsViewModel()
ToggleLayoutModeAdaptiveCommand = new RelayCommand(ToggleLayoutModeAdaptive);

ChangeGroupOptionCommand = new RelayCommand<GroupOption>(ChangeGroupOption);
ChangeGroupDirectionCommand = new RelayCommand<SortDirection>(ChangeGroupDirection);
}
public FolderSettingsViewModel(FolderLayoutModes modeOverride) : this()
=> (rootLayoutMode, LayoutPreference.IsAdaptiveLayoutOverridden) = (modeOverride, true);
Expand Down Expand Up @@ -215,6 +216,8 @@ public int GridViewSize

public event EventHandler<SortDirection>? SortDirectionPreferenceUpdated;

public event EventHandler<SortDirection>? GroupDirectionPreferenceUpdated;

public event EventHandler<bool>? SortDirectoriesAlongsideFilesPreferenceUpdated;

public SortOption DirectorySortOption
Expand Down Expand Up @@ -258,6 +261,21 @@ public SortDirection DirectorySortDirection
}
}

public ICommand ChangeGroupDirectionCommand { get; }

public SortDirection DirectoryGroupDirection
{
get => LayoutPreference.DirectoryGroupDirection;
set
{
if (SetProperty(ref LayoutPreference.DirectoryGroupDirection, value, nameof(DirectoryGroupDirection)))
{
LayoutPreferencesUpdateRequired?.Invoke(this, new LayoutPreferenceEventArgs(LayoutPreference));
GroupDirectionPreferenceUpdated?.Invoke(this, DirectoryGroupDirection);
}
}
}

public bool SortDirectoriesAlongsideFiles
{
get
Expand Down Expand Up @@ -327,6 +345,7 @@ public static void SetLayoutPreferencesForPath(string folderPath, LayoutPreferen
userSettingsService.FoldersSettingsService.DefaultGroupOption = prefs.DirectoryGroupOption;
}
userSettingsService.LayoutSettingsService.DefaultDirectorySortDirection = prefs.DirectorySortDirection;
userSettingsService.LayoutSettingsService.DefaultDirectoryGroupDirection = prefs.DirectoryGroupDirection;
userSettingsService.LayoutSettingsService.DefaultSortDirectoriesAlongsideFiles = prefs.SortDirectoriesAlongsideFiles;

userSettingsService.FoldersSettingsService.ShowDateColumn = !prefs.ColumnsViewModel.DateModifiedColumn.UserCollapsed;
Expand Down Expand Up @@ -376,7 +395,10 @@ private static LayoutPreferences GetDefaultLayoutPreferences(string folderPath)

if (folderPath == CommonPaths.DownloadsPath)
// Default for downloads folder is to group by date created
return new LayoutPreferences() { DirectoryGroupOption = GroupOption.DateCreated };
return new LayoutPreferences() {
DirectoryGroupOption = GroupOption.DateCreated,
DirectoryGroupDirection = SortDirection.Descending
};
else if (LibraryManager.IsLibraryPath(folderPath))
// Default for libraries is to group by folder path
return new LayoutPreferences() { DirectoryGroupOption = GroupOption.FolderPath };
Expand Down Expand Up @@ -414,6 +436,7 @@ private set
OnPropertyChanged(nameof(DirectoryGroupOption));
OnPropertyChanged(nameof(DirectorySortOption));
OnPropertyChanged(nameof(DirectorySortDirection));
OnPropertyChanged(nameof(DirectoryGroupDirection));
OnPropertyChanged(nameof(SortDirectoriesAlongsideFiles));
OnPropertyChanged(nameof(ColumnsViewModel));
}
Expand Down Expand Up @@ -498,6 +521,8 @@ public void ToggleLayoutModeAdaptive()

private void ChangeGroupOption(GroupOption option) => DirectoryGroupOption = option;

private void ChangeGroupDirection(SortDirection option) => DirectoryGroupDirection = option;

public void OnDefaultPreferencesChanged(string folderPath, string settingsName)
{
var prefs = GetLayoutPreferencesForPath(folderPath);
Expand Down
17 changes: 16 additions & 1 deletion src/Files.App/ViewModels/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,22 @@ private void OrderGroups(CancellationToken token = default)

if (FilesAndFolders.GroupedCollection is null || FilesAndFolders.GroupedCollection.IsSorted)
return;
FilesAndFolders.GroupedCollection.Order(x => x.OrderBy(y => y.Model.SortIndexOverride).ThenBy(y => y.Model.Text));
if (folderSettings.DirectoryGroupDirection == SortDirection.Ascending)
{
if (folderSettings.DirectoryGroupOption == GroupOption.Size)
// Always show file sections below folders
FilesAndFolders.GroupedCollection.Order(x => x.OrderBy(y => y.First().PrimaryItemAttribute != StorageItemTypes.Folder).ThenBy(y => y.Model.SortIndexOverride).ThenBy(y => y.Model.Text));
else
FilesAndFolders.GroupedCollection.Order(x => x.OrderBy(y => y.Model.SortIndexOverride).ThenBy(y => y.Model.Text));
}
else
{
if (folderSettings.DirectoryGroupOption == GroupOption.Size)
// Always show file sections below folders
FilesAndFolders.GroupedCollection.Order(x => x.OrderBy(y => y.First().PrimaryItemAttribute != StorageItemTypes.Folder).ThenByDescending(y => y.Model.SortIndexOverride).ThenByDescending(y => y.Model.Text));
else
FilesAndFolders.GroupedCollection.Order(x => x.OrderByDescending(y => y.Model.SortIndexOverride).ThenByDescending(y => y.Model.Text));
}
FilesAndFolders.GroupedCollection.IsSorted = true;
}

Expand Down
Loading