Timer lock work done
This commit is contained in:
@@ -2,6 +2,6 @@
|
||||
|
||||
namespace Wallet;
|
||||
|
||||
public interface IWalletActivityService :
|
||||
public interface IWalletInactivityTimer :
|
||||
IInitialization,
|
||||
IDisposable;
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user