-
Notifications
You must be signed in to change notification settings - Fork 491
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
Draft
dartasen
wants to merge
5
commits into
CommunityToolkit:main
Choose a base branch
from
dartasen:feat/addnavigatingextension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3ec77c8
Add support for NavigatingFromEventArgs
dartasen 0a4dd08
Adjust documentation of NavigatedFromEventArgsExtensions
dartasen 484f716
Consolidate NavigationEventArgsExtensions
TheCodeTraveler 90cdcef
Ensure Unit Tests unsubscribe from event handler
TheCodeTraveler c6886ac
Implement `OnNavigatingFrom` in `PopupsPage`
TheCodeTraveler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/CommunityToolkit.Maui.UnitTests/Extensions/NavigatingFromEventArgsExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| using CommunityToolkit.Maui.Extensions; | ||
| using CommunityToolkit.Maui.UnitTests.Mocks; | ||
| using Xunit; | ||
|
|
||
| namespace CommunityToolkit.Maui.UnitTests.Extensions; | ||
|
|
||
| public class NavigatingFromEventArgsExtensionsTests : BaseViewTest | ||
| { | ||
| [Fact] | ||
| 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()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task NavigatingFromEventArgsExtensions_IsDestinationPageACommunityToolkitPopupPage_ShouldReturnFalse() | ||
| { | ||
| // Arrange | ||
| MockApplication application = (MockApplication)ServiceProvider.GetRequiredService<IApplication>(); | ||
| IPopupService popupService = ServiceProvider.GetRequiredService<IPopupService>(); | ||
|
|
||
| Shell shell = (Shell)(application.Windows[0].Page ?? throw new InvalidOperationException("Unable to retrieve Shell")); | ||
| Page mainPage = shell.CurrentPage; | ||
|
|
||
| ShellContentPage shellContentPage = new(); | ||
| ShellContentPage anotherShellContentPage = new(); | ||
|
|
||
| TaskCompletionSource<bool?> isDestinationPageACommunityToolkitPopupPageTCS = new(); | ||
| shellContentPage.NavigatingFromEventArgsReceived += HandleNavigatingFromEventArgsReceived; | ||
|
|
||
| // Act | ||
| await mainPage.Navigation.PushAsync(shellContentPage); | ||
| await mainPage.Navigation.PushAsync(anotherShellContentPage); | ||
| bool? isDestinationPageACommunityToolkitPopupPage = await isDestinationPageACommunityToolkitPopupPageTCS.Task; | ||
|
|
||
| // Assert | ||
| Assert.False(isDestinationPageACommunityToolkitPopupPage); | ||
|
|
||
| void HandleNavigatingFromEventArgsReceived(object? sender, NavigatingFromEventArgs e) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(sender); | ||
|
|
||
| shellContentPage.NavigatingFromEventArgsReceived -= HandleNavigatingFromEventArgsReceived; | ||
| isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage()); | ||
| } | ||
| } | ||
|
|
||
| sealed class ShellContentPage : ContentPage | ||
| { | ||
| public event EventHandler<NavigatingFromEventArgs>? NavigatingFromEventArgsReceived; | ||
|
|
||
| protected override void OnNavigatingFrom(NavigatingFromEventArgs args) | ||
| { | ||
| base.OnNavigatingFrom(args); | ||
| NavigatingFromEventArgsReceived?.Invoke(this, args); | ||
| } | ||
| } | ||
| } | ||
16 changes: 0 additions & 16 deletions
16
src/CommunityToolkit.Maui/Extensions/NavigatedFromEventArgsExtensions.shared.cs
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/CommunityToolkit.Maui/Extensions/NavigatedToEventArgsExtensions.shared.cs
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
src/CommunityToolkit.Maui/Extensions/NavigationEventArgsExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"/>, <see cref="NavigatingFromEventArgs"/> and <see cref="NavigatedFromEventArgs"/>. | ||
| /// </summary> | ||
| public static class NavigationEventArgsExtensions | ||
| { | ||
| /// <summary> | ||
| /// Determines if the destination page is 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 Community Toolkit <see cref="Popup"/>.</returns> | ||
| public static bool IsDestinationPageACommunityToolkitPopupPage(this NavigatedFromEventArgs args) => args.DestinationPage is PopupPage; | ||
|
|
||
| /// <summary> | ||
| /// Determines whether the previous page was 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 Community Toolkit <see cref="Popup"/>.</returns> | ||
| public static bool WasPreviousPageACommunityToolkitPopupPage(this NavigatedToEventArgs args) => args.PreviousPage is PopupPage; | ||
|
|
||
| /// <summary> | ||
| /// Determines if the destination page will be 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 Community Toolkit <see cref="Popup"/>.</returns> | ||
| public static bool IsDestinationPageACommunityToolkitPopupPage(this NavigatingFromEventArgs args) => args.DestinationPage is PopupPage; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.