From 410c919e2e9d59fc4f26129a7cb57431f722cda9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Sat, 28 Jan 2023 15:50:09 +0100 Subject: [PATCH] Handle refresh request --- .../Services/ContainerRefreshHandler.cs | 50 +++++++++++++++++++ src/AppCommon/FileTime.App.Core/Startup.cs | 3 +- .../Copy/CopyStrategy.cs | 2 + .../CommandScheduler.cs | 4 +- .../Services/LifecycleService.cs | 15 +++++- 5 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 src/AppCommon/FileTime.App.Core/Services/ContainerRefreshHandler.cs diff --git a/src/AppCommon/FileTime.App.Core/Services/ContainerRefreshHandler.cs b/src/AppCommon/FileTime.App.Core/Services/ContainerRefreshHandler.cs new file mode 100644 index 0000000..a93d44e --- /dev/null +++ b/src/AppCommon/FileTime.App.Core/Services/ContainerRefreshHandler.cs @@ -0,0 +1,50 @@ +using System.Reactive.Linq; +using FileTime.App.Core.UserCommand; +using FileTime.App.Core.ViewModels; +using FileTime.Core.Models; +using FileTime.Core.Timeline; + +namespace FileTime.App.Core.Services; + +public class ContainerRefreshHandler : IStartupHandler, IDisposable +{ + private readonly List _refreshSubscriptions = new(); + private List _folders = new(); + + public ContainerRefreshHandler( + ICommandScheduler commandScheduler, + IUserCommandHandlerService userCommandHandlerService, + IAppState appState + ) + { + _refreshSubscriptions.Add( + Observable.CombineLatest( + appState.SelectedTab.Select(t => t?.CurrentLocation.Select(l => l?.FullName) ?? Observable.Never()).Switch(), + appState.SelectedTab.Select(t => t?.CurrentLocation.Select(l => l?.Parent?.Path) ?? Observable.Never()).Switch(), + appState.SelectedTab.Select(t => t?.CurrentSelectedItem.Select(l => l?.BaseItem?.FullName) ?? Observable.Never()).Switch(), + (a, b, c) => new[] {a, b, c} + ).Subscribe(folders => { _folders = folders.Where(f => f is not null).Cast().ToList(); }) + ); + + + _refreshSubscriptions.Add( + commandScheduler.ContainerToRefresh.Subscribe(refreshRequestedIn => + { + if (_folders.Contains(refreshRequestedIn)) + { + userCommandHandlerService.HandleCommandAsync(RefreshCommand.Instance); + } + }) + ); + } + + public void Dispose() + { + foreach (var refreshSubscription in _refreshSubscriptions) + { + refreshSubscription.Dispose(); + } + } + + public Task InitAsync() => Task.CompletedTask; +} \ No newline at end of file diff --git a/src/AppCommon/FileTime.App.Core/Startup.cs b/src/AppCommon/FileTime.App.Core/Startup.cs index a87aca9..7be65af 100644 --- a/src/AppCommon/FileTime.App.Core/Startup.cs +++ b/src/AppCommon/FileTime.App.Core/Startup.cs @@ -26,7 +26,8 @@ public static class Startup return serviceCollection .AddCommandHandlers() - .AddSingleton(); + .AddSingleton() + .AddSingleton(); } private static IServiceCollection AddCommandHandlers(this IServiceCollection serviceCollection) diff --git a/src/Core/FileTime.Core.Command/Copy/CopyStrategy.cs b/src/Core/FileTime.Core.Command/Copy/CopyStrategy.cs index 7f3efa9..101b2a4 100644 --- a/src/Core/FileTime.Core.Command/Copy/CopyStrategy.cs +++ b/src/Core/FileTime.Core.Command/Copy/CopyStrategy.cs @@ -28,6 +28,8 @@ public class CopyStrategy : ICopyStrategy { await _copy(from, to, context); context.CurrentProgress?.SetProgress(context.CurrentProgress.TotalCount); + if (to.Path.GetParent() is { } parent) + await _copyStrategyParam.RefreshContainerAsync(parent); } public Task CreateContainerAsync(IContainer target, string name, PointInTime currentTime) diff --git a/src/Core/FileTime.Core.Timeline/CommandScheduler.cs b/src/Core/FileTime.Core.Timeline/CommandScheduler.cs index c21f5ff..bb1bc94 100644 --- a/src/Core/FileTime.Core.Timeline/CommandScheduler.cs +++ b/src/Core/FileTime.Core.Timeline/CommandScheduler.cs @@ -22,7 +22,7 @@ public class CommandScheduler : ICommandScheduler { get { - bool result = true; + var result = true; RunWithLock(() => result = _enableRunning); return result; } @@ -33,7 +33,7 @@ public class CommandScheduler : ICommandScheduler public CommandScheduler(ILocalCommandExecutor localExecutor) { ContainerToRefresh = _containerToRefresh.AsObservable(); - + localExecutor.CommandFinished += LocalExecutorOnCommandFinished; _commandExecutors.Add(localExecutor); } diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp/Services/LifecycleService.cs b/src/GuiApp/Avalonia/FileTime.GuiApp/Services/LifecycleService.cs index 341cf86..5997611 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp/Services/LifecycleService.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp/Services/LifecycleService.cs @@ -29,7 +29,7 @@ public class LifecycleService } catch (Exception ex) { - _logger.LogError(ex, "Error while running startup handler {handler}", startupHandler?.GetType().FullName); + _logger.LogError(ex, "Error while running startup handler {Handler}", startupHandler?.GetType().FullName); } } } @@ -44,8 +44,19 @@ public class LifecycleService } catch (Exception ex) { - _logger.LogError(ex, "Error while running exit handler {handler}", exitHandler?.GetType().FullName); + _logger.LogError(ex, "Error while running exit handler {Handler}", exitHandler?.GetType().FullName); } } + + foreach (var disposable in + _startupHandlers + .OfType() + .Concat( + _exitHandlers.OfType() + ) + ) + { + disposable.Dispose(); + } } } \ No newline at end of file