From d949a1d48e4649d0658b1a2cf46f3fa8b2a16189 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Sat, 15 Jun 2024 20:15:58 +0100 Subject: [PATCH] Added an ActivityLock for locking into an activity state --- Toolkit.Avalonia/ContentDialogHandler.cs | 22 ---------------------- Toolkit.Foundation/AsyncLock.cs | 19 +++++++++++++++++-- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/Toolkit.Avalonia/ContentDialogHandler.cs b/Toolkit.Avalonia/ContentDialogHandler.cs index af56a81..4ab92e0 100644 --- a/Toolkit.Avalonia/ContentDialogHandler.cs +++ b/Toolkit.Avalonia/ContentDialogHandler.cs @@ -21,13 +21,6 @@ public class ContentDialogHandler(IDispatcher dispatcher) : { if (content is IPrimaryConfirmation primaryConfirmation) { - List postActions = []; - if (content is IActivityIndicator activityIndicator) - { - activityIndicator.Active = true; - postActions.Add(() => activityIndicator.Active = false); - } - Deferral deferral = args.GetDeferral(); if (!await primaryConfirmation.Confirm()) { @@ -36,10 +29,6 @@ public class ContentDialogHandler(IDispatcher dispatcher) : } deferral.Complete(); - foreach (Action action in postActions) - { - action.Invoke(); - } } } } @@ -52,13 +41,6 @@ public class ContentDialogHandler(IDispatcher dispatcher) : { if (content is ISecondaryConfirmation secondaryConfirmation) { - List postActions = []; - if (content is IActivityIndicator activityIndicator) - { - activityIndicator.Active = true; - postActions.Add(() => activityIndicator.Active = false); - } - Deferral deferral = args.GetDeferral(); if (!await secondaryConfirmation.Confirm()) { @@ -67,10 +49,6 @@ public class ContentDialogHandler(IDispatcher dispatcher) : } deferral.Complete(); - foreach (Action action in postActions) - { - action.Invoke(); - } } } } diff --git a/Toolkit.Foundation/AsyncLock.cs b/Toolkit.Foundation/AsyncLock.cs index 5646f49..0d6382e 100644 --- a/Toolkit.Foundation/AsyncLock.cs +++ b/Toolkit.Foundation/AsyncLock.cs @@ -2,18 +2,33 @@ namespace Toolkit.Foundation; +public class ActivityLock(IActivityIndicator activityIndicator) : AsyncLock +{ + public override TaskAwaiter GetAwaiter() + { + activityIndicator.Active = true; + return base.GetAwaiter(); + } + + public override void Dispose() + { + activityIndicator.Active = false; + base.Dispose(); + } +} + public class AsyncLock(int initial = 1, int maximum = 1) : IDisposable { private readonly SemaphoreSlim semaphore = new(initial, maximum); - public void Dispose() + public virtual void Dispose() { semaphore.Release(); } - public TaskAwaiter GetAwaiter() => LockAsync().GetAwaiter(); + public virtual TaskAwaiter GetAwaiter() => LockAsync().GetAwaiter(); private async Task LockAsync() {