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;
using FileTime.App.Core.Models.Enums; using FileTime.App.Core.Models.Enums;
using FileTime.App.Core.Services; using FileTime.App.Core.Services;
using FileTime.Core.Helper;
using FileTime.Core.Models; using FileTime.Core.Models;
using MoreLinq; using MoreLinq;
using MvvmGen; using MvvmGen;
@@ -15,6 +16,8 @@ namespace FileTime.App.Core.ViewModels;
[Inject(typeof(IItemNameConverterService), "_itemNameConverterService")] [Inject(typeof(IItemNameConverterService), "_itemNameConverterService")]
public abstract partial class ItemViewModel : IItemViewModel public abstract partial class ItemViewModel : IItemViewModel
{ {
private ITabViewModel? _parentTab;
[Property] [Property]
private IItem? _baseItem; private IItem? _baseItem;
@@ -44,6 +47,8 @@ public abstract partial class ItemViewModel : IItemViewModel
public void Init(IItem item, ITabViewModel parentTab, ItemViewModelType itemViewModelType) public void Init(IItem item, ITabViewModel parentTab, ItemViewModelType itemViewModelType)
{ {
_parentTab = parentTab;
var sourceCollection = itemViewModelType switch var sourceCollection = itemViewModelType switch
{ {
ItemViewModelType.Main => parentTab.CurrentItemsCollectionObservable, ItemViewModelType.Main => parentTab.CurrentItemsCollectionObservable,
@@ -51,21 +56,21 @@ public abstract partial class ItemViewModel : IItemViewModel
ItemViewModelType.SelectedChild => parentTab.SelectedsChildrenCollectionObservable, ItemViewModelType.SelectedChild => parentTab.SelectedsChildrenCollectionObservable,
_ => throw new InvalidEnumArgumentException() _ => throw new InvalidEnumArgumentException()
}; };
BaseItem = item; BaseItem = item;
DisplayName = _appState.SearchText.Select(s => _itemNameConverterService.GetDisplayName(item.DisplayName, s)); DisplayName = _appState.SearchText.Select(s => _itemNameConverterService.GetDisplayName(item.DisplayName, s));
DisplayNameText = item.DisplayName; DisplayNameText = item.DisplayName;
IsMarked = itemViewModelType is ItemViewModelType.Main IsMarked = itemViewModelType is ItemViewModelType.Main
? parentTab.MarkedItems.ToCollection().Select(m => m.Any(i => i.Path == item.FullName?.Path)) ? parentTab.MarkedItems.ToCollection().Select(m => m.Any(i => i.Path == item.FullName?.Path))
: Observable.Return(false); : Observable.Return(false);
IsSelected = itemViewModelType is ItemViewModelType.Main IsSelected = itemViewModelType is ItemViewModelType.Main
? parentTab.CurrentSelectedItem.Select(EqualsTo) ? parentTab.CurrentSelectedItem.Select(EqualsTo)
: Observable.Return(false); : Observable.Return(IsInDeespestPath());
IsAlternative = sourceCollection.Select(c => c?.Index().FirstOrDefault(i => EqualsTo(i.Value)).Key % 2 == 0); IsAlternative = sourceCollection.Select(c => c?.Index().FirstOrDefault(i => EqualsTo(i.Value)).Key % 2 == 0);
ViewMode = Observable.CombineLatest(IsMarked, IsSelected, IsAlternative, GenerateViewMode).Throttle(TimeSpan.FromMilliseconds(10)); ViewMode = Observable.CombineLatest(IsMarked, IsSelected, IsAlternative, GenerateViewMode).Throttle(TimeSpan.FromMilliseconds(10));
Attributes = item.Attributes; Attributes = item.Attributes;
CreatedAt = item.CreatedAt; CreatedAt = item.CreatedAt;
@@ -86,4 +91,19 @@ public abstract partial class ItemViewModel : IItemViewModel
{ {
return BaseItem?.FullName?.Path is string path && path == itemViewModel?.BaseItem?.FullName?.Path; 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<IContainer?> CurrentLocation { get; }
IObservable<AbsolutePath?> CurrentSelectedItem { get; } IObservable<AbsolutePath?> CurrentSelectedItem { get; }
IObservable<IObservable<IChangeSet<IItem>>?> CurrentItems { get; } IObservable<IObservable<IChangeSet<IItem>>?> CurrentItems { get; }
FullName? LastDeepestSelectedPath { get; }
void SetCurrentLocation(IContainer newLocation); void SetCurrentLocation(IContainer newLocation);
void AddItemFilter(ItemFilter filter); void AddItemFilter(ItemFilter filter);

View File

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

View File

@@ -1,4 +1,5 @@
using FileTime.App.Core.Services; using FileTime.App.Core.Services;
using Microsoft.Extensions.Logging;
namespace FileTime.GuiApp.Services; namespace FileTime.GuiApp.Services;
@@ -6,18 +7,30 @@ public class LifecycleService
{ {
private readonly IEnumerable<IExitHandler> _exitHandlers; private readonly IEnumerable<IExitHandler> _exitHandlers;
private readonly IEnumerable<IStartupHandler> _startupHandlers; 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; _exitHandlers = exitHandlers;
_startupHandlers = startupHandlers; _startupHandlers = startupHandlers;
_logger = logger;
} }
public async Task InitStartupHandlersAsync() public async Task InitStartupHandlersAsync()
{ {
foreach (var startupHandler in _startupHandlers) 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) 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; Title = "FileTime " + versionString;
//TODO: refactor Task.Run(async () => await _lifecycleService.InitStartupHandlersAsync()).Wait();
/*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();
} }
public void ProcessKeyDown(Key key, KeyModifiers keyModifiers, Action<bool> setHandled) public void ProcessKeyDown(Key key, KeyModifiers keyModifiers, Action<bool> setHandled)