Repository navigation
[iOS/MacCatalyst] Adopt scene lifecycle and AppActions dispatch #38601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
07cb116
dffde8f
b9b9ad3
35432d1
f25bdb1
70374be
299fa05
7deff1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| #nullable enable | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Maui.ApplicationModel; | ||
| using Microsoft.Maui.LifecycleEvents; | ||
| using UIKit; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Maui.MauiBlazorWebView.DeviceTests; | ||
|
|
||
| [Category("AppActions")] | ||
| public class AppActionsHostConfigurationTests : Microsoft.Maui.DeviceTests.ControlsHandlerTestBase | ||
| { | ||
| [Fact] | ||
| public async Task ConfigureTestBuilderDeliversAppActionAndCompletesOnce() | ||
| { | ||
| await InvokeOnMainThreadAsync(async () => | ||
| { | ||
| var lifecycleService = ApplicationServices.GetRequiredService<ILifecycleEventService>(); | ||
| var forwarder = Assert.Single( | ||
| lifecycleService.GetEventDelegates<iOSLifecycle.PerformActionForShortcutItem>( | ||
| nameof(iOSLifecycle.PerformActionForShortcutItem))); | ||
| var originalShortcutItems = UIApplication.SharedApplication.ShortcutItems; | ||
| var activationCount = 0; | ||
| var completionCount = 0; | ||
| bool? completionResult = null; | ||
| string? receivedId = null; | ||
| EventHandler<AppActionEventArgs> activated = (_, args) => | ||
| { | ||
| activationCount++; | ||
| receivedId = args.AppAction.Id; | ||
| }; | ||
| AppActions.OnAppAction += activated; | ||
|
|
||
| try | ||
| { | ||
| await AppActions.SetAsync(new AppAction("configured-host-action", "Configured Host Action")); | ||
| var shortcutItems = UIApplication.SharedApplication.ShortcutItems; | ||
| Assert.NotNull(shortcutItems); | ||
| var shortcutItem = Assert.Single(shortcutItems); | ||
|
|
||
| forwarder( | ||
| UIApplication.SharedApplication, | ||
| shortcutItem, | ||
| handled => | ||
| { | ||
| completionCount++; | ||
| completionResult = handled; | ||
| }); | ||
|
|
||
| Assert.Equal(1, activationCount); | ||
| Assert.Equal("configured-host-action", receivedId); | ||
| Assert.Equal(1, completionCount); | ||
| Assert.Equal(true, completionResult); | ||
| } | ||
| finally | ||
| { | ||
| AppActions.OnAppAction -= activated; | ||
| UIApplication.SharedApplication.ShortcutItems = originalShortcutItems; | ||
| } | ||
| }); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #nullable enable | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Maui.ApplicationModel; | ||
| using Microsoft.Maui.LifecycleEvents; | ||
| using UIKit; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Maui.DeviceTests | ||
| { | ||
| [Category("AppActions")] | ||
| public class AppActionsHostConfigurationTests : ControlsHandlerTestBase | ||
| { | ||
| [Fact] | ||
| public async Task ConfigureTestBuilderDeliversAppActionAndCompletesOnce() | ||
| { | ||
| await InvokeOnMainThreadAsync(async () => | ||
| { | ||
| var lifecycleService = ApplicationServices.GetRequiredService<ILifecycleEventService>(); | ||
| var forwarder = Assert.Single( | ||
| lifecycleService.GetEventDelegates<iOSLifecycle.PerformActionForShortcutItem>( | ||
| nameof(iOSLifecycle.PerformActionForShortcutItem))); | ||
| var originalShortcutItems = UIApplication.SharedApplication.ShortcutItems; | ||
| var activationCount = 0; | ||
| var completionCount = 0; | ||
| bool? completionResult = null; | ||
| string? receivedId = null; | ||
| EventHandler<AppActionEventArgs> activated = (_, args) => | ||
| { | ||
| activationCount++; | ||
| receivedId = args.AppAction.Id; | ||
| }; | ||
| AppActions.OnAppAction += activated; | ||
|
|
||
| try | ||
| { | ||
| await AppActions.SetAsync(new AppAction("configured-host-action", "Configured Host Action")); | ||
| var shortcutItems = UIApplication.SharedApplication.ShortcutItems; | ||
| Assert.NotNull(shortcutItems); | ||
| var shortcutItem = Assert.Single(shortcutItems); | ||
|
|
||
| forwarder( | ||
| UIApplication.SharedApplication, | ||
| shortcutItem, | ||
| handled => | ||
| { | ||
| completionCount++; | ||
| completionResult = handled; | ||
| }); | ||
|
|
||
| Assert.Equal(1, activationCount); | ||
| Assert.Equal("configured-host-action", receivedId); | ||
| Assert.Equal(1, completionCount); | ||
| Assert.Equal(true, completionResult); | ||
| } | ||
| finally | ||
| { | ||
| AppActions.OnAppAction -= activated; | ||
| UIApplication.SharedApplication.ShortcutItems = originalShortcutItems; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| #nullable enable | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.Maui.LifecycleEvents | ||
| { | ||
|
|
@@ -31,6 +33,49 @@ internal static void InvokeLifecycleEvents<TDelegate>(this IServiceProvider serv | |
| action?.Invoke(del); | ||
| } | ||
|
|
||
| internal static void InvokeLifecycleEventsWithCompletion<TDelegate>( | ||
| this IServiceProvider? services, | ||
| Action<TDelegate, Action<bool>> invoke, | ||
| Action<bool> completionHandler) | ||
| where TDelegate : Delegate | ||
| { | ||
| if (invoke is null) | ||
| throw new ArgumentNullException(nameof(invoke)); | ||
| if (completionHandler is null) | ||
| throw new ArgumentNullException(nameof(completionHandler)); | ||
|
|
||
| var completion = new LifecycleEventCompletion(completionHandler); | ||
|
|
||
| try | ||
| { | ||
| if (services is not null) | ||
| { | ||
| var delegates = new List<TDelegate>(services.GetLifecycleEventDelegates<TDelegate>()); | ||
| var logger = delegates.Count > 0 | ||
| ? services.GetService<ILoggerFactory>()?.CreateLogger<LifecycleEventService>() | ||
| : null; | ||
|
|
||
| for (var i = 0; i < delegates.Count; i++) | ||
| invoke(delegates[i], completion.CreateCallback(logger, typeof(TDelegate).Name)); | ||
|
Copilot marked this conversation as resolved.
Resolved
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[major] Logic and Correctness —
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The all-unhandled + missing-acknowledgement case is real. 35432d159a fixes the concrete repository observer, removes the duplicate forwarders, and explicitly documents the per-registration compatibility contract. I did not add the proposed arbitrary timeout or treat returning from a This is an explicit limitation, not a claim that arbitrary non-cooperative third-party callbacks now complete automatically: if no registration reports I also investigated the actual CI failure rather than attributing it to this pending-callback case. Both Release Helix consoles showed |
||
| } | ||
| } | ||
| catch (Exception exception) | ||
| { | ||
| try | ||
| { | ||
| completion.Fail(); | ||
| } | ||
| catch (Exception completionException) | ||
| { | ||
| throw new AggregateException(exception, completionException); | ||
| } | ||
|
|
||
| throw; | ||
| } | ||
|
|
||
| completion.CompleteInvocation(); | ||
| } | ||
|
|
||
| internal static IEnumerable<TDelegate> GetLifecycleEventDelegates<TDelegate>(this IServiceProvider services, string? eventName = null) | ||
| where TDelegate : Delegate | ||
| { | ||
|
|
@@ -60,5 +105,57 @@ internal static bool ContainsLifecycleEvent<TDelegate>(this IServiceProvider ser | |
|
|
||
| return false; | ||
| } | ||
|
|
||
| sealed class LifecycleEventCompletion | ||
| { | ||
| Action<bool>? _completionHandler; | ||
| // Keep one response pending until every handler invocation has returned successfully. | ||
| int _remaining = 1; | ||
| int _handled; | ||
| int _invocationCompleted; | ||
|
|
||
| public LifecycleEventCompletion(Action<bool> completionHandler) => | ||
| _completionHandler = completionHandler; | ||
|
|
||
| public Action<bool> CreateCallback(ILogger? logger, string eventName) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Testing — The "handler never acknowledges and nobody reports Flagged by: 3/3 reviewers (converged after dispute; the stronger ❌ framing of this area was discarded)
The existing tests cover the Why it is worth pinning: this is the discriminating negative case for the new contract. Without it, a future refactor that adds a default- Suggested fix: add a case to
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added It registers the requested pair: the first handler acknowledges I also checked the discriminating negative case: temporarily acknowledging The name and assertion deliberately pin non-completion by dispatch return. An immediate deterministic test cannot prove the absence of every possible future timeout, so I have not made that stronger claim or added timing-dependent coverage. No runtime acknowledgement policy changed. |
||
| { | ||
| Interlocked.Increment(ref _remaining); | ||
| var invoked = 0; | ||
|
|
||
| return handled => | ||
| { | ||
| if (Interlocked.Exchange(ref invoked, 1) != 0) | ||
| { | ||
| logger?.LogWarning("The {LifecycleEvent} lifecycle event handler invoked its completion callback more than once.", eventName); | ||
| return; | ||
| } | ||
|
|
||
| Complete(handled); | ||
| }; | ||
| } | ||
|
|
||
| public void CompleteInvocation() | ||
| { | ||
| Interlocked.Exchange(ref _invocationCompleted, 1); | ||
| Complete(false); | ||
| } | ||
|
|
||
| void Complete(bool handled) | ||
| { | ||
| if (handled) | ||
| Interlocked.Exchange(ref _handled, 1); | ||
|
|
||
| var remaining = Interlocked.Decrement(ref _remaining); | ||
| var wasHandled = Volatile.Read(ref _handled) != 0; | ||
| if (Volatile.Read(ref _invocationCompleted) != 0 && (wasHandled || remaining == 0)) | ||
| Finish(wasHandled); | ||
|
Copilot marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| public void Fail() => | ||
| Finish(false); | ||
|
|
||
| void Finish(bool handled) => | ||
| Interlocked.Exchange(ref _completionHandler, null)?.Invoke(handled); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.