Command refactor
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
using System.Reactive.Linq;
|
||||
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.Models;
|
||||
using CopyCommand = FileTime.Core.Command.Copy.CopyCommand;
|
||||
|
||||
namespace FileTime.App.Core.Services.UserCommandHandler;
|
||||
|
||||
public class ItemManipulationUserCommandHandlerService : UserCommandHandlerServiceBase
|
||||
{
|
||||
private ITabViewModel? _selectedTab;
|
||||
private IItemViewModel? _currentSelectedItem;
|
||||
private readonly IUserCommandHandlerService _userCommandHandlerService;
|
||||
private readonly IClipboardService _clipboardService;
|
||||
private readonly BindedCollection<IAbsolutePath>? _markedItems;
|
||||
|
||||
public ItemManipulationUserCommandHandlerService(
|
||||
IAppState appState,
|
||||
IUserCommandHandlerService userCommandHandlerService,
|
||||
IClipboardService clipboardService) : base(appState)
|
||||
{
|
||||
_userCommandHandlerService = userCommandHandlerService;
|
||||
_clipboardService = clipboardService;
|
||||
|
||||
SaveSelectedTab(t => _selectedTab = t);
|
||||
SaveCurrentSelectedItem(i => _currentSelectedItem = i);
|
||||
|
||||
_markedItems = new BindedCollection<IAbsolutePath>(appState.SelectedTab.Select(t => t?.MarkedItems));
|
||||
|
||||
AddCommandHandlers(new IUserCommandHandler[]
|
||||
{
|
||||
new TypeUserCommandHandler<CopyCommand>(Copy),
|
||||
new TypeUserCommandHandler<MarkCommand>(MarkItem),
|
||||
new TypeUserCommandHandler<PasteCommand>(Paste),
|
||||
});
|
||||
}
|
||||
|
||||
private async Task MarkItem()
|
||||
{
|
||||
if (_selectedTab == null || _currentSelectedItem?.BaseItem?.FullName == null) return;
|
||||
|
||||
_selectedTab.ToggleMarkedItem(new AbsolutePath(_currentSelectedItem.BaseItem));
|
||||
await _userCommandHandlerService.HandleCommandAsync(MoveCursorDownCommand.Instance);
|
||||
}
|
||||
|
||||
private Task Copy()
|
||||
{
|
||||
_clipboardService.Clear();
|
||||
_clipboardService.SetCommand<CopyCommand>();
|
||||
|
||||
if ((_markedItems?.Collection?.Count ?? 0) > 0)
|
||||
{
|
||||
foreach (var item in _markedItems!.Collection!)
|
||||
{
|
||||
_clipboardService.AddContent(item);
|
||||
}
|
||||
|
||||
_selectedTab?.ClearMarkedItems();
|
||||
}
|
||||
else if (_currentSelectedItem?.BaseItem != null)
|
||||
{
|
||||
_clipboardService.AddContent(new AbsolutePath(_currentSelectedItem.BaseItem));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private async Task PasteOverwrite()
|
||||
{
|
||||
await Paste(TransportMode.Overwrite);
|
||||
}
|
||||
|
||||
private async Task PasteSkip()
|
||||
{
|
||||
await Paste(TransportMode.Skip);
|
||||
}
|
||||
|
||||
private Task Paste(TransportMode skip)
|
||||
{
|
||||
if (_clipboardService.CommandType is null) return Task.CompletedTask;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
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.UserCommandHandler;
|
||||
|
||||
public class NavigationUserCommandHandlerService : UserCommandHandlerServiceBase
|
||||
{
|
||||
private readonly IAppState _appState;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly ILocalContentProvider _localContentProvider;
|
||||
private readonly IUserCommandHandlerService _userCommandHandlerService;
|
||||
private ITabViewModel? _selectedTab;
|
||||
private IContainer? _currentLocation;
|
||||
private IItemViewModel? _currentSelectedItem;
|
||||
private IEnumerable<IItemViewModel> _currentItems = Enumerable.Empty<IItemViewModel>();
|
||||
private ViewMode _viewMode;
|
||||
|
||||
public NavigationUserCommandHandlerService(
|
||||
IAppState appState,
|
||||
IServiceProvider serviceProvider,
|
||||
ILocalContentProvider localContentProvider,
|
||||
IUserCommandHandlerService userCommandHandlerService) : base(appState)
|
||||
{
|
||||
_appState = appState;
|
||||
_serviceProvider = serviceProvider;
|
||||
_localContentProvider = localContentProvider;
|
||||
_userCommandHandlerService = userCommandHandlerService;
|
||||
|
||||
SaveSelectedTab(t => _selectedTab = t);
|
||||
SaveCurrentSelectedItem(i => _currentSelectedItem = i);
|
||||
SaveCurrentLocation(l => _currentLocation = l);
|
||||
SaveCurrentItems(i => _currentItems = i);
|
||||
|
||||
appState.ViewMode.Subscribe(v => _viewMode = v);
|
||||
|
||||
AddCommandHandlers(new IUserCommandHandler[]
|
||||
{
|
||||
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),
|
||||
});
|
||||
}
|
||||
|
||||
private Task OpenContainer()
|
||||
{
|
||||
if (_currentSelectedItem is not IContainerViewModel containerViewModel || containerViewModel.Container is null) return Task.CompletedTask;
|
||||
|
||||
_selectedTab?.Tab?.SetCurrentLocation(containerViewModel.Container);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task GoUp()
|
||||
{
|
||||
if (_currentLocation?.Parent is not IAbsolutePath parentPath || await parentPath.ResolveAsyncSafe() is not IContainer newContainer) return;
|
||||
_selectedTab?.Tab?.SetCurrentLocation(newContainer);
|
||||
}
|
||||
|
||||
private Task MoveCursorDown()
|
||||
{
|
||||
SelectNewSelectedItem(i => i.SkipWhile(i => !i.EqualsTo(_currentSelectedItem)).Skip(1).FirstOrDefault());
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task MoveCursorUp()
|
||||
{
|
||||
SelectNewSelectedItem(i => i.TakeWhile(i => !i.EqualsTo(_currentSelectedItem)).LastOrDefault());
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void SelectNewSelectedItem(Func<IEnumerable<IItemViewModel>, IItemViewModel?> getNewSelected)
|
||||
{
|
||||
if (_selectedTab is null || _currentLocation is null) return;
|
||||
|
||||
var newSelectedItem = getNewSelected(_currentItems);
|
||||
if (newSelectedItem == null) return;
|
||||
|
||||
_selectedTab.Tab?.SetSelectedItem(newSelectedItem.ToAbsolutePath());
|
||||
}
|
||||
|
||||
private Task EnterRapidTravel()
|
||||
{
|
||||
_appState.SwitchViewMode(ViewMode.RapidTravel);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task ExitRapidTravel()
|
||||
{
|
||||
_appState.SwitchViewMode(ViewMode.Default);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task SwitchToTab(SwitchToTabCommand command)
|
||||
{
|
||||
var number = command.TabNumber;
|
||||
var tabViewModel = _appState.Tabs.FirstOrDefault(t => t.TabNumber == number);
|
||||
|
||||
if (number == -1)
|
||||
{
|
||||
var greatestNumber = _appState.Tabs.Max(t => t.TabNumber);
|
||||
tabViewModel = _appState.Tabs.FirstOrDefault(t => t.TabNumber == greatestNumber);
|
||||
}
|
||||
else if (tabViewModel == null)
|
||||
{
|
||||
var tab = _serviceProvider.GetInitableResolver<IContainer>(_currentLocation ?? _localContentProvider).GetRequiredService<ITab>();
|
||||
var newTabViewModel = _serviceProvider.GetInitableResolver(tab, number).GetRequiredService<ITabViewModel>();
|
||||
|
||||
_appState.AddTab(newTabViewModel);
|
||||
tabViewModel = newTabViewModel;
|
||||
}
|
||||
|
||||
if (_viewMode == ViewMode.RapidTravel)
|
||||
{
|
||||
_userCommandHandlerService.HandleCommandAsync(ExitRapidTravelCommand.Instance);
|
||||
}
|
||||
|
||||
_appState.SetSelectedTab(tabViewModel!);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task CloseTab()
|
||||
{
|
||||
if (_appState.Tabs.Count < 2) return Task.CompletedTask;
|
||||
|
||||
_appState.RemoveTab(_selectedTab!);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using System.Reactive.Linq;
|
||||
using DynamicData;
|
||||
using FileTime.App.Core.ViewModels;
|
||||
using FileTime.Core.Models;
|
||||
|
||||
namespace FileTime.App.Core.Services.UserCommandHandler;
|
||||
|
||||
public abstract class UserCommandHandlerServiceBase : IUserCommandHandler
|
||||
{
|
||||
private readonly List<IUserCommandHandler> _userCommandHandlers = new();
|
||||
private readonly IAppState? _appState;
|
||||
|
||||
protected UserCommandHandlerServiceBase(IAppState? appState = null)
|
||||
{
|
||||
_appState = appState;
|
||||
}
|
||||
|
||||
public bool CanHandleCommand(UserCommand.IUserCommand command) => _userCommandHandlers.Any(h => h.CanHandleCommand(command));
|
||||
|
||||
public async Task HandleCommandAsync(UserCommand.IUserCommand command)
|
||||
{
|
||||
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(userCommandHandler);
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
protected IDisposable SaveCurrentLocation(Action<IContainer?> handler)
|
||||
=> 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>())));
|
||||
|
||||
protected IDisposable SaveMarkedItems(Action<IChangeSet<IAbsolutePath>> 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(UserCommandHandlerServiceBase)}.");
|
||||
|
||||
return act(_appState);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user