Timer lock work done

This commit is contained in:
TheXamlGuy
2024-07-18 21:45:15 +01:00
parent c7d4daca94
commit 925de28bac
4 changed files with 65 additions and 22 deletions
+19 -10
View File
@@ -57,7 +57,8 @@ public partial class App : Application
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime)
{
services.AddTemplate<MainWindowViewModel, MainWindow>("MainWindow");
services.AddTemplate<MainWindowViewModel,
MainWindow>("MainWindow");
}
services.AddHandler<WalletActivatedHandler>();
@@ -65,15 +66,17 @@ public partial class App : Application
{
args.AddServices(services =>
{
services.AddTransient<IComparer<Item<(Guid, string)>>>(provider => Comparer<Item<(Guid, string)>>.Create((x, z) =>
StringComparer.CurrentCultureIgnoreCase.Compare(x.Value.Item2, z.Value.Item2)));
services.AddSingleton<IUserInteraction, UserInteraction>();
services.AddTransient<IComparer<Item<(Guid, string)>>>(provider => Comparer<Item<(Guid Id, string Name)>>.Create((x, z) =>
StringComparer.CurrentCultureIgnoreCase.Compare(x.Value.Name, z.Value.Name)));
services.AddCache<Item<(Guid, string)>>();
services.AddTransient(_ =>
provider.GetServices<IConfigurationDescriptor<ItemConfiguration>>());
services.AddInitializer<WalletActivityService>();
services.AddInitializer<WalletInactivityTimer>();
services.AddTransient<IWalletFactory, WalletFactory>();
@@ -106,7 +109,8 @@ public partial class App : Application
services.TryAddSingleton<IDecoratorService<SecurityKey>, DecoratorService<SecurityKey>>();
services.TryAddSingleton<IDecoratorService<WalletConnection>, DecoratorService<WalletConnection>>();
services.AddTransient<IConnection>(provider => provider.GetRequiredService<IDecoratorService<WalletConnection>>().Value!);
services.AddTransient<IConnection>(provider =>
provider.GetRequiredService<IDecoratorService<WalletConnection>>().Value!);
services.AddDbContextFactory<WalletContext>();
@@ -148,24 +152,29 @@ public partial class App : Application
services.AddTemplate<BackActionViewModel, BackActionView>();
services.AddTemplate<SearchWalletActionViewModel, SearchWalletActionView>();
services.AddTemplate<ItemCategoryNavigationCollectionViewModel, ItemCategoryNavigationCollectionView>("ItemCategoryCollection");
services.AddTemplate<ItemCategoryNavigationCollectionViewModel,
ItemCategoryNavigationCollectionView>("ItemCategoryCollection");
services.AddTemplate<ItemCategoryNavigationViewModel, ItemCategoryNavigationView>();
services.AddHandler<CategoriesNavigationViewModelActivationHandler>();
services.AddHandler<ItemCategoryViewModelActivatedHandler>();
services.AddScoped<IDecoratorService<Item<(Guid, string)>>, DecoratorService<Item<(Guid, string)>>>();
services.AddScoped<IDecoratorService<Item<(Guid, string)>>,
DecoratorService<Item<(Guid, string)>>>();
services.AddTemplate<AddItemNavigationViewModel, AddItemNavigationView>();
services.AddTemplate<ItemNavigationViewModel, ItemNavigationView>();
services.AddHandler<ItemNavigationViewModelActivatedHandler>();
services.AddTemplate<EmptyItemCollectionViewModel, EmptyItemCollectionView>("EmptyItemCollection");
services.AddTemplate<EmptyItemCollectionViewModel,
EmptyItemCollectionView>("EmptyItemCollection");
services.AddScoped<IDecoratorService<ItemHeaderConfiguration>, DecoratorService<ItemHeaderConfiguration>>();
services.AddScoped<IDecoratorService<ItemConfiguration>, DecoratorService<ItemConfiguration>>();
services.AddScoped<IDecoratorService<ItemHeaderConfiguration>,
DecoratorService<ItemHeaderConfiguration>>();
services.AddScoped<IDecoratorService<ItemConfiguration>,
DecoratorService<ItemConfiguration>>();
services.AddTemplate<ItemViewModel, ItemView>("Item");
@@ -2,6 +2,6 @@
namespace Wallet;
public interface IWalletActivityService :
public interface IWalletInactivityTimer :
IInitialization,
IDisposable;
+2
View File
@@ -6,4 +6,6 @@ public record WalletConfiguration :
ComponentConfiguration
{
public string? Key { get; set; }
public long? LockTimeout { get; set; } = 300000;
}
@@ -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<ActivatedEventArgs<Wallet>>,
INotificationHandler<DeactivatedEventArgs<Wallet>>,
INotificationHandler<OpenedEventArgs<Wallet>>,
INotificationHandler<ClosedEventArgs<Wallet>>
{
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<Wallet> 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<Wallet> 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<Wallet>());
}
}
private void OnUserInteracted(object? sender, UserInteractedEventArgs args)
{
if (isOpen)
{
ResetTimer();
}
}
public void Dispose()
{
userInteraction.UserInteracted -= OnUserInteracted;
timer?.Dispose();
}
}