Marked items

This commit is contained in:
2022-04-24 21:46:31 +02:00
parent 2eae6b6c60
commit 35c050c612
8 changed files with 110 additions and 16 deletions

View File

@@ -1,10 +1,20 @@
using System.Reactive.Linq;
using FileTime.App.Core.Command;
using FileTime.App.Core.ViewModels;
using FileTime.Core.Models;
namespace FileTime.App.Core.Services.CommandHandler
{
public abstract class CommandHanderBase : ICommandHandler
{
private readonly Dictionary<Commands, Func<Task>> _commandHandlers = new();
private readonly IAppState? _appState;
protected CommandHanderBase(IAppState? appState = null)
{
_appState = appState;
}
public bool CanHandleCommand(Commands command) => _commandHandlers.ContainsKey(command);
public async Task HandleCommandAsync(Commands command) => await _commandHandlers[command].Invoke();
@@ -19,5 +29,22 @@ namespace FileTime.App.Core.Services.CommandHandler
}
protected void RemoveCommandHandler(Commands command) => _commandHandlers.Remove(command);
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>())));
private IDisposable RunWithAppState(Func<IAppState, IDisposable> act)
{
if (_appState == null) throw new NullReferenceException($"AppState is nit initialized in {nameof(CommandHanderBase)}.");
return act(_appState);
}
}
}

View File

@@ -0,0 +1,33 @@
using FileTime.App.Core.Command;
using FileTime.App.Core.ViewModels;
namespace FileTime.App.Core.Services.CommandHandler
{
public class ItemManipulationCommandHandler : CommandHanderBase
{
private ITabViewModel? _selectedTab;
private IItemViewModel? _currentSelectedItem;
private readonly ICommandHandlerService _commandHandlerService;
public ItemManipulationCommandHandler(IAppState appState, ICommandHandlerService commandHandlerService) : base(appState)
{
_commandHandlerService = commandHandlerService;
SaveSelectedTab(t => _selectedTab = t);
SaveCurrentSelectedItem(i => _currentSelectedItem = i);
AddCommandHandlers(new (Commands, Func<Task>)[]
{
(Commands.Mark, MarkItem)
});
}
private async Task MarkItem()
{
if (_selectedTab == null || _currentSelectedItem?.BaseItem?.FullName == null) return;
_selectedTab.ToggleMarkedItem(_currentSelectedItem.BaseItem.FullName);
await _commandHandlerService.HandleCommandAsync(Commands.MoveCursorDown);
}
}
}

View File

@@ -8,20 +8,17 @@ namespace FileTime.App.Core.Services.CommandHandler
{
public class NavigationCommandHandler : CommandHanderBase
{
private readonly IAppState _appState;
private ITabViewModel? _selectedTab;
private IContainer? _currentLocation;
private IItemViewModel? _currentSelectedItem;
private IEnumerable<IItemViewModel> _currentItems = Enumerable.Empty<IItemViewModel>();
public NavigationCommandHandler(IAppState appState)
public NavigationCommandHandler(IAppState appState) : base(appState)
{
_appState = appState;
_appState.SelectedTab.Subscribe(t => _selectedTab = t);
_appState.SelectedTab.Select(t => t == null ? Observable.Return<IContainer?>(null) : t.CurrentLocation).Switch().Subscribe(l => _currentLocation = l);
_appState.SelectedTab.Select(t => t == null ? Observable.Return<IItemViewModel?>(null) : t.CurrentSelectedItem).Switch().Subscribe(l => _currentSelectedItem = l);
_appState.SelectedTab.Select(t => t?.CurrentItemsCollectionObservable ?? Observable.Return((IEnumerable<IItemViewModel>?)Enumerable.Empty<IItemViewModel>())).Switch().Subscribe(i => _currentItems = i ?? Enumerable.Empty<IItemViewModel>());
SaveSelectedTab(t => _selectedTab = t);
SaveCurrentSelectedItem(i => _currentSelectedItem = i);
SaveCurrentLocation(l => _currentLocation = l);
SaveCurrentItems(i => _currentItems = i);
AddCommandHandlers(new (Commands, Func<Task>)[]
{

View File

@@ -1,14 +1,15 @@
using FileTime.App.Core.Command;
using Microsoft.Extensions.DependencyInjection;
namespace FileTime.App.Core.Services
{
public class CommandHandlerService : ICommandHandlerService
{
private readonly IEnumerable<ICommandHandler> _commandHandlers;
private readonly Lazy<IEnumerable<ICommandHandler>> _commandHandlers;
public CommandHandlerService(IEnumerable<ICommandHandler> commandHandlers)
public CommandHandlerService(IServiceProvider serviceProvider)
{
_commandHandlers = commandHandlers;
_commandHandlers = new Lazy<IEnumerable<ICommandHandler>>(() => serviceProvider.GetServices<ICommandHandler>());
//(Commands.AutoRefresh, ToggleAutoRefresh),
//(Commands.ChangeTimelineMode, ChangeTimelineMode),
@@ -68,7 +69,7 @@ namespace FileTime.App.Core.Services
public async Task HandleCommandAsync(Commands command)
{
var handler = _commandHandlers.FirstOrDefault(h => h.CanHandleCommand(command));
var handler = _commandHandlers.Value.FirstOrDefault(h => h.CanHandleCommand(command));
if (handler != null)
{