Select previous/next item
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
using FileTime.App.Core.ViewModels;
|
||||
using FileTime.Core.Models;
|
||||
|
||||
namespace FileTime.App.Core.Extensions
|
||||
{
|
||||
public static class ViewModelExtensions
|
||||
{
|
||||
public static IAbsolutePath ToAbsolutePath(this IItemViewModel itemViewModel)
|
||||
{
|
||||
var item = itemViewModel.BaseItem ?? throw new ArgumentException($"{nameof(itemViewModel)} does not have {nameof(IItemViewModel.BaseItem)}");
|
||||
return new AbsolutePath(item.Provider, item.FullName ?? throw new ArgumentException($"Parameter does not have {nameof(IItem.FullName)}"), item.Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Core\FileTime.Core.Abstraction\FileTime.Core.Abstraction.csproj" />
|
||||
<ProjectReference Include="..\..\Core\FileTime.Core.Models\FileTime.Core.Models.csproj" />
|
||||
<ProjectReference Include="..\FileTime.App.Core.Abstraction\FileTime.App.Core.Abstraction.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Reactive.Linq;
|
||||
using FileTime.App.Core.Command;
|
||||
using FileTime.App.Core.Extensions;
|
||||
using FileTime.App.Core.ViewModels;
|
||||
using FileTime.Core.Models;
|
||||
|
||||
@@ -12,14 +13,16 @@ namespace FileTime.App.Core.Services
|
||||
private ITabViewModel? _selectedTab;
|
||||
private IContainer? _currentLocation;
|
||||
private IItemViewModel? _currentSelectedItem;
|
||||
private List<IItemViewModel> _currentItems = new();
|
||||
|
||||
public CommandHandlerService(IAppState 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 == 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 == null ? Observable.Return(Enumerable.Empty<IItemViewModel>()) : t.CurrentItems).Switch().Subscribe(i => _currentItems = i.ToList());
|
||||
|
||||
_commandHandlers = new Dictionary<Commands, Func<Task>>
|
||||
{
|
||||
@@ -44,9 +47,9 @@ namespace FileTime.App.Core.Services
|
||||
{Commands.GoUp, GoUp},
|
||||
//{Commands.HardDelete, HardDelete},
|
||||
//{Commands.Mark, MarkCurrentItem},
|
||||
//{Commands.MoveCursorDown, MoveCursorDown},
|
||||
{Commands.MoveCursorDown, MoveCursorDown},
|
||||
//{Commands.MoveCursorDownPage, MoveCursorDownPage},
|
||||
//{Commands.MoveCursorUp, MoveCursorUp},
|
||||
{Commands.MoveCursorUp, MoveCursorUp},
|
||||
//{Commands.MoveCursorUpPage, MoveCursorUpPage},
|
||||
//{Commands.MoveToFirst, MoveToFirst},
|
||||
//{Commands.MoveToLast, MoveToLast},
|
||||
@@ -100,5 +103,27 @@ namespace FileTime.App.Core.Services
|
||||
if (_currentLocation?.Parent is not IAbsolutePath parentPath || await parentPath.ResolveAsync() is not IContainer newContainer) return;
|
||||
_selectedTab?.Tab?.SetCurrentLocation(newContainer);
|
||||
}
|
||||
|
||||
private Task MoveCursorDown()
|
||||
{
|
||||
SelectNewSelectedItem(i => i.SkipWhile(i => i != _currentSelectedItem).Skip(1).FirstOrDefault());
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task MoveCursorUp()
|
||||
{
|
||||
SelectNewSelectedItem(i => i.TakeWhile(i => i != _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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,9 +22,9 @@ namespace FileTime.App.Core.ViewModels
|
||||
|
||||
public IObservable<bool> IsSelected { get; }
|
||||
|
||||
public IObservable<IContainer?>? CurrentLocation { get; private set; }
|
||||
public IObservable<IItemViewModel?>? CurrentSelectedItem { get; private set; }
|
||||
public IObservable<IEnumerable<IItemViewModel>>? CurrentItems { get; private set; }
|
||||
public IObservable<IContainer?> CurrentLocation { get; private set; } = null!;
|
||||
public IObservable<IItemViewModel?> CurrentSelectedItem { get; private set; } = null!;
|
||||
public IObservable<IEnumerable<IItemViewModel>> CurrentItems { get; private set; } = null!;
|
||||
public IObservable<IEnumerable<FullName>> MarkedItems { get; }
|
||||
|
||||
public TabViewModel(
|
||||
|
||||
Reference in New Issue
Block a user