Skip to content

Feature: Added support for grouping items by month #12288

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

Merged
merged 8 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions specs/RichCommand/CommandList.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ This is the list of all commands defined in `CommandCodes` enum except `None`.
| | GroupAscending | Ascending | Sort groups in ascending order | |
| | GroupDescending | Descending | Sort groups in descending order | |
| | ToggleGroupDirection | Toggle sort direction | Toggle group sort direction | |
| | GroupByYear | Year | Group items by year | |
| | GroupByMonth | Month | Group items by month | |
| | ToggleGroupByDateUnit | Toggle grouping unit | Toggle unit for grouping by date | |
| Navigation | NewTab | New tab | Open new tab | Ctrl+T |
| | NavigateBack | Back | Navigate backward in navigation history | Alt+Left, Backspace |
| | NavigateForward | Forward | Navigate forward in navigation history | Alt+Right |
Expand Down
93 changes: 87 additions & 6 deletions src/Files.App/Actions/Display/GroupAction.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.Shared.Enums;
using System.ComponentModel;
using System.Threading.Tasks;

namespace Files.App.Actions
{
Expand Down Expand Up @@ -251,4 +245,91 @@ public Task ExecuteAsync()
return Task.CompletedTask;
}
}

internal class GroupByYearAction : ObservableObject, IToggleAction
{
private readonly IDisplayPageContext context = Ioc.Default.GetRequiredService<IDisplayPageContext>();

public string Label { get; } = "Year".GetLocalizedResource();

public string Description => "GroupByYearDescription".GetLocalizedResource();

public bool IsOn => context.GroupByDateUnit is GroupByDateUnit.Year;
public bool IsExecutable => context.GroupOption.IsGroupByDate();

public GroupByYearAction()
{
context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync()
{
context.GroupByDateUnit = GroupByDateUnit.Year;
return Task.CompletedTask;
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IDisplayPageContext.GroupOption):
OnPropertyChanged(nameof(IsExecutable));
break;
case nameof(IDisplayPageContext.GroupByDateUnit):
OnPropertyChanged(nameof(IsOn));
break;
}
}
}

internal class GroupByMonthAction : ObservableObject, IToggleAction
{
private readonly IDisplayPageContext context = Ioc.Default.GetRequiredService<IDisplayPageContext>();

public string Label { get; } = "Month".GetLocalizedResource();

public string Description => "GroupByMonthDescription".GetLocalizedResource();

public bool IsOn => context.GroupByDateUnit is GroupByDateUnit.Month;
public bool IsExecutable => context.GroupOption.IsGroupByDate();

public GroupByMonthAction()
{
context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync()
{
context.GroupByDateUnit = GroupByDateUnit.Month;
return Task.CompletedTask;
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IDisplayPageContext.GroupOption):
OnPropertyChanged(nameof(IsExecutable));
break;
case nameof(IDisplayPageContext.GroupByDateUnit):
OnPropertyChanged(nameof(IsOn));
break;
}
}
}

internal class ToggleGroupByDateUnitAction : IAction
{
private readonly IDisplayPageContext context = Ioc.Default.GetRequiredService<IDisplayPageContext>();

public string Label { get; } = "ToggleGroupingUnit".GetLocalizedResource();

public string Description => "ToggleGroupByDateUnitDescription".GetLocalizedResource();

public Task ExecuteAsync()
{
context.GroupByDateUnit = context.GroupByDateUnit is GroupByDateUnit.Month ? GroupByDateUnit.Year : GroupByDateUnit.Month;
return Task.CompletedTask;
}
}
}
5 changes: 5 additions & 0 deletions src/Files.App/BaseLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
FolderSettings!.LayoutModeChangeRequested += BaseFolderSettings_LayoutModeChangeRequested;
FolderSettings.GroupOptionPreferenceUpdated += FolderSettings_GroupOptionPreferenceUpdated;
FolderSettings.GroupDirectionPreferenceUpdated += FolderSettings_GroupDirectionPreferenceUpdated;
FolderSettings.GroupByDateUnitPreferenceUpdated += FolderSettings_GroupByDateUnitPreferenceUpdated;

ParentShellPageInstance.FilesystemViewModel.EmptyTextType = EmptyTextType.None;
ParentShellPageInstance.ToolbarViewModel.CanRefresh = true;
Expand Down Expand Up @@ -509,6 +510,9 @@ private void FolderSettings_GroupOptionPreferenceUpdated(object? sender, GroupOp
private void FolderSettings_GroupDirectionPreferenceUpdated(object? sender, SortDirection e)
=> GroupPreferenceUpdated();

private void FolderSettings_GroupByDateUnitPreferenceUpdated(object? sender, GroupByDateUnit e)
=> GroupPreferenceUpdated();

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

Expand Down
3 changes: 3 additions & 0 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ public enum CommandCodes
GroupAscending,
GroupDescending,
ToggleGroupDirection,
GroupByYear,
GroupByMonth,
ToggleGroupByDateUnit,

// Navigation
NewTab,
Expand Down
16 changes: 6 additions & 10 deletions src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Actions;
using Files.Backend.Services.Settings;
using Microsoft.AppCenter.Analytics;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace Files.App.Commands
{
Expand Down Expand Up @@ -128,6 +118,9 @@ internal class CommandManager : ICommandManager
public IRichCommand GroupAscending => commands[CommandCodes.GroupAscending];
public IRichCommand GroupDescending => commands[CommandCodes.GroupDescending];
public IRichCommand ToggleGroupDirection => commands[CommandCodes.ToggleGroupDirection];
public IRichCommand GroupByYear => commands[CommandCodes.GroupByYear];
public IRichCommand GroupByMonth => commands[CommandCodes.GroupByMonth];
public IRichCommand ToggleGroupByDateUnit => commands[CommandCodes.ToggleGroupByDateUnit];
public IRichCommand NewTab => commands[CommandCodes.NewTab];
public IRichCommand FormatDrive => commands[CommandCodes.FormatDrive];
public IRichCommand NavigateBack => commands[CommandCodes.NavigateBack];
Expand Down Expand Up @@ -264,6 +257,9 @@ public CommandManager()
[CommandCodes.GroupAscending] = new GroupAscendingAction(),
[CommandCodes.GroupDescending] = new GroupDescendingAction(),
[CommandCodes.ToggleGroupDirection] = new ToggleGroupDirectionAction(),
[CommandCodes.GroupByYear] = new GroupByYearAction(),
[CommandCodes.GroupByMonth] = new GroupByMonthAction(),
[CommandCodes.ToggleGroupByDateUnit] = new ToggleGroupByDateUnitAction(),
[CommandCodes.NewTab] = new NewTabAction(),
[CommandCodes.FormatDrive] = new FormatDriveAction(),
[CommandCodes.NavigateBack] = new NavigateBackAction(),
Expand Down
5 changes: 3 additions & 2 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using System.Collections.Generic;

namespace Files.App.Commands
{
public interface ICommandManager : IEnumerable<IRichCommand>
Expand Down Expand Up @@ -124,6 +122,9 @@ public interface ICommandManager : IEnumerable<IRichCommand>
IRichCommand GroupAscending { get; }
IRichCommand GroupDescending { get; }
IRichCommand ToggleGroupDirection { get; }
IRichCommand GroupByYear { get; }
IRichCommand GroupByMonth { get; }
IRichCommand ToggleGroupByDateUnit { get; }

IRichCommand NewTab { get; }
IRichCommand NavigateBack { get; }
Expand Down
23 changes: 16 additions & 7 deletions src/Files.App/Contexts/DisplayPage/DisplayPageContext.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.ViewModels;
using Files.Backend.Services.Settings;
using Files.Shared.Enums;
using System;
using System.ComponentModel;
using static Files.App.Constants.Browser.GridViewBrowser;

namespace Files.App.Contexts
Expand Down Expand Up @@ -100,6 +93,17 @@ public SortDirection GroupDirection
}
}

private GroupByDateUnit groupByDateUnit = GroupByDateUnit.Year;
public GroupByDateUnit GroupByDateUnit
{
get => groupByDateUnit;
set
{
if (FolderSettings is FolderSettingsViewModel viewModel)
viewModel.DirectoryGroupByDateUnit = value;
}
}

private bool sortDirectoriesAlongsideFiles = false;
public bool SortDirectoriesAlongsideFiles
{
Expand Down Expand Up @@ -170,6 +174,9 @@ private void FolderSettings_PropertyChanged(object? sender, PropertyChangedEvent
case nameof(FolderSettingsViewModel.DirectoryGroupDirection):
SetProperty(ref groupDirection, viewModel.DirectoryGroupDirection, nameof(GroupDirection));
break;
case nameof(FolderSettingsViewModel.DirectoryGroupByDateUnit):
SetProperty(ref groupByDateUnit, viewModel.DirectoryGroupByDateUnit, nameof(GroupByDateUnit));
break;
case nameof(FolderSettingsViewModel.SortDirectoriesAlongsideFiles):
SetProperty(ref sortDirectoriesAlongsideFiles, viewModel.SortDirectoriesAlongsideFiles, nameof(SortDirectoriesAlongsideFiles));
break;
Expand All @@ -195,6 +202,7 @@ private void Update()
SetProperty(ref sortDirection, SortDirection.Ascending, nameof(SortDirection));
SetProperty(ref groupOption, GroupOption.None, nameof(GroupOption));
SetProperty(ref groupDirection, SortDirection.Ascending, nameof(GroupDirection));
SetProperty(ref groupByDateUnit, GroupByDateUnit.Year, nameof(GroupByDateUnit));
}
else
{
Expand All @@ -203,6 +211,7 @@ private void Update()
SetProperty(ref sortDirection, viewModel.DirectorySortDirection, nameof(SortDirection));
SetProperty(ref groupOption, viewModel.DirectoryGroupOption, nameof(GroupOption));
SetProperty(ref groupDirection, viewModel.DirectoryGroupDirection, nameof(GroupDirection));
SetProperty(ref groupByDateUnit, viewModel.DirectoryGroupByDateUnit, nameof(GroupByDateUnit));
SetProperty(ref sortDirectoriesAlongsideFiles, viewModel.SortDirectoriesAlongsideFiles, nameof(SortDirectoriesAlongsideFiles));
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/Files.App/Contexts/DisplayPage/IDisplayPageContext.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.Shared.Enums;
using System.ComponentModel;

namespace Files.App.Contexts
{
public interface IDisplayPageContext : INotifyPropertyChanging, INotifyPropertyChanged
Expand All @@ -16,6 +13,7 @@ public interface IDisplayPageContext : INotifyPropertyChanging, INotifyPropertyC

GroupOption GroupOption { get; set; }
SortDirection GroupDirection { get; set; }
GroupByDateUnit GroupByDateUnit { get; set; }

bool SortDirectoriesAlongsideFiles { get; set; }

Expand Down
31 changes: 14 additions & 17 deletions src/Files.App/Helpers/ContextFlyoutItemHelper.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Extensions;
using Files.App.Filesystem;
using Files.App.Interacts;
using Files.App.ViewModels;
using Files.Backend.Helpers;
using Files.Backend.Services;
using Files.Backend.Services.Settings;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media.Imaging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.System;

namespace Files.App.Helpers
{
Expand Down Expand Up @@ -155,8 +142,8 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
ShowInFtpPage = true,
ShowInZipPage = true,
},
new ContextMenuFlyoutItemViewModelBuilder(commands.SortAscending).Build(),
new ContextMenuFlyoutItemViewModelBuilder(commands.SortDescending).Build(),
new ContextMenuFlyoutItemViewModelBuilder(commands.SortAscending){IsToggle = true}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(commands.SortDescending){IsToggle = true}.Build(),
},
},
new ContextMenuFlyoutItemViewModel()
Expand Down Expand Up @@ -189,8 +176,18 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
ShowInFtpPage = true,
ShowInZipPage = true,
},
new ContextMenuFlyoutItemViewModelBuilder(commands.GroupAscending){IsVisible = true}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(commands.GroupDescending){IsVisible = true}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(commands.GroupAscending){IsToggle = true, IsVisible = true}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(commands.GroupDescending){IsToggle = true, IsVisible = true}.Build(),
new ContextMenuFlyoutItemViewModel
{
ItemType = ItemType.Separator,
ShowInRecycleBin = true,
ShowInSearchPage = true,
ShowInFtpPage = true,
ShowInZipPage = true,
},
new ContextMenuFlyoutItemViewModelBuilder(commands.GroupByYear){IsToggle = true, IsVisible = true}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(commands.GroupByMonth){IsToggle = true, IsVisible = true}.Build(),
},
},
new ContextMenuFlyoutItemViewModelBuilder(commands.RefreshItems)
Expand Down
Loading