From 925de28bac667cb89a7035843c7de57f4f1cf89e Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Thu, 18 Jul 2024 21:45:15 +0100 Subject: [PATCH] Timer lock work done --- Wallet.Avalonia/App.axaml.cs | 29 ++++++---- ...tyService.cs => IWalletInactivityTimer.cs} | 2 +- Wallet/WalletConfiguration.cs | 2 + ...ityService.cs => WalletInactivityTimer.cs} | 54 +++++++++++++++---- 4 files changed, 65 insertions(+), 22 deletions(-) rename Wallet/{IWalletActivityService.cs => IWalletInactivityTimer.cs} (67%) rename Wallet/{WalletActivityService.cs => WalletInactivityTimer.cs} (59%) diff --git a/Wallet.Avalonia/App.axaml.cs b/Wallet.Avalonia/App.axaml.cs index 93a96a3..2d3bf0f 100644 --- a/Wallet.Avalonia/App.axaml.cs +++ b/Wallet.Avalonia/App.axaml.cs @@ -57,7 +57,8 @@ public partial class App : Application if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime) { - services.AddTemplate("MainWindow"); + services.AddTemplate("MainWindow"); } services.AddHandler(); @@ -65,15 +66,17 @@ public partial class App : Application { args.AddServices(services => { - services.AddTransient>>(provider => Comparer>.Create((x, z) => - StringComparer.CurrentCultureIgnoreCase.Compare(x.Value.Item2, z.Value.Item2))); + services.AddSingleton(); + + services.AddTransient>>(provider => Comparer>.Create((x, z) => + StringComparer.CurrentCultureIgnoreCase.Compare(x.Value.Name, z.Value.Name))); services.AddCache>(); services.AddTransient(_ => provider.GetServices>()); - services.AddInitializer(); + services.AddInitializer(); services.AddTransient(); @@ -106,7 +109,8 @@ public partial class App : Application services.TryAddSingleton, DecoratorService>(); services.TryAddSingleton, DecoratorService>(); - services.AddTransient(provider => provider.GetRequiredService>().Value!); + services.AddTransient(provider => + provider.GetRequiredService>().Value!); services.AddDbContextFactory(); @@ -148,24 +152,29 @@ public partial class App : Application services.AddTemplate(); services.AddTemplate(); - services.AddTemplate("ItemCategoryCollection"); + services.AddTemplate("ItemCategoryCollection"); services.AddTemplate(); services.AddHandler(); services.AddHandler(); - services.AddScoped>, DecoratorService>>(); + services.AddScoped>, + DecoratorService>>(); services.AddTemplate(); services.AddTemplate(); services.AddHandler(); - services.AddTemplate("EmptyItemCollection"); + services.AddTemplate("EmptyItemCollection"); - services.AddScoped, DecoratorService>(); - services.AddScoped, DecoratorService>(); + services.AddScoped, + DecoratorService>(); + services.AddScoped, + DecoratorService>(); services.AddTemplate("Item"); diff --git a/Wallet/IWalletActivityService.cs b/Wallet/IWalletInactivityTimer.cs similarity index 67% rename from Wallet/IWalletActivityService.cs rename to Wallet/IWalletInactivityTimer.cs index 61a2223..acc3f61 100644 --- a/Wallet/IWalletActivityService.cs +++ b/Wallet/IWalletInactivityTimer.cs @@ -2,6 +2,6 @@ namespace Wallet; -public interface IWalletActivityService : +public interface IWalletInactivityTimer : IInitialization, IDisposable; diff --git a/Wallet/WalletConfiguration.cs b/Wallet/WalletConfiguration.cs index 054ac4d..ba0d73b 100644 --- a/Wallet/WalletConfiguration.cs +++ b/Wallet/WalletConfiguration.cs @@ -6,4 +6,6 @@ public record WalletConfiguration : ComponentConfiguration { public string? Key { get; set; } + + public long? LockTimeout { get; set; } = 300000; } \ No newline at end of file diff --git a/Wallet/WalletActivityService.cs b/Wallet/WalletInactivityTimer.cs similarity index 59% rename from Wallet/WalletActivityService.cs rename to Wallet/WalletInactivityTimer.cs index 3393196..a1e6537 100644 --- a/Wallet/WalletActivityService.cs +++ b/Wallet/WalletInactivityTimer.cs @@ -3,28 +3,33 @@ using Timer = System.Threading.Timer; namespace Wallet; -public class WalletActivityService(ISubscriber subscriber, +public class WalletInactivityTimer(WalletConfiguration configuration, + IUserInteraction userInteraction, + ISubscriber subscriber, IPublisher publisher) : - IWalletActivityService, + IWalletInactivityTimer, INotificationHandler>, INotificationHandler>, INotificationHandler>, INotificationHandler> { private bool isOpen; - private readonly int timeout = 10000; private Timer? timer; + private readonly object timerLock = new(); - public void Dispose() + public void Initialize() { - timer?.Dispose(); + subscriber.Subscribe(this); + timer = new Timer(OnTimedEvent, null, Timeout.Infinite, Timeout.Infinite); + userInteraction.UserInteracted += OnUserInteracted; } public Task Handle(ActivatedEventArgs args) { if (isOpen) { - timer?.Change(Timeout.Infinite, Timeout.Infinite); + ResetTimer(); + userInteraction.Start(); } return Task.CompletedTask; @@ -34,7 +39,8 @@ public class WalletActivityService(ISubscriber subscriber, { if (isOpen) { - timer?.Change(timeout, Timeout.Infinite); + ResetTimer(); + userInteraction.Stop(); } return Task.CompletedTask; @@ -49,15 +55,27 @@ public class WalletActivityService(ISubscriber subscriber, public Task Handle(ClosedEventArgs args) { isOpen = false; - timer?.Change(Timeout.Infinite, Timeout.Infinite); + + StopTimer(); + userInteraction.Stop(); return Task.CompletedTask; } - public void Initialize() + private void ResetTimer() { - subscriber.Subscribe(this); - timer = new Timer(OnTimedEvent, null, Timeout.Infinite, Timeout.Infinite); + lock (timerLock) + { + timer?.Change(configuration.LockTimeout ?? 300000, Timeout.Infinite); + } + } + + private void StopTimer() + { + lock (timerLock) + { + timer?.Change(Timeout.Infinite, Timeout.Infinite); + } } private void OnTimedEvent(object? state) @@ -67,4 +85,18 @@ public class WalletActivityService(ISubscriber subscriber, publisher.PublishUI(new ClosedEventArgs()); } } + + private void OnUserInteracted(object? sender, UserInteractedEventArgs args) + { + if (isOpen) + { + ResetTimer(); + } + } + + public void Dispose() + { + userInteraction.UserInteracted -= OnUserInteracted; + timer?.Dispose(); + } }