using Toolkit.Foundation; using Timer = System.Threading.Timer; namespace Wallet; public class WalletInactivity(WalletConfiguration configuration, IUserInteraction userInteraction, ISubscriber subscriber, IPublisher publisher) : IWalletInactivity, INotificationHandler>, INotificationHandler>, INotificationHandler>, INotificationHandler> { 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 args) { if (isOpen) { ResetTimer(); userInteraction.Start(); } return Task.CompletedTask; } public Task Handle(DeactivatedEventArgs args) { if (isOpen) { ResetTimer(); userInteraction.Stop(); } return Task.CompletedTask; } public Task Handle(OpenedEventArgs args) { isOpen = true; return Task.CompletedTask; } public Task Handle(ClosedEventArgs 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()); } } private void OnUserInteracted(object? sender, UserInteractedEventArgs args) { if (isOpen) { ResetTimer(); } } public void Dispose() { userInteraction.UserInteracted -= OnUserInteracted; timer?.Dispose(); } }