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

@@ -22,5 +22,9 @@ namespace FileTime.App.Core.ViewModels
BindedCollection<IItemViewModel>? SelectedsChildrenCollection { get; }
BindedCollection<IItemViewModel>? ParentsChildrenCollection { get; }
IObservable<IReadOnlyCollection<IItemViewModel>?> CurrentItemsCollectionObservable { get; }
void ClearMarkedItems();
void RemoveMarkedItem(FullName item);
void AddMarkedItem(FullName item);
void ToggleMarkedItem(FullName item);
}
}

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)
{

View File

@@ -21,7 +21,8 @@ namespace FileTime.App.Core
private static IServiceCollection AddCommandHandlers(this IServiceCollection serviceCollection)
{
return serviceCollection
.AddSingleton<ICommandHandler, NavigationCommandHandler>();
.AddSingleton<ICommandHandler, NavigationCommandHandler>()
.AddSingleton<ICommandHandler, ItemManipulationCommandHandler>();
}
}
}

View File

@@ -20,6 +20,7 @@ namespace FileTime.App.Core.ViewModels
private readonly IAppState _appState;
private readonly IRxSchedulerService _rxSchedulerService;
private readonly BehaviorSubject<IEnumerable<FullName>> _markedItems = new(Enumerable.Empty<FullName>());
private readonly List<FullName> _currentMarkedItems = new();
private readonly List<IDisposable> _disposables = new();
private bool disposed;
@@ -191,7 +192,37 @@ namespace FileTime.App.Core.ViewModels
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);

View File

@@ -44,8 +44,8 @@ namespace FileTime.GuiApp
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(MainConfiguration.Configuration)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Program.EnvironmentName}.json", true, true)
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile($"appsettings.{Program.EnvironmentName}.json", true)
.Build();
return serviceCollection