-
Notifications
You must be signed in to change notification settings - Fork 493
Added OnNavigatingFrom extension for popup related navigation #3099
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
base: main
Are you sure you want to change the base?
Changes from all commits
3ec77c8
0a4dd08
484f716
90cdcef
c6886ac
1c930b9
943cfce
1ce05c8
7db69f9
c0a805f
e6b5f65
e1e08f6
a06260d
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,89 @@ | ||||||||||||||||
| using CommunityToolkit.Maui.Extensions; | ||||||||||||||||
| using CommunityToolkit.Maui.UnitTests.Mocks; | ||||||||||||||||
| using Xunit; | ||||||||||||||||
|
|
||||||||||||||||
| namespace CommunityToolkit.Maui.UnitTests.Extensions; | ||||||||||||||||
|
|
||||||||||||||||
| public class NavigatingFromEventArgsExtensionsTests : BaseViewTest | ||||||||||||||||
| { | ||||||||||||||||
| [Fact(Timeout = (int)TestDuration.Medium)] | ||||||||||||||||
| public async Task NavigatingFromEventArgsExtensions_IsDestinationPageACommunityToolkitPopupPage_ShouldReturnTrue() | ||||||||||||||||
| { | ||||||||||||||||
| // Arrange | ||||||||||||||||
| TaskCompletionSource<bool?> isDestinationPageACommunityToolkitPopupPageTCS = new(); | ||||||||||||||||
| var application = (MockApplication)ServiceProvider.GetRequiredService<IApplication>(); | ||||||||||||||||
| var popupService = ServiceProvider.GetRequiredService<IPopupService>(); | ||||||||||||||||
|
|
||||||||||||||||
| var shell = (Shell)(application.Windows[0].Page ?? throw new InvalidOperationException("Unable to retrieve Shell")); | ||||||||||||||||
| var mainPage = shell.CurrentPage; | ||||||||||||||||
| var shellContentPage = new ShellContentPage(); | ||||||||||||||||
| shellContentPage.NavigatingFromEventArgsReceived += HandleNavigatingFromEventArgsReceived; | ||||||||||||||||
|
|
||||||||||||||||
| var shellParameters = new Dictionary<string, object> | ||||||||||||||||
| { | ||||||||||||||||
| { nameof(ContentPage.BackgroundColor), Colors.Orange } | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| // Act | ||||||||||||||||
| await mainPage.Navigation.PushAsync(shellContentPage); | ||||||||||||||||
| await popupService.ShowPopupAsync<ShortLivedMockPageViewModel>(shell, null, shellParameters, TestContext.Current.CancellationToken); | ||||||||||||||||
| bool? isDestinationPageACommunityToolkitPopupPage = await isDestinationPageACommunityToolkitPopupPageTCS.Task; | ||||||||||||||||
|
|
||||||||||||||||
| // Assert | ||||||||||||||||
| Assert.True(isDestinationPageACommunityToolkitPopupPage); | ||||||||||||||||
|
|
||||||||||||||||
| void HandleNavigatingFromEventArgsReceived(object? sender, NavigatingFromEventArgs e) | ||||||||||||||||
| { | ||||||||||||||||
| ArgumentNullException.ThrowIfNull(sender); | ||||||||||||||||
|
|
||||||||||||||||
| if (sender is not ShellContentPage) | ||||||||||||||||
| { | ||||||||||||||||
| shellContentPage.NavigatingFromEventArgsReceived -= HandleNavigatingFromEventArgsReceived; | ||||||||||||||||
| isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage()); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+39
to
+43
|
||||||||||||||||
| if (sender is not ShellContentPage) | |
| { | |
| shellContentPage.NavigatingFromEventArgsReceived -= HandleNavigatingFromEventArgsReceived; | |
| isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage()); | |
| } | |
| shellContentPage.NavigatingFromEventArgsReceived -= HandleNavigatingFromEventArgsReceived; | |
| isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage()); |
dartasen marked this conversation as resolved.
Show resolved
Hide resolved
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using CommunityToolkit.Maui.Views; | ||
|
|
||
| namespace CommunityToolkit.Maui.Extensions; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for <see cref="NavigatedFromEventArgs"/>, <see cref="NavigatedToEventArgs"/> and <see cref="NavigatingFromEventArgs"/>. | ||
| /// </summary> | ||
| public static class NavigationEventArgsExtensions | ||
| { | ||
| /// <summary> | ||
| /// Determines if the destination page is the internal <see cref="PopupPage"/> wrapper used when displaying a Community Toolkit <see cref="Popup"/>. | ||
| /// </summary> | ||
| /// <param name="args">The current <see cref="NavigatedFromEventArgs"/>.</param> | ||
| /// <returns>A boolean indicating if the destination page is a <see cref="PopupPage"/> representing a Community Toolkit <see cref="Popup"/>.</returns> | ||
| public static bool IsDestinationPageACommunityToolkitPopupPage(this NavigatedFromEventArgs args) => args.DestinationPage is PopupPage; | ||
|
|
||
| /// <summary> | ||
| /// Determines whether the previous page was the internal <see cref="PopupPage"/> wrapper used when displaying a Community Toolkit <see cref="Popup"/>. | ||
| /// </summary> | ||
| /// <param name="args">The current <see cref="NavigatedToEventArgs"/>.</param> | ||
| /// <returns>A boolean indicating whether the previous page was a <see cref="PopupPage"/> representing a Community Toolkit <see cref="Popup"/>.</returns> | ||
| public static bool WasPreviousPageACommunityToolkitPopupPage(this NavigatedToEventArgs args) => args.PreviousPage is PopupPage; | ||
|
|
||
TheCodeTraveler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// <summary> | ||
| /// Determines if the destination page will be the internal <see cref="PopupPage"/> wrapper used when displaying a Community Toolkit <see cref="Popup"/>. | ||
| /// </summary> | ||
| /// <param name="args">The current <see cref="NavigatingFromEventArgs"/>.</param> | ||
| /// <returns>A boolean indicating if the destination page will be a <see cref="PopupPage"/> representing a Community Toolkit <see cref="Popup"/>.</returns> | ||
| public static bool IsDestinationPageACommunityToolkitPopupPage(this NavigatingFromEventArgs args) => args.DestinationPage is PopupPage; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.