Partial work on WalletActivityService

This commit is contained in:
TheXamlGuy
2024-07-18 20:16:45 +01:00
parent f6762f593d
commit c7d4daca94
4 changed files with 80 additions and 2 deletions
+2
View File
@@ -73,6 +73,8 @@ public partial class App : Application
services.AddTransient(_ =>
provider.GetServices<IConfigurationDescriptor<ItemConfiguration>>());
services.AddInitializer<WalletActivityService>();
services.AddTransient<IWalletFactory, WalletFactory>();
services.AddTransient<IKeyGenerator, KeyGenerator>();
+7
View File
@@ -0,0 +1,7 @@
using Toolkit.Foundation;
namespace Wallet;
public interface IWalletActivityService :
IInitialization,
IDisposable;
+1 -2
View File
@@ -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");
}
}
+70
View File
@@ -0,0 +1,70 @@
using Toolkit.Foundation;
using Timer = System.Threading.Timer;
namespace Wallet;
public class WalletActivityService(ISubscriber subscriber,
IPublisher publisher) :
IWalletActivityService,
INotificationHandler<ActivatedEventArgs<Wallet>>,
INotificationHandler<DeactivatedEventArgs<Wallet>>,
INotificationHandler<OpenedEventArgs<Wallet>>,
INotificationHandler<ClosedEventArgs<Wallet>>
{
private bool isOpen;
private readonly int timeout = 10000;
private Timer? timer;
public void Dispose()
{
timer?.Dispose();
}
public Task Handle(ActivatedEventArgs<Wallet> args)
{
if (isOpen)
{
timer?.Change(Timeout.Infinite, Timeout.Infinite);
}
return Task.CompletedTask;
}
public Task Handle(DeactivatedEventArgs<Wallet> args)
{
if (isOpen)
{
timer?.Change(timeout, Timeout.Infinite);
}
return Task.CompletedTask;
}
public Task Handle(OpenedEventArgs<Wallet> args)
{
isOpen = true;
return Task.CompletedTask;
}
public Task Handle(ClosedEventArgs<Wallet> 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<Wallet>());
}
}
}