Timer lock work done
This commit is contained in:
@@ -57,7 +57,8 @@ public partial class App : Application
|
|||||||
|
|
||||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime)
|
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime)
|
||||||
{
|
{
|
||||||
services.AddTemplate<MainWindowViewModel, MainWindow>("MainWindow");
|
services.AddTemplate<MainWindowViewModel,
|
||||||
|
MainWindow>("MainWindow");
|
||||||
}
|
}
|
||||||
|
|
||||||
services.AddHandler<WalletActivatedHandler>();
|
services.AddHandler<WalletActivatedHandler>();
|
||||||
@@ -65,15 +66,17 @@ public partial class App : Application
|
|||||||
{
|
{
|
||||||
args.AddServices(services =>
|
args.AddServices(services =>
|
||||||
{
|
{
|
||||||
services.AddTransient<IComparer<Item<(Guid, string)>>>(provider => Comparer<Item<(Guid, string)>>.Create((x, z) =>
|
services.AddSingleton<IUserInteraction, UserInteraction>();
|
||||||
StringComparer.CurrentCultureIgnoreCase.Compare(x.Value.Item2, z.Value.Item2)));
|
|
||||||
|
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.AddCache<Item<(Guid, string)>>();
|
||||||
|
|
||||||
services.AddTransient(_ =>
|
services.AddTransient(_ =>
|
||||||
provider.GetServices<IConfigurationDescriptor<ItemConfiguration>>());
|
provider.GetServices<IConfigurationDescriptor<ItemConfiguration>>());
|
||||||
|
|
||||||
services.AddInitializer<WalletActivityService>();
|
services.AddInitializer<WalletInactivityTimer>();
|
||||||
|
|
||||||
services.AddTransient<IWalletFactory, WalletFactory>();
|
services.AddTransient<IWalletFactory, WalletFactory>();
|
||||||
|
|
||||||
@@ -106,7 +109,8 @@ public partial class App : Application
|
|||||||
services.TryAddSingleton<IDecoratorService<SecurityKey>, DecoratorService<SecurityKey>>();
|
services.TryAddSingleton<IDecoratorService<SecurityKey>, DecoratorService<SecurityKey>>();
|
||||||
services.TryAddSingleton<IDecoratorService<WalletConnection>, DecoratorService<WalletConnection>>();
|
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>();
|
services.AddDbContextFactory<WalletContext>();
|
||||||
|
|
||||||
@@ -148,24 +152,29 @@ public partial class App : Application
|
|||||||
services.AddTemplate<BackActionViewModel, BackActionView>();
|
services.AddTemplate<BackActionViewModel, BackActionView>();
|
||||||
services.AddTemplate<SearchWalletActionViewModel, SearchWalletActionView>();
|
services.AddTemplate<SearchWalletActionViewModel, SearchWalletActionView>();
|
||||||
|
|
||||||
services.AddTemplate<ItemCategoryNavigationCollectionViewModel, ItemCategoryNavigationCollectionView>("ItemCategoryCollection");
|
services.AddTemplate<ItemCategoryNavigationCollectionViewModel,
|
||||||
|
ItemCategoryNavigationCollectionView>("ItemCategoryCollection");
|
||||||
services.AddTemplate<ItemCategoryNavigationViewModel, ItemCategoryNavigationView>();
|
services.AddTemplate<ItemCategoryNavigationViewModel, ItemCategoryNavigationView>();
|
||||||
|
|
||||||
services.AddHandler<CategoriesNavigationViewModelActivationHandler>();
|
services.AddHandler<CategoriesNavigationViewModelActivationHandler>();
|
||||||
|
|
||||||
services.AddHandler<ItemCategoryViewModelActivatedHandler>();
|
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<AddItemNavigationViewModel, AddItemNavigationView>();
|
||||||
|
|
||||||
services.AddTemplate<ItemNavigationViewModel, ItemNavigationView>();
|
services.AddTemplate<ItemNavigationViewModel, ItemNavigationView>();
|
||||||
services.AddHandler<ItemNavigationViewModelActivatedHandler>();
|
services.AddHandler<ItemNavigationViewModelActivatedHandler>();
|
||||||
|
|
||||||
services.AddTemplate<EmptyItemCollectionViewModel, EmptyItemCollectionView>("EmptyItemCollection");
|
services.AddTemplate<EmptyItemCollectionViewModel,
|
||||||
|
EmptyItemCollectionView>("EmptyItemCollection");
|
||||||
|
|
||||||
services.AddScoped<IDecoratorService<ItemHeaderConfiguration>, DecoratorService<ItemHeaderConfiguration>>();
|
services.AddScoped<IDecoratorService<ItemHeaderConfiguration>,
|
||||||
services.AddScoped<IDecoratorService<ItemConfiguration>, DecoratorService<ItemConfiguration>>();
|
DecoratorService<ItemHeaderConfiguration>>();
|
||||||
|
services.AddScoped<IDecoratorService<ItemConfiguration>,
|
||||||
|
DecoratorService<ItemConfiguration>>();
|
||||||
|
|
||||||
services.AddTemplate<ItemViewModel, ItemView>("Item");
|
services.AddTemplate<ItemViewModel, ItemView>("Item");
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,6 @@
|
|||||||
|
|
||||||
namespace Wallet;
|
namespace Wallet;
|
||||||
|
|
||||||
public interface IWalletActivityService :
|
public interface IWalletInactivityTimer :
|
||||||
IInitialization,
|
IInitialization,
|
||||||
IDisposable;
|
IDisposable;
|
||||||
@@ -6,4 +6,6 @@ public record WalletConfiguration :
|
|||||||
ComponentConfiguration
|
ComponentConfiguration
|
||||||
{
|
{
|
||||||
public string? Key { get; set; }
|
public string? Key { get; set; }
|
||||||
|
|
||||||
|
public long? LockTimeout { get; set; } = 300000;
|
||||||
}
|
}
|
||||||
@@ -3,28 +3,33 @@ using Timer = System.Threading.Timer;
|
|||||||
|
|
||||||
namespace Wallet;
|
namespace Wallet;
|
||||||
|
|
||||||
public class WalletActivityService(ISubscriber subscriber,
|
public class WalletInactivityTimer(WalletConfiguration configuration,
|
||||||
|
IUserInteraction userInteraction,
|
||||||
|
ISubscriber subscriber,
|
||||||
IPublisher publisher) :
|
IPublisher publisher) :
|
||||||
IWalletActivityService,
|
IWalletInactivityTimer,
|
||||||
INotificationHandler<ActivatedEventArgs<Wallet>>,
|
INotificationHandler<ActivatedEventArgs<Wallet>>,
|
||||||
INotificationHandler<DeactivatedEventArgs<Wallet>>,
|
INotificationHandler<DeactivatedEventArgs<Wallet>>,
|
||||||
INotificationHandler<OpenedEventArgs<Wallet>>,
|
INotificationHandler<OpenedEventArgs<Wallet>>,
|
||||||
INotificationHandler<ClosedEventArgs<Wallet>>
|
INotificationHandler<ClosedEventArgs<Wallet>>
|
||||||
{
|
{
|
||||||
private bool isOpen;
|
private bool isOpen;
|
||||||
private readonly int timeout = 10000;
|
|
||||||
private Timer? timer;
|
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)
|
public Task Handle(ActivatedEventArgs<Wallet> args)
|
||||||
{
|
{
|
||||||
if (isOpen)
|
if (isOpen)
|
||||||
{
|
{
|
||||||
timer?.Change(Timeout.Infinite, Timeout.Infinite);
|
ResetTimer();
|
||||||
|
userInteraction.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
@@ -34,7 +39,8 @@ public class WalletActivityService(ISubscriber subscriber,
|
|||||||
{
|
{
|
||||||
if (isOpen)
|
if (isOpen)
|
||||||
{
|
{
|
||||||
timer?.Change(timeout, Timeout.Infinite);
|
ResetTimer();
|
||||||
|
userInteraction.Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
@@ -49,15 +55,27 @@ public class WalletActivityService(ISubscriber subscriber,
|
|||||||
public Task Handle(ClosedEventArgs<Wallet> args)
|
public Task Handle(ClosedEventArgs<Wallet> args)
|
||||||
{
|
{
|
||||||
isOpen = false;
|
isOpen = false;
|
||||||
timer?.Change(Timeout.Infinite, Timeout.Infinite);
|
|
||||||
|
StopTimer();
|
||||||
|
userInteraction.Stop();
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Initialize()
|
private void ResetTimer()
|
||||||
{
|
{
|
||||||
subscriber.Subscribe(this);
|
lock (timerLock)
|
||||||
timer = new Timer(OnTimedEvent, null, Timeout.Infinite, Timeout.Infinite);
|
{
|
||||||
|
timer?.Change(configuration.LockTimeout ?? 300000, Timeout.Infinite);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void StopTimer()
|
||||||
|
{
|
||||||
|
lock (timerLock)
|
||||||
|
{
|
||||||
|
timer?.Change(Timeout.Infinite, Timeout.Infinite);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnTimedEvent(object? state)
|
private void OnTimedEvent(object? state)
|
||||||
@@ -67,4 +85,18 @@ public class WalletActivityService(ISubscriber subscriber,
|
|||||||
publisher.PublishUI(new ClosedEventArgs<Wallet>());
|
publisher.PublishUI(new ClosedEventArgs<Wallet>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnUserInteracted(object? sender, UserInteractedEventArgs args)
|
||||||
|
{
|
||||||
|
if (isOpen)
|
||||||
|
{
|
||||||
|
ResetTimer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
userInteraction.UserInteracted -= OnUserInteracted;
|
||||||
|
timer?.Dispose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user