Files
Walleby/Wallet/WalletInactivity.cs
TheXamlGuy 8e0f1e6715 Rename
2024-07-18 21:55:01 +01:00

103 lines
2.2 KiB
C#

using Toolkit.Foundation;
using Timer = System.Threading.Timer;
namespace Wallet;
public class WalletInactivity(WalletConfiguration configuration,
IUserInteraction userInteraction,
ISubscriber subscriber,
IPublisher publisher) :
IWalletInactivity,
INotificationHandler<ActivatedEventArgs<Wallet>>,
INotificationHandler<DeactivatedEventArgs<Wallet>>,
INotificationHandler<OpenedEventArgs<Wallet>>,
INotificationHandler<ClosedEventArgs<Wallet>>
{
private bool isOpen;
private Timer? timer;
private readonly object timerLock = new();
public void Initialize()
{
subscriber.Subscribe(this);
timer = new Timer(OnTimedEvent, null, Timeout.Infinite, Timeout.Infinite);
userInteraction.UserInteracted += OnUserInteracted;
}
public Task Handle(ActivatedEventArgs<Wallet> args)
{
if (isOpen)
{
ResetTimer();
userInteraction.Start();
}
return Task.CompletedTask;
}
public Task Handle(DeactivatedEventArgs<Wallet> args)
{
if (isOpen)
{
ResetTimer();
userInteraction.Stop();
}
return Task.CompletedTask;
}
public Task Handle(OpenedEventArgs<Wallet> args)
{
isOpen = true;
return Task.CompletedTask;
}
public Task Handle(ClosedEventArgs<Wallet> args)
{
isOpen = false;
StopTimer();
userInteraction.Stop();
return Task.CompletedTask;
}
private void ResetTimer()
{
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)
{
if (isOpen)
{
publisher.PublishUI(new ClosedEventArgs<Wallet>());
}
}
private void OnUserInteracted(object? sender, UserInteractedEventArgs args)
{
if (isOpen)
{
ResetTimer();
}
}
public void Dispose()
{
userInteraction.UserInteracted -= OnUserInteracted;
timer?.Dispose();
}
}