Marked items
This commit is contained in:
@@ -22,5 +22,9 @@ namespace FileTime.App.Core.ViewModels
|
|||||||
BindedCollection<IItemViewModel>? SelectedsChildrenCollection { get; }
|
BindedCollection<IItemViewModel>? SelectedsChildrenCollection { get; }
|
||||||
BindedCollection<IItemViewModel>? ParentsChildrenCollection { get; }
|
BindedCollection<IItemViewModel>? ParentsChildrenCollection { get; }
|
||||||
IObservable<IReadOnlyCollection<IItemViewModel>?> CurrentItemsCollectionObservable { get; }
|
IObservable<IReadOnlyCollection<IItemViewModel>?> CurrentItemsCollectionObservable { get; }
|
||||||
|
void ClearMarkedItems();
|
||||||
|
void RemoveMarkedItem(FullName item);
|
||||||
|
void AddMarkedItem(FullName item);
|
||||||
|
void ToggleMarkedItem(FullName item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,20 @@
|
|||||||
|
using System.Reactive.Linq;
|
||||||
using FileTime.App.Core.Command;
|
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.CommandHandler
|
||||||
{
|
{
|
||||||
public abstract class CommandHanderBase : ICommandHandler
|
public abstract class CommandHanderBase : ICommandHandler
|
||||||
{
|
{
|
||||||
private readonly Dictionary<Commands, Func<Task>> _commandHandlers = new();
|
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 bool CanHandleCommand(Commands command) => _commandHandlers.ContainsKey(command);
|
||||||
|
|
||||||
public async Task HandleCommandAsync(Commands command) => await _commandHandlers[command].Invoke();
|
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 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,20 +8,17 @@ namespace FileTime.App.Core.Services.CommandHandler
|
|||||||
{
|
{
|
||||||
public class NavigationCommandHandler : CommandHanderBase
|
public class NavigationCommandHandler : CommandHanderBase
|
||||||
{
|
{
|
||||||
private readonly IAppState _appState;
|
|
||||||
private ITabViewModel? _selectedTab;
|
private ITabViewModel? _selectedTab;
|
||||||
private IContainer? _currentLocation;
|
private IContainer? _currentLocation;
|
||||||
private IItemViewModel? _currentSelectedItem;
|
private IItemViewModel? _currentSelectedItem;
|
||||||
private IEnumerable<IItemViewModel> _currentItems = Enumerable.Empty<IItemViewModel>();
|
private IEnumerable<IItemViewModel> _currentItems = Enumerable.Empty<IItemViewModel>();
|
||||||
|
|
||||||
public NavigationCommandHandler(IAppState appState)
|
public NavigationCommandHandler(IAppState appState) : base(appState)
|
||||||
{
|
{
|
||||||
_appState = appState;
|
SaveSelectedTab(t => _selectedTab = t);
|
||||||
|
SaveCurrentSelectedItem(i => _currentSelectedItem = i);
|
||||||
_appState.SelectedTab.Subscribe(t => _selectedTab = t);
|
SaveCurrentLocation(l => _currentLocation = l);
|
||||||
_appState.SelectedTab.Select(t => t == null ? Observable.Return<IContainer?>(null) : t.CurrentLocation).Switch().Subscribe(l => _currentLocation = l);
|
SaveCurrentItems(i => _currentItems = i);
|
||||||
_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>());
|
|
||||||
|
|
||||||
AddCommandHandlers(new (Commands, Func<Task>)[]
|
AddCommandHandlers(new (Commands, Func<Task>)[]
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
using FileTime.App.Core.Command;
|
using FileTime.App.Core.Command;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace FileTime.App.Core.Services
|
namespace FileTime.App.Core.Services
|
||||||
{
|
{
|
||||||
public class CommandHandlerService : ICommandHandlerService
|
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.AutoRefresh, ToggleAutoRefresh),
|
||||||
//(Commands.ChangeTimelineMode, ChangeTimelineMode),
|
//(Commands.ChangeTimelineMode, ChangeTimelineMode),
|
||||||
@@ -68,7 +69,7 @@ namespace FileTime.App.Core.Services
|
|||||||
|
|
||||||
public async Task HandleCommandAsync(Commands command)
|
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)
|
if (handler != null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -21,7 +21,8 @@ namespace FileTime.App.Core
|
|||||||
private static IServiceCollection AddCommandHandlers(this IServiceCollection serviceCollection)
|
private static IServiceCollection AddCommandHandlers(this IServiceCollection serviceCollection)
|
||||||
{
|
{
|
||||||
return serviceCollection
|
return serviceCollection
|
||||||
.AddSingleton<ICommandHandler, NavigationCommandHandler>();
|
.AddSingleton<ICommandHandler, NavigationCommandHandler>()
|
||||||
|
.AddSingleton<ICommandHandler, ItemManipulationCommandHandler>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -20,6 +20,7 @@ namespace FileTime.App.Core.ViewModels
|
|||||||
private readonly IAppState _appState;
|
private readonly IAppState _appState;
|
||||||
private readonly IRxSchedulerService _rxSchedulerService;
|
private readonly IRxSchedulerService _rxSchedulerService;
|
||||||
private readonly BehaviorSubject<IEnumerable<FullName>> _markedItems = new(Enumerable.Empty<FullName>());
|
private readonly BehaviorSubject<IEnumerable<FullName>> _markedItems = new(Enumerable.Empty<FullName>());
|
||||||
|
private readonly List<FullName> _currentMarkedItems = new();
|
||||||
private readonly List<IDisposable> _disposables = new();
|
private readonly List<IDisposable> _disposables = new();
|
||||||
private bool disposed;
|
private bool disposed;
|
||||||
|
|
||||||
@@ -191,7 +192,37 @@ namespace FileTime.App.Core.ViewModels
|
|||||||
return elementViewModel;
|
return elementViewModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new ArgumentException($"{nameof(item)} is not {nameof(IContainer)} neighter {nameof(IElement)}");
|
throw new ArgumentException($"{nameof(item)} is not {nameof(IContainer)} neither {nameof(IElement)}");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddMarkedItem(FullName item)
|
||||||
|
{
|
||||||
|
_currentMarkedItems.Add(item);
|
||||||
|
_markedItems.OnNext(_currentMarkedItems);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveMarkedItem(FullName item)
|
||||||
|
{
|
||||||
|
_currentMarkedItems.RemoveAll(i => i.Path == item.Path);
|
||||||
|
_markedItems.OnNext(_currentMarkedItems);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ToggleMarkedItem(FullName item)
|
||||||
|
{
|
||||||
|
if (_currentMarkedItems.Any(i => i.Path == item.Path))
|
||||||
|
{
|
||||||
|
RemoveMarkedItem(item);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
AddMarkedItem(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClearMarkedItems()
|
||||||
|
{
|
||||||
|
_currentMarkedItems.Clear();
|
||||||
|
_markedItems.OnNext(_currentMarkedItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
~TabViewModel() => Dispose(false);
|
~TabViewModel() => Dispose(false);
|
||||||
|
|||||||
@@ -44,8 +44,8 @@ namespace FileTime.GuiApp
|
|||||||
{
|
{
|
||||||
var configuration = new ConfigurationBuilder()
|
var configuration = new ConfigurationBuilder()
|
||||||
.AddInMemoryCollection(MainConfiguration.Configuration)
|
.AddInMemoryCollection(MainConfiguration.Configuration)
|
||||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
.AddJsonFile("appsettings.json", optional: true)
|
||||||
.AddJsonFile($"appsettings.{Program.EnvironmentName}.json", true, true)
|
.AddJsonFile($"appsettings.{Program.EnvironmentName}.json", true)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
return serviceCollection
|
return serviceCollection
|
||||||
|
|||||||
Reference in New Issue
Block a user