Item ordering

This commit is contained in:
2023-07-28 01:38:53 +02:00
parent 27ebb1efa8
commit 0db7949037
13 changed files with 155 additions and 28 deletions

View File

@@ -56,6 +56,7 @@ public class CommandPaletteViewModel : FuzzyPanelViewModel<ICommandPaletteEntryV
if (keyEventArgs.Key == Key.Escape)
{
keyEventArgs.Handled = true;
Close();
return true;
}
@@ -67,6 +68,9 @@ public class CommandPaletteViewModel : FuzzyPanelViewModel<ICommandPaletteEntryV
var command = _identifiableUserCommandService.GetCommand(SelectedItem.Identifier);
if (command is null) return false;
keyEventArgs.Handled = true;
Close();
try
{
await _userCommandHandlerService.HandleCommandAsync(command);
@@ -76,7 +80,6 @@ public class CommandPaletteViewModel : FuzzyPanelViewModel<ICommandPaletteEntryV
_logger.LogError(e, "Unknown error while running command. {Command} {Error}", command.GetType().Name, e);
}
Close();
return true;
}

View File

@@ -0,0 +1,9 @@
namespace FileTime.App.Core.Models;
public enum ItemOrdering
{
Name,
NameDesc,
LastModifyDate,
LastModifyDateDesc,
}

View File

@@ -25,7 +25,7 @@ public class IdentifiableSearchCommand : SearchCommand, IIdentifiableUserCommand
public static readonly IdentifiableSearchCommand SearchByNameContains =
new(null, SearchType.NameContains, SearchByNameContainsCommandName);
public IdentifiableSearchCommand(
private IdentifiableSearchCommand(
string? searchText,
SearchType searchType,
string commandId)

View File

@@ -0,0 +1,33 @@
using FileTime.App.Core.Models;
namespace FileTime.App.Core.UserCommand;
public class SortItemsCommand : IIdentifiableUserCommand
{
public const string OrderByNameCommandName = "order_by_name";
public const string OrderByNameDescCommandName = "order_by_name_desc";
public const string OrderByDateCommandName = "order_by_date";
public const string OrderByDateDescCommandName = "order_by_date_desc";
public static readonly SortItemsCommand OrderByNameCommand =
new(OrderByNameCommandName, ItemOrdering.Name);
public static readonly SortItemsCommand OrderByNameDescCommand =
new(OrderByNameDescCommandName, ItemOrdering.NameDesc);
public static readonly SortItemsCommand OrderByDateCommand =
new(OrderByDateCommandName, ItemOrdering.LastModifyDate);
public static readonly SortItemsCommand OrderByDateDescCommand =
new(OrderByDateDescCommandName, ItemOrdering.LastModifyDateDesc);
private SortItemsCommand(string userCommandId, ItemOrdering ordering)
{
UserCommandID = userCommandId;
Ordering = ordering;
}
public string UserCommandID { get; }
public ItemOrdering Ordering { get; }
}

View File

@@ -2,6 +2,7 @@
using System.Collections.ObjectModel;
using DeclarativeProperty;
using DynamicData;
using FileTime.App.Core.Models;
using FileTime.Core.Models;
using FileTime.Core.Services;
using InitableService;
@@ -20,6 +21,7 @@ public interface ITabViewModel : IInitable<ITab, int>, IDisposable
IObservable<IChangeSet<FullName>> MarkedItems { get; }
IDeclarativeProperty<ObservableCollection<IItemViewModel>> SelectedsChildren { get; }
IDeclarativeProperty<ObservableCollection<IItemViewModel>> ParentsChildren { get; }
DeclarativeProperty<ItemOrdering?> Ordering { get; }
void ClearMarkedItems();
void RemoveMarkedItem(FullName fullName);

View File

@@ -22,6 +22,7 @@ public class ToolUserCommandHandlerService : UserCommandHandlerServiceBase
private readonly IContentAccessorFactory _contentAccessorFactory;
private IDeclarativeProperty<IContainer?>? _currentLocation;
private IDeclarativeProperty<IItemViewModel?>? _currentSelectedItem;
private ITabViewModel? _currentSelectedTab;
public ToolUserCommandHandlerService(
IAppState appState,
@@ -42,6 +43,7 @@ public class ToolUserCommandHandlerService : UserCommandHandlerServiceBase
_contentAccessorFactory = contentAccessorFactory;
SaveCurrentLocation(l => _currentLocation = l);
SaveCurrentSelectedItem(i => _currentSelectedItem = i);
SaveSelectedTab(t => _currentSelectedTab = t);
AddCommandHandlers(new IUserCommandHandler[]
{
@@ -49,9 +51,17 @@ public class ToolUserCommandHandlerService : UserCommandHandlerServiceBase
new TypeUserCommandHandler<CopyNativePathCommand>(CopyNativePath),
new TypeUserCommandHandler<CopyBase64Command>(CopyBase64),
new TypeUserCommandHandler<SearchCommand>(Search),
new TypeUserCommandHandler<SortItemsCommand>(SortItems),
});
}
private async Task SortItems(SortItemsCommand sortItemsCommand)
{
if (_currentSelectedTab is null) return;
await _currentSelectedTab.Ordering.SetValue(sortItemsCommand.Ordering);
}
private async Task CopyBase64()
{
var item = _currentSelectedItem?.Value?.BaseItem;

View File

@@ -49,6 +49,10 @@ public class DefaultIdentifiableCommandHandlerRegister : IStartupHandler
AddUserCommand(RenameCommand.Instance);
AddUserCommand(RunOrOpenCommand.Instance);
AddUserCommand(StartCommandSchedulerCommand.Instance);
AddUserCommand(SortItemsCommand.OrderByNameCommand);
AddUserCommand(SortItemsCommand.OrderByNameDescCommand);
AddUserCommand(SortItemsCommand.OrderByDateCommand);
AddUserCommand(SortItemsCommand.OrderByDateDescCommand);
AddUserCommand(IdentifiableSearchCommand.SearchByNameContains);
AddUserCommand(SwitchToTabCommand.SwitchToLastTab);
AddUserCommand(SwitchToTabCommand.SwitchToTab1);

View File

@@ -1,9 +1,12 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reactive.Linq;
using DeclarativeProperty;
using DynamicData;
using DynamicData.Binding;
using FileTime.App.Core.Extensions;
using FileTime.App.Core.Models;
using FileTime.App.Core.Models.Enums;
using FileTime.App.Core.Services;
using FileTime.Core.Enums;
@@ -45,6 +48,7 @@ public partial class TabViewModel : ITabViewModel
public IObservable<IChangeSet<FullName>> MarkedItems { get; }
public IDeclarativeProperty<ObservableCollection<IItemViewModel>?> SelectedsChildren { get; private set; }
public IDeclarativeProperty<ObservableCollection<IItemViewModel>?> ParentsChildren { get; private set; }
public DeclarativeProperty<ItemOrdering?> Ordering { get; } = new(ItemOrdering.Name);
public TabViewModel(
@@ -71,14 +75,56 @@ public partial class TabViewModel : ITabViewModel
CurrentLocation = tab.CurrentLocation;
CurrentItems = tab.CurrentItems
.Map((items, _) =>
Task.FromResult<ObservableCollection<IItemViewModel>?>(
items?.Selecting<IItem, IItemViewModel>(
i => MapItemToViewModel(i, ItemViewModelType.Main)
CurrentItems =
tab.CurrentItems
.Map((items, _) =>
Task.FromResult<ObservableCollection<IItemViewModel>?>(
items?.Selecting<IItem, IItemViewModel>(
i => MapItemToViewModel(i, ItemViewModelType.Main)
)
)
)
);
).CombineLatest(
Ordering,
(items, ordering) =>
{
if (items is null) return Task.FromResult<ObservableCollection<IItemViewModel>?>(null);
/*Expression<Func<IItemViewModel, object?>> orderExpression = ordering switch
{
ItemOrdering.Name or ItemOrdering.NameDesc => i => i.DisplayNameText,
ItemOrdering.LastModifyDate or ItemOrdering.LastModifyDateDesc => i => i.CreatedAt,
_ => throw new NotImplementedException()
};
Expression<Func<ListSortDirection>> direction = ordering switch
{
ItemOrdering.Name or ItemOrdering.LastModifyDate => () => ListSortDirection.Ascending,
ItemOrdering.NameDesc or ItemOrdering.LastModifyDateDesc => () => ListSortDirection.Descending,
_ => throw new NotImplementedException()
};
return Task.FromResult((ObservableCollection<IItemViewModel>?) items.Ordering(orderExpression, direction));*/
ObservableCollection<IItemViewModel>? orderedItems = ordering switch
{
ItemOrdering.Name =>
items
.Ordering(i => i.BaseItem.Type)
.ThenOrdering(i => i.DisplayNameText),
ItemOrdering.NameDesc =>
items
.Ordering(i => i.BaseItem.Type)
.ThenOrdering(i => i.DisplayNameText, ListSortDirection.Descending),
ItemOrdering.LastModifyDate =>
items
.Ordering(i => i.CreatedAt),
ItemOrdering.LastModifyDateDesc =>
items
.Ordering(i => i.CreatedAt, ListSortDirection.Descending),
_ => throw new NotImplementedException()
};
return Task.FromResult(orderedItems);
}
);
using var _ = Defer(
() => CurrentItems.Subscribe(c => UpdateConsumer(c, ref _currentItemsConsumer))
);
@@ -127,6 +173,8 @@ public partial class TabViewModel : ITabViewModel
var items = parent.Items
.Selecting(i => MapItem(i))
.Ordering(i => i.Type)
.ThenOrdering(i => i.Name)
.Selecting(i => MapItemToViewModel(i, ItemViewModelType.SelectedChild));
return items;
@@ -222,7 +270,7 @@ public partial class TabViewModel : ITabViewModel
}
}
public void ClearMarkedItems()
public void ClearMarkedItems()
=> _markedItems.Clear();
~TabViewModel() => Dispose(false);

View File

@@ -33,11 +33,12 @@ public class FrequencyNavigationViewModel : FuzzyPanelViewModel<string>, IFreque
public override async Task<bool> HandleKeyDown(KeyEventArgs keyEventArgs)
{
var handled = await base.HandleKeyDown(keyEventArgs);
if (handled) return true;
if (keyEventArgs.Key == Key.Enter)
{
keyEventArgs.Handled = true;
var targetContainer = await _timelessContentProvider.GetItemByFullNameAsync(new FullName(SelectedItem), PointInTime.Present);
var openContainerCommand = new OpenContainerCommand(new AbsolutePath(_timelessContentProvider, targetContainer));
await _userCommandHandlerService.HandleCommandAsync(openContainerCommand);

View File

@@ -46,6 +46,7 @@ public abstract partial class FuzzyPanelViewModel<TItem> : IFuzzyPanelViewModel<
if (nextItem is not null)
{
keyEventArgs.Handled = true;
SelectedItem = nextItem;
}
@@ -57,6 +58,7 @@ public abstract partial class FuzzyPanelViewModel<TItem> : IFuzzyPanelViewModel<
if (previousItem is not null)
{
keyEventArgs.Handled = true;
SelectedItem = previousItem;
}