From c7d4daca942607957fe51a71b6648ff58352ab80 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Thu, 18 Jul 2024 20:16:45 +0100 Subject: [PATCH] Partial work on WalletActivityService --- Wallet.Avalonia/App.axaml.cs | 2 + Wallet/IWalletActivityService.cs | 7 ++++ Wallet/ItemCreatedHandler.cs | 3 +- Wallet/WalletActivityService.cs | 70 ++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 Wallet/IWalletActivityService.cs create mode 100644 Wallet/WalletActivityService.cs diff --git a/Wallet.Avalonia/App.axaml.cs b/Wallet.Avalonia/App.axaml.cs index 7e990e2..93a96a3 100644 --- a/Wallet.Avalonia/App.axaml.cs +++ b/Wallet.Avalonia/App.axaml.cs @@ -73,6 +73,8 @@ public partial class App : Application services.AddTransient(_ => provider.GetServices>()); + services.AddInitializer(); + services.AddTransient(); services.AddTransient(); diff --git a/Wallet/IWalletActivityService.cs b/Wallet/IWalletActivityService.cs new file mode 100644 index 0000000..61a2223 --- /dev/null +++ b/Wallet/IWalletActivityService.cs @@ -0,0 +1,7 @@ +using Toolkit.Foundation; + +namespace Wallet; + +public interface IWalletActivityService : + IInitialization, + IDisposable; diff --git a/Wallet/ItemCreatedHandler.cs b/Wallet/ItemCreatedHandler.cs index 9612064..5945542 100644 --- a/Wallet/ItemCreatedHandler.cs +++ b/Wallet/ItemCreatedHandler.cs @@ -28,8 +28,7 @@ public class ItemCreatedHandler(IServiceProvider serviceProvider, int index = cache.IndexOf(cachedItem); decoratorService.Set(cachedItem); - publisher.Publish(Insert.As(index, viewModel), - nameof(ItemNavigationCollectionViewModel)); + publisher.Publish(Insert.As(index, viewModel), "All"); } } diff --git a/Wallet/WalletActivityService.cs b/Wallet/WalletActivityService.cs new file mode 100644 index 0000000..3393196 --- /dev/null +++ b/Wallet/WalletActivityService.cs @@ -0,0 +1,70 @@ +using Toolkit.Foundation; +using Timer = System.Threading.Timer; + +namespace Wallet; + +public class WalletActivityService(ISubscriber subscriber, + IPublisher publisher) : + IWalletActivityService, + INotificationHandler>, + INotificationHandler>, + INotificationHandler>, + INotificationHandler> +{ + private bool isOpen; + private readonly int timeout = 10000; + private Timer? timer; + + public void Dispose() + { + timer?.Dispose(); + } + + public Task Handle(ActivatedEventArgs args) + { + if (isOpen) + { + timer?.Change(Timeout.Infinite, Timeout.Infinite); + } + + return Task.CompletedTask; + } + + public Task Handle(DeactivatedEventArgs args) + { + if (isOpen) + { + timer?.Change(timeout, Timeout.Infinite); + } + + return Task.CompletedTask; + } + + public Task Handle(OpenedEventArgs args) + { + isOpen = true; + return Task.CompletedTask; + } + + public Task Handle(ClosedEventArgs args) + { + isOpen = false; + timer?.Change(Timeout.Infinite, Timeout.Infinite); + + return Task.CompletedTask; + } + + public void Initialize() + { + subscriber.Subscribe(this); + timer = new Timer(OnTimedEvent, null, Timeout.Infinite, Timeout.Infinite); + } + + private void OnTimedEvent(object? state) + { + if (isOpen) + { + publisher.PublishUI(new ClosedEventArgs()); + } + } +}