Error handling, better path remembrance

This commit is contained in:
2022-05-24 19:16:37 +02:00
parent c08e345c2f
commit 518cc350ad
5 changed files with 64 additions and 31 deletions

View File

@@ -4,6 +4,7 @@ using DynamicData;
using FileTime.App.Core.Models;
using FileTime.App.Core.Models.Enums;
using FileTime.App.Core.Services;
using FileTime.Core.Helper;
using FileTime.Core.Models;
using MoreLinq;
using MvvmGen;
@@ -15,6 +16,8 @@ namespace FileTime.App.Core.ViewModels;
[Inject(typeof(IItemNameConverterService), "_itemNameConverterService")]
public abstract partial class ItemViewModel : IItemViewModel
{
private ITabViewModel? _parentTab;
[Property]
private IItem? _baseItem;
@@ -44,6 +47,8 @@ public abstract partial class ItemViewModel : IItemViewModel
public void Init(IItem item, ITabViewModel parentTab, ItemViewModelType itemViewModelType)
{
_parentTab = parentTab;
var sourceCollection = itemViewModelType switch
{
ItemViewModelType.Main => parentTab.CurrentItemsCollectionObservable,
@@ -62,7 +67,7 @@ public abstract partial class ItemViewModel : IItemViewModel
IsSelected = itemViewModelType is ItemViewModelType.Main
? parentTab.CurrentSelectedItem.Select(EqualsTo)
: Observable.Return(false);
: Observable.Return(IsInDeespestPath());
IsAlternative = sourceCollection.Select(c => c?.Index().FirstOrDefault(i => EqualsTo(i.Value)).Key % 2 == 0);
@@ -86,4 +91,19 @@ public abstract partial class ItemViewModel : IItemViewModel
{
return BaseItem?.FullName?.Path is string path && path == itemViewModel?.BaseItem?.FullName?.Path;
}
private bool IsInDeespestPath()
{
if (_parentTab?.Tab?.LastDeepestSelectedPath is null
|| BaseItem?.FullName is null)
{
return false;
}
var ownFullName = BaseItem.FullName;
var deepestPath = _parentTab.Tab.LastDeepestSelectedPath;
var commonPath = new FullName(PathHelper.GetCommonPath(ownFullName.Path, deepestPath.Path));
return commonPath.Path == ownFullName.Path;
}
}

View File

@@ -9,6 +9,7 @@ public interface ITab : IInitable<IContainer>, IDisposable
IObservable<IContainer?> CurrentLocation { get; }
IObservable<AbsolutePath?> CurrentSelectedItem { get; }
IObservable<IObservable<IChangeSet<IItem>>?> CurrentItems { get; }
FullName? LastDeepestSelectedPath { get; }
void SetCurrentLocation(IContainer newLocation);
void AddItemFilter(ItemFilter filter);

View File

@@ -15,13 +15,13 @@ public class Tab : ITab
private readonly BehaviorSubject<IContainer?> _currentLocationForced = new(null);
private readonly BehaviorSubject<AbsolutePath?> _currentSelectedItem = new(null);
private readonly SourceList<ItemFilter> _itemFilters = new();
private FullName? _lastDeepestSelected;
private AbsolutePath? _currentSelectedItemCached;
private PointInTime _currentPointInTime;
public IObservable<IContainer?> CurrentLocation { get; }
public IObservable<IObservable<IChangeSet<IItem>>?> CurrentItems { get; }
public IObservable<AbsolutePath?> CurrentSelectedItem { get; }
public FullName? LastDeepestSelectedPath { get; private set; }
public Tab(ITimelessContentProvider timelessContentProvider)
{
@@ -33,6 +33,13 @@ public class Tab : ITab
CurrentLocation = _currentLocation
.DistinctUntilChanged()
.Merge(_currentLocationForced)
.Do(_ =>
{
if (_currentSelectedItemCached is not null)
{
LastDeepestSelectedPath = new FullName(PathHelper.GetLongerPath(LastDeepestSelectedPath?.Path, _currentSelectedItemCached.Path.Path));
}
})
.Publish(null)
.RefCount();
@@ -79,11 +86,6 @@ public class Tab : ITab
{
_currentSelectedItemCached = s;
_currentSelectedItem.OnNext(s);
if (s is not null)
{
_lastDeepestSelected = new FullName(PathHelper.GetLongerPath(_lastDeepestSelected?.Path, s.Path.Path));
}
});
}
@@ -99,13 +101,13 @@ public class Tab : ITab
if (!items.Any()) return null;
var newSelectedItem = new AbsolutePath(_timelessContentProvider, items.First());
if (_lastDeepestSelected is not null)
if (LastDeepestSelectedPath is not null)
{
var parentPath = items.First().FullName?.GetParent()?.Path;
if (parentPath is not null && _lastDeepestSelected.Path.StartsWith(parentPath))
if (parentPath is not null && LastDeepestSelectedPath.Path.StartsWith(parentPath))
{
var itemNameToSelect = _lastDeepestSelected.Path
var itemNameToSelect = LastDeepestSelectedPath.Path
.Split(Constants.SeparatorChar)
.Skip((parentPath?.Split(Constants.SeparatorChar).Length) ?? 0)
.FirstOrDefault();
@@ -119,7 +121,7 @@ public class Tab : ITab
}
}
_lastDeepestSelected = new FullName(PathHelper.GetLongerPath(_lastDeepestSelected?.Path, newSelectedItem.Path.Path));
LastDeepestSelectedPath = new FullName(PathHelper.GetLongerPath(LastDeepestSelectedPath?.Path, newSelectedItem.Path.Path));
return newSelectedItem;
}

View File

@@ -1,4 +1,5 @@
using FileTime.App.Core.Services;
using Microsoft.Extensions.Logging;
namespace FileTime.GuiApp.Services;
@@ -6,18 +7,30 @@ public class LifecycleService
{
private readonly IEnumerable<IExitHandler> _exitHandlers;
private readonly IEnumerable<IStartupHandler> _startupHandlers;
private readonly ILogger<LifecycleService> _logger;
public LifecycleService(IEnumerable<IStartupHandler> startupHandlers, IEnumerable<IExitHandler> exitHandlers)
public LifecycleService(
IEnumerable<IStartupHandler> startupHandlers,
IEnumerable<IExitHandler> exitHandlers,
ILogger<LifecycleService> logger)
{
_exitHandlers = exitHandlers;
_startupHandlers = startupHandlers;
_logger = logger;
}
public async Task InitStartupHandlersAsync()
{
foreach (var startupHandler in _startupHandlers)
{
await startupHandler.InitAsync();
try
{
await startupHandler.InitAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while running startup handler {handler}", startupHandler?.GetType().FullName);
}
}
}
@@ -25,7 +38,14 @@ public class LifecycleService
{
foreach (var exitHandler in _exitHandlers)
{
await exitHandler.ExitAsync();
try
{
await exitHandler.ExitAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while running exit handler {handler}", exitHandler?.GetType().FullName);
}
}
}
}

View File

@@ -49,17 +49,7 @@ public partial class MainWindowViewModel : IMainWindowViewModelBase
Title = "FileTime " + versionString;
//TODO: refactor
/*if (AppState.Tabs.Count == 0)
{
var tab = _serviceProvider.GetInitableResolver<IContainer>(_localContentProvider)
.GetRequiredService<ITab>();
var tabViewModel = _serviceProvider.GetInitableResolver(tab, 1).GetRequiredService<ITabViewModel>();
_appState.AddTab(tabViewModel);
}*/
_lifecycleService.InitStartupHandlersAsync().Wait();
Task.Run(async () => await _lifecycleService.InitStartupHandlersAsync()).Wait();
}
public void ProcessKeyDown(Key key, KeyModifiers keyModifiers, Action<bool> setHandled)