Command refactor

This commit is contained in:
2022-05-15 21:42:01 +02:00
parent 9a99aad030
commit f99f90783f
42 changed files with 544 additions and 217 deletions

View File

@@ -0,0 +1,8 @@
namespace FileTime.App.Core.Models.Enums;
public enum PasteMode
{
Merge,
Overwrite,
Skip
}

View File

@@ -1,9 +0,0 @@
using FileTime.App.Core.Command;
namespace FileTime.App.Core.Services;
public interface ICommandHandler
{
bool CanHandleCommand(Command.Command command);
Task HandleCommandAsync(Command.Command command);
}

View File

@@ -1,8 +0,0 @@
using FileTime.App.Core.Command;
namespace FileTime.App.Core.Services;
public interface ICommandHandlerService
{
Task HandleCommandAsync(Command.Command command);
}

View File

@@ -0,0 +1,9 @@
using FileTime.App.Core.UserCommand;
namespace FileTime.App.Core.Services;
public interface IIdentifiableUserCommandService
{
void AddIdentifiableUserCommandFactory(string identifier, Func<IIdentifiableUserCommand> commandFactory);
IIdentifiableUserCommand GetCommand(string identifier);
}

View File

@@ -0,0 +1,9 @@
using FileTime.App.Core.UserCommand;
namespace FileTime.App.Core.Services;
public interface IUserCommandHandler
{
bool CanHandleCommand(IUserCommand command);
Task HandleCommandAsync(IUserCommand command);
}

View File

@@ -0,0 +1,7 @@
namespace FileTime.App.Core.Services;
public interface IUserCommandHandlerService
{
Task HandleCommandAsync(UserCommand.IUserCommand command);
Task HandleCommandAsync<TCommand>() where TCommand : UserCommand.IUserCommand, new();
}

View File

@@ -0,0 +1,13 @@
namespace FileTime.App.Core.UserCommand;
public class CloseTabCommand : IIdentifiableUserCommand
{
public const string CommandName = "close_tab";
public static CloseTabCommand Instance { get; } = new CloseTabCommand();
private CloseTabCommand()
{
}
public string UserCommandID => CommandName;
}

View File

@@ -0,0 +1,13 @@
namespace FileTime.App.Core.UserCommand;
public class CopyCommand : IIdentifiableUserCommand
{
public const string CommandName = "copy";
public static CopyCommand Instance { get; } = new CopyCommand();
private CopyCommand()
{
}
public string UserCommandID => CommandName;
}

View File

@@ -0,0 +1,13 @@
namespace FileTime.App.Core.UserCommand;
public class EnterRapidTravelCommand : IIdentifiableUserCommand
{
public const string CommandName = "exter_rapid_travel_mode";
public static EnterRapidTravelCommand Instance { get; } = new EnterRapidTravelCommand();
private EnterRapidTravelCommand()
{
}
public string UserCommandID => CommandName;
}

View File

@@ -0,0 +1,13 @@
namespace FileTime.App.Core.UserCommand;
public class ExitRapidTravelCommand : IIdentifiableUserCommand
{
public const string CommandName = "exit_rapid_travel_mode";
public static ExitRapidTravelCommand Instance { get; } = new ExitRapidTravelCommand();
private ExitRapidTravelCommand()
{
}
public string UserCommandID => CommandName;
}

View File

@@ -0,0 +1,13 @@
namespace FileTime.App.Core.UserCommand;
public class GoUpCommand : IIdentifiableUserCommand
{
public const string CommandName = "go_up";
public static GoUpCommand Instance { get; } = new GoUpCommand();
private GoUpCommand()
{
}
public string UserCommandID => CommandName;
}

View File

@@ -0,0 +1,6 @@
namespace FileTime.App.Core.UserCommand;
public interface IIdentifiableUserCommand : IUserCommand
{
string UserCommandID { get; }
}

View File

@@ -1,6 +1,11 @@
namespace FileTime.App.Core.Command;
namespace FileTime.App.Core.UserCommand;
public enum Command
public interface IUserCommand
{
}
/*public enum Command
{
None,
@@ -63,4 +68,4 @@ public enum Command
TimelineStart,
ToggleAdvancedIcons,
ToggleHidden,
}
}*/

View File

@@ -0,0 +1,13 @@
namespace FileTime.App.Core.UserCommand;
public class MarkCommand : IIdentifiableUserCommand
{
public const string CommandName = "mark_selected";
public static MarkCommand Instance { get; } = new MarkCommand();
private MarkCommand()
{
}
public string UserCommandID => CommandName;
}

View File

@@ -0,0 +1,13 @@
namespace FileTime.App.Core.UserCommand;
public class MoveCursorDownCommand : IIdentifiableUserCommand
{
public const string CommandName = "move_cursor_down";
public static MoveCursorDownCommand Instance { get; } = new MoveCursorDownCommand();
private MoveCursorDownCommand()
{
}
public string UserCommandID => CommandName;
}

View File

@@ -0,0 +1,13 @@
namespace FileTime.App.Core.UserCommand;
public class MoveCursorUpCommand : IIdentifiableUserCommand
{
public const string CommandName = "move_cursor_up";
public static MoveCursorUpCommand Instance { get; } = new MoveCursorUpCommand();
private MoveCursorUpCommand()
{
}
public string UserCommandID => CommandName;
}

View File

@@ -0,0 +1,13 @@
using FileTime.Core.Models;
namespace FileTime.App.Core.UserCommand;
public class OpenContainerCommand : IUserCommand
{
public IAbsolutePath Path { get; }
private OpenContainerCommand(IAbsolutePath path)
{
Path = path;
}
}

View File

@@ -0,0 +1,13 @@
namespace FileTime.App.Core.UserCommand;
public class OpenSelectedCommand : IIdentifiableUserCommand
{
public const string CommandName = "open_selected";
public static OpenSelectedCommand Instance { get; } = new OpenSelectedCommand();
private OpenSelectedCommand()
{
}
public string UserCommandID => CommandName;
}

View File

@@ -0,0 +1,24 @@
using FileTime.App.Core.Models.Enums;
namespace FileTime.App.Core.UserCommand;
public class PasteCommand : IIdentifiableUserCommand
{
public const string PasteMergeCommandName = "paste_merge";
public const string PasteOverwriteCommandName = "paste_overwrite";
public const string PasteSkipCommandName = "paste_skip";
public static PasteCommand Merge { get; } = new PasteCommand(PasteMode.Merge, PasteMergeCommandName);
public static PasteCommand Overwrite { get; } = new PasteCommand(PasteMode.Overwrite, PasteOverwriteCommandName);
public static PasteCommand Skip { get; } = new PasteCommand(PasteMode.Skip, PasteSkipCommandName);
public PasteMode PasteMode { get; }
private PasteCommand(PasteMode pasteMode, string commandName)
{
PasteMode = pasteMode;
UserCommandID = commandName;
}
public string UserCommandID { get; }
}

View File

@@ -0,0 +1,34 @@
namespace FileTime.App.Core.UserCommand;
public class SwitchToTabCommand : IIdentifiableUserCommand
{
private const string SwitchToTabBase = "switch_to_tab";
public const string SwitchToTab1CommandName = SwitchToTabBase + "1";
public const string SwitchToTab2CommandName = SwitchToTabBase + "2";
public const string SwitchToTab3CommandName = SwitchToTabBase + "3";
public const string SwitchToTab4CommandName = SwitchToTabBase + "4";
public const string SwitchToTab5CommandName = SwitchToTabBase + "5";
public const string SwitchToTab6CommandName = SwitchToTabBase + "6";
public const string SwitchToTab7CommandName = SwitchToTabBase + "7";
public const string SwitchToTab8CommandName = SwitchToTabBase + "8";
public const string SwitchToLastTabCommandName = "switch_to_last_tab";
public static SwitchToTabCommand SwitchToTab1 { get; } = new(1, SwitchToTab1CommandName);
public static SwitchToTabCommand SwitchToTab2 { get; } = new(2, SwitchToTab2CommandName);
public static SwitchToTabCommand SwitchToTab3 { get; } = new(3, SwitchToTab3CommandName);
public static SwitchToTabCommand SwitchToTab4 { get; } = new(4, SwitchToTab4CommandName);
public static SwitchToTabCommand SwitchToTab5 { get; } = new(5, SwitchToTab5CommandName);
public static SwitchToTabCommand SwitchToTab6 { get; } = new(6, SwitchToTab6CommandName);
public static SwitchToTabCommand SwitchToTab7 { get; } = new(7, SwitchToTab7CommandName);
public static SwitchToTabCommand SwitchToTab8 { get; } = new(8, SwitchToTab8CommandName);
public static SwitchToTabCommand SwitchToLastTab { get; } = new(-1, SwitchToLastTabCommandName);
private SwitchToTabCommand(int tabNumber, string commandName)
{
TabNumber = tabNumber;
UserCommandID = commandName;
}
public string UserCommandID { get; }
public int TabNumber { get; }
}

View File

@@ -0,0 +1,19 @@
using FileTime.App.Core.UserCommand;
namespace FileTime.App.Core.Services.UserCommandHandler;
public class IdentifiableUserCommandService : IIdentifiableUserCommandService
{
private readonly Dictionary<string, Func<IIdentifiableUserCommand>> _identifiableUserCommands = new();
public void AddIdentifiableUserCommandFactory(string identifier, Func<IIdentifiableUserCommand> commandFactory)
=> _identifiableUserCommands.Add(identifier, commandFactory);
public IIdentifiableUserCommand GetCommand(string identifier)
{
if (!_identifiableUserCommands.ContainsKey(identifier))
throw new IndexOutOfRangeException($"No command factory is registered for command {identifier}");
return _identifiableUserCommands[identifier].Invoke();
}
}

View File

@@ -1,27 +1,28 @@
using System.Reactive.Linq;
using FileTime.App.Core.Command;
using FileTime.App.Core.Models;
using FileTime.App.Core.Models.Enums;
using FileTime.App.Core.UserCommand;
using FileTime.App.Core.ViewModels;
using FileTime.Core.Command;
using FileTime.Core.Command.Copy;
using FileTime.Core.Models;
using CopyCommand = FileTime.Core.Command.Copy.CopyCommand;
namespace FileTime.App.Core.Services.CommandHandler;
namespace FileTime.App.Core.Services.UserCommandHandler;
public class ItemManipulationCommandHandler : CommandHandlerBase
public class ItemManipulationUserCommandHandlerService : UserCommandHandlerServiceBase
{
private ITabViewModel? _selectedTab;
private IItemViewModel? _currentSelectedItem;
private readonly ICommandHandlerService _commandHandlerService;
private readonly IUserCommandHandlerService _userCommandHandlerService;
private readonly IClipboardService _clipboardService;
private readonly BindedCollection<IAbsolutePath>? _markedItems;
public ItemManipulationCommandHandler(
public ItemManipulationUserCommandHandlerService(
IAppState appState,
ICommandHandlerService commandHandlerService,
IUserCommandHandlerService userCommandHandlerService,
IClipboardService clipboardService) : base(appState)
{
_commandHandlerService = commandHandlerService;
_userCommandHandlerService = userCommandHandlerService;
_clipboardService = clipboardService;
SaveSelectedTab(t => _selectedTab = t);
@@ -29,13 +30,11 @@ public class ItemManipulationCommandHandler : CommandHandlerBase
_markedItems = new BindedCollection<IAbsolutePath>(appState.SelectedTab.Select(t => t?.MarkedItems));
AddCommandHandlers(new (Command.Command, Func<Task>)[]
AddCommandHandlers(new IUserCommandHandler[]
{
(Command.Command.Copy, Copy),
(Command.Command.Mark, MarkItem),
(Command.Command.PasteMerge, PasteMerge),
(Command.Command.PasteOverwrite, PasteOverwrite),
(Command.Command.PasteSkip, PasteSkip),
new TypeUserCommandHandler<CopyCommand>(Copy),
new TypeUserCommandHandler<MarkCommand>(MarkItem),
new TypeUserCommandHandler<PasteCommand>(Paste),
});
}
@@ -44,7 +43,7 @@ public class ItemManipulationCommandHandler : CommandHandlerBase
if (_selectedTab == null || _currentSelectedItem?.BaseItem?.FullName == null) return;
_selectedTab.ToggleMarkedItem(new AbsolutePath(_currentSelectedItem.BaseItem));
await _commandHandlerService.HandleCommandAsync(Command.Command.MoveCursorDown);
await _userCommandHandlerService.HandleCommandAsync(MoveCursorDownCommand.Instance);
}
private Task Copy()
@@ -69,6 +68,17 @@ public class ItemManipulationCommandHandler : CommandHandlerBase
return Task.CompletedTask;
}
private async Task Paste(PasteCommand command)
{
await (command.PasteMode switch
{
PasteMode.Merge => PasteMerge(),
PasteMode.Overwrite => PasteOverwrite(),
PasteMode.Skip => PasteSkip(),
_ => throw new ArgumentException($"Unknown {nameof(PasteMode)} value: {command.PasteMode}")
});
}
private async Task PasteMerge()
{
await Paste(TransportMode.Merge);

View File

@@ -1,37 +1,36 @@
using System.Reactive.Linq;
using FileTime.App.Core.Command;
using FileTime.App.Core.Extensions;
using FileTime.App.Core.Models.Enums;
using FileTime.App.Core.UserCommand;
using FileTime.App.Core.ViewModels;
using FileTime.Core.Models;
using FileTime.Core.Services;
using FileTime.Providers.Local;
using Microsoft.Extensions.DependencyInjection;
namespace FileTime.App.Core.Services.CommandHandler;
namespace FileTime.App.Core.Services.UserCommandHandler;
public class NavigationCommandHandler : CommandHandlerBase
public class NavigationUserCommandHandlerService : UserCommandHandlerServiceBase
{
private readonly IAppState _appState;
private readonly IServiceProvider _serviceProvider;
private readonly ILocalContentProvider _localContentProvider;
private readonly ICommandHandlerService _commandHandlerService;
private readonly IUserCommandHandlerService _userCommandHandlerService;
private ITabViewModel? _selectedTab;
private IContainer? _currentLocation;
private IItemViewModel? _currentSelectedItem;
private IEnumerable<IItemViewModel> _currentItems = Enumerable.Empty<IItemViewModel>();
private ViewMode _viewMode;
public NavigationCommandHandler(
public NavigationUserCommandHandlerService(
IAppState appState,
IServiceProvider serviceProvider,
ILocalContentProvider localContentProvider,
ICommandHandlerService commandHandlerService) : base(appState)
IUserCommandHandlerService userCommandHandlerService) : base(appState)
{
_appState = appState;
_serviceProvider = serviceProvider;
_localContentProvider = localContentProvider;
_commandHandlerService = commandHandlerService;
_userCommandHandlerService = userCommandHandlerService;
SaveSelectedTab(t => _selectedTab = t);
SaveCurrentSelectedItem(i => _currentSelectedItem = i);
@@ -40,24 +39,16 @@ public class NavigationCommandHandler : CommandHandlerBase
appState.ViewMode.Subscribe(v => _viewMode = v);
AddCommandHandlers(new (Command.Command, Func<Task>)[]
AddCommandHandlers(new IUserCommandHandler[]
{
(Command.Command.CloseTab, CloseTab),
(Command.Command.EnterRapidTravel, EnterRapidTravel),
(Command.Command.ExitRapidTravel, ExitRapidTravel),
(Command.Command.GoUp, GoUp),
(Command.Command.MoveCursorDown, MoveCursorDown),
(Command.Command.MoveCursorUp, MoveCursorUp),
(Command.Command.Open, OpenContainer),
(Command.Command.SwitchToLastTab, async () => await SwitchToTab(-1)),
(Command.Command.SwitchToTab1, async () => await SwitchToTab(1)),
(Command.Command.SwitchToTab2, async () => await SwitchToTab(2)),
(Command.Command.SwitchToTab3, async () => await SwitchToTab(3)),
(Command.Command.SwitchToTab4, async () => await SwitchToTab(4)),
(Command.Command.SwitchToTab5, async () => await SwitchToTab(5)),
(Command.Command.SwitchToTab6, async () => await SwitchToTab(6)),
(Command.Command.SwitchToTab7, async () => await SwitchToTab(7)),
(Command.Command.SwitchToTab8, async () => await SwitchToTab(8)),
new TypeUserCommandHandler<CloseTabCommand>(CloseTab),
new TypeUserCommandHandler<EnterRapidTravelCommand>(EnterRapidTravel),
new TypeUserCommandHandler<ExitRapidTravelCommand>(ExitRapidTravel),
new TypeUserCommandHandler<GoUpCommand>(GoUp),
new TypeUserCommandHandler<MoveCursorDownCommand>(MoveCursorDown),
new TypeUserCommandHandler<MoveCursorUpCommand>(MoveCursorUp),
new TypeUserCommandHandler<OpenSelectedCommand>(OpenContainer),
new TypeUserCommandHandler<SwitchToTabCommand>(SwitchToTab),
});
}
@@ -109,8 +100,9 @@ public class NavigationCommandHandler : CommandHandlerBase
return Task.CompletedTask;
}
private Task SwitchToTab(int number)
private Task SwitchToTab(SwitchToTabCommand command)
{
var number = command.TabNumber;
var tabViewModel = _appState.Tabs.FirstOrDefault(t => t.TabNumber == number);
if (number == -1)
@@ -129,7 +121,7 @@ public class NavigationCommandHandler : CommandHandlerBase
if (_viewMode == ViewMode.RapidTravel)
{
_commandHandlerService.HandleCommandAsync(Command.Command.ExitRapidTravel);
_userCommandHandlerService.HandleCommandAsync(ExitRapidTravelCommand.Instance);
}
_appState.SetSelectedTab(tabViewModel!);

View File

@@ -0,0 +1,26 @@
using FileTime.App.Core.UserCommand;
namespace FileTime.App.Core.Services.UserCommandHandler;
public class TypeUserCommandHandler<T> : IUserCommandHandler
{
private readonly Func<T, Task> _handler;
public TypeUserCommandHandler(Func<T, Task> handler)
{
_handler = handler;
}
public TypeUserCommandHandler(Func<Task> handler)
{
_handler = async (_) => await handler();
}
public bool CanHandleCommand(IUserCommand command) => command is T;
public Task HandleCommandAsync(IUserCommand command)
{
if (command is not T typedCommand) throw new ArgumentException($"Parameter '{nameof(command)}' is not of type '{typeof(T).Name}'");
return _handler(typedCommand);
}
}

View File

@@ -0,0 +1,16 @@
namespace FileTime.App.Core.Services.UserCommandHandler;
public sealed class UserCommandHandler : IUserCommandHandler
{
private readonly Func<UserCommand.IUserCommand, bool> _canHandle;
private readonly Func<UserCommand.IUserCommand, Task> _handle;
public UserCommandHandler(Func<UserCommand.IUserCommand, bool> canHandle, Func<UserCommand.IUserCommand, Task> handle)
{
_canHandle = canHandle;
_handle = handle;
}
public bool CanHandleCommand(UserCommand.IUserCommand command) => _canHandle(command);
public async Task HandleCommandAsync(UserCommand.IUserCommand command) => await _handle(command);
}

View File

@@ -1,37 +1,44 @@
using System.Reactive.Linq;
using DynamicData;
using FileTime.App.Core.Command;
using FileTime.App.Core.ViewModels;
using FileTime.Core.Models;
namespace FileTime.App.Core.Services.CommandHandler;
namespace FileTime.App.Core.Services.UserCommandHandler;
public abstract class CommandHandlerBase : ICommandHandler
public abstract class UserCommandHandlerServiceBase : IUserCommandHandler
{
private readonly Dictionary<Command.Command, Func<Task>> _commandHandlers = new();
private readonly List<IUserCommandHandler> _userCommandHandlers = new();
private readonly IAppState? _appState;
protected CommandHandlerBase(IAppState? appState = null)
protected UserCommandHandlerServiceBase(IAppState? appState = null)
{
_appState = appState;
}
public bool CanHandleCommand(Command.Command command) => _commandHandlers.ContainsKey(command);
public bool CanHandleCommand(UserCommand.IUserCommand command) => _userCommandHandlers.Any(h => h.CanHandleCommand(command));
public async Task HandleCommandAsync(Command.Command command) => await _commandHandlers[command].Invoke();
protected void AddCommandHandler(Command.Command command, Func<Task> handler) => _commandHandlers.Add(command, handler);
protected void AddCommandHandlers(IEnumerable<(Command.Command command, Func<Task> handler)> commandHandlers)
public async Task HandleCommandAsync(UserCommand.IUserCommand command)
{
foreach (var (command, handler) in commandHandlers)
var handler = _userCommandHandlers.Find(h => h.CanHandleCommand(command));
if (handler is null) return;
await handler.HandleCommandAsync(command);
}
protected void AddCommandHandler(IUserCommandHandler userCommandHandler) => _userCommandHandlers.Add(userCommandHandler);
protected void AddCommandHandlers(IEnumerable<IUserCommandHandler> commandHandlers)
{
foreach (var userCommandHandler in commandHandlers)
{
AddCommandHandler(command, handler);
AddCommandHandler(userCommandHandler);
}
}
protected void RemoveCommandHandler(Command.Command command) => _commandHandlers.Remove(command);
protected void RemoveCommandHandler(IUserCommandHandler userCommandHandler) => _userCommandHandlers.Remove(userCommandHandler);
protected IDisposable SaveSelectedTab(Action<ITabViewModel?> handler) => RunWithAppState(appState => appState.SelectedTab.Subscribe(handler));
protected IDisposable SaveCurrentSelectedItem(Action<IItemViewModel?> handler)
=> RunWithAppState(appState => appState.SelectedTab.Select(t => t == null ? Observable.Return<IItemViewModel?>(null) : t.CurrentSelectedItem).Switch().Subscribe(handler));
@@ -39,14 +46,14 @@ public abstract class CommandHandlerBase : ICommandHandler
=> RunWithAppState(appState => appState.SelectedTab.Select(t => t == null ? Observable.Return<IContainer?>(null) : t.CurrentLocation).Switch().Subscribe(handler));
protected IDisposable SaveCurrentItems(Action<IEnumerable<IItemViewModel>> handler)
=> RunWithAppState(appState => appState.SelectedTab.Select(t => t?.CurrentItemsCollectionObservable ?? Observable.Return((IEnumerable<IItemViewModel>?)Enumerable.Empty<IItemViewModel>())).Switch().Subscribe(i => handler(i ?? Enumerable.Empty<IItemViewModel>())));
=> RunWithAppState(appState => appState.SelectedTab.Select(t => t?.CurrentItemsCollectionObservable ?? Observable.Return((IEnumerable<IItemViewModel>?) Enumerable.Empty<IItemViewModel>())).Switch().Subscribe(i => handler(i ?? Enumerable.Empty<IItemViewModel>())));
protected IDisposable SaveMarkedItems(Action<IChangeSet<IAbsolutePath>> handler)
=> RunWithAppState(appstate => appstate.SelectedTab.Select(t => t == null ? Observable.Empty<IChangeSet<IAbsolutePath>>() : t.MarkedItems).Switch().Subscribe(handler));
=> RunWithAppState(appState => appState.SelectedTab.Select(t => t == null ? Observable.Empty<IChangeSet<IAbsolutePath>>() : t.MarkedItems).Switch().Subscribe(handler));
private IDisposable RunWithAppState(Func<IAppState, IDisposable> act)
{
if (_appState == null) throw new NullReferenceException($"AppState is nit initialized in {nameof(CommandHandlerBase)}.");
if (_appState == null) throw new NullReferenceException($"AppState is nit initialized in {nameof(UserCommandHandlerServiceBase)}.");
return act(_appState);
}

View File

@@ -1,15 +1,14 @@
using FileTime.App.Core.Command;
using Microsoft.Extensions.DependencyInjection;
namespace FileTime.App.Core.Services;
public class CommandHandlerService : ICommandHandlerService
public class UserCommandHandlerService : IUserCommandHandlerService
{
private readonly Lazy<IEnumerable<ICommandHandler>> _commandHandlers;
private readonly Lazy<IEnumerable<IUserCommandHandler>> _commandHandlers;
public CommandHandlerService(IServiceProvider serviceProvider)
public UserCommandHandlerService(IServiceProvider serviceProvider)
{
_commandHandlers = new Lazy<IEnumerable<ICommandHandler>>(() => serviceProvider.GetServices<ICommandHandler>());
_commandHandlers = new Lazy<IEnumerable<IUserCommandHandler>>(serviceProvider.GetServices<IUserCommandHandler>);
//(Commands.AutoRefresh, ToggleAutoRefresh),
//(Commands.ChangeTimelineMode, ChangeTimelineMode),
@@ -67,7 +66,7 @@ public class CommandHandlerService : ICommandHandlerService
//(Commands.ToggleHidden, ToggleHidden),
}
public async Task HandleCommandAsync(Command.Command command)
public async Task HandleCommandAsync(UserCommand.IUserCommand command)
{
var handler = _commandHandlers.Value.FirstOrDefault(h => h.CanHandleCommand(command));
@@ -76,4 +75,7 @@ public class CommandHandlerService : ICommandHandlerService
await handler.HandleCommandAsync(command);
}
}
public async Task HandleCommandAsync<TUserCommand>() where TUserCommand : UserCommand.IUserCommand, new()
=> await HandleCommandAsync(new TUserCommand());
}

View File

@@ -1,5 +1,6 @@
using FileTime.App.Core.Services;
using FileTime.App.Core.Services.CommandHandler;
using FileTime.App.Core.Services.UserCommandHandler;
using FileTime.App.Core.StartupServices;
using FileTime.App.Core.ViewModels;
using Microsoft.Extensions.DependencyInjection;
@@ -14,15 +15,17 @@ public static class Startup
.AddTransient<IContainerViewModel, ContainerViewModel>()
.AddTransient<IElementViewModel, ElementViewModel>()
.AddTransient<IItemNameConverterService, ItemNameConverterService>()
.AddSingleton<ICommandHandlerService, CommandHandlerService>()
.AddSingleton<IUserCommandHandlerService, UserCommandHandlerService>()
.AddSingleton<IClipboardService, ClipboardService>()
.AddSingleton<IIdentifiableUserCommandService, IdentifiableUserCommandService>()
.AddSingleton<IStartupHandler, DefaultIdentifiableCommandHandlerRegister>()
.AddCommandHandlers();
}
private static IServiceCollection AddCommandHandlers(this IServiceCollection serviceCollection)
{
return serviceCollection
.AddSingleton<ICommandHandler, NavigationCommandHandler>()
.AddSingleton<ICommandHandler, ItemManipulationCommandHandler>();
.AddSingleton<IUserCommandHandler, NavigationUserCommandHandlerService>()
.AddSingleton<IUserCommandHandler, ItemManipulationUserCommandHandlerService>();
}
}

View File

@@ -0,0 +1,39 @@
using FileTime.App.Core.Services;
using FileTime.App.Core.UserCommand;
namespace FileTime.App.Core.StartupServices;
public class DefaultIdentifiableCommandHandlerRegister : IStartupHandler
{
private readonly IIdentifiableUserCommandService _service;
public DefaultIdentifiableCommandHandlerRegister(IIdentifiableUserCommandService service)
{
_service = service;
AddUserCommand(CloseTabCommand.Instance);
AddUserCommand(CopyCommand.Instance);
AddUserCommand(EnterRapidTravelCommand.Instance);
AddUserCommand(ExitRapidTravelCommand.Instance);
AddUserCommand(GoUpCommand.Instance);
AddUserCommand(MarkCommand.Instance);
AddUserCommand(MoveCursorDownCommand.Instance);
AddUserCommand(MoveCursorUpCommand.Instance);
AddUserCommand(OpenSelectedCommand.Instance);
AddUserCommand(PasteCommand.Merge);
AddUserCommand(PasteCommand.Overwrite);
AddUserCommand(PasteCommand.Skip);
AddUserCommand(SwitchToTabCommand.SwitchToTab1);
AddUserCommand(SwitchToTabCommand.SwitchToTab2);
AddUserCommand(SwitchToTabCommand.SwitchToTab3);
AddUserCommand(SwitchToTabCommand.SwitchToTab4);
AddUserCommand(SwitchToTabCommand.SwitchToTab5);
AddUserCommand(SwitchToTabCommand.SwitchToTab6);
AddUserCommand(SwitchToTabCommand.SwitchToTab7);
AddUserCommand(SwitchToTabCommand.SwitchToTab8);
AddUserCommand(SwitchToTabCommand.SwitchToLastTab);
}
private void AddUserCommand(IIdentifiableUserCommand command)
=> _service.AddIdentifiableUserCommandFactory(command.UserCommandID, () => command);
}