-
Notifications
You must be signed in to change notification settings - Fork 466
Implement extension method for NavigatedFromEventArgs #2935
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
Closed
JamesBrooksbankIceland
wants to merge
6
commits into
CommunityToolkit:main
from
JamesBrooksbankIceland:add-popup-related-navigation-extension-methods-destinationpage
+108
−1
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
06132b9
Implement extension method for NavigatedFromEventArgs
JamesBrooksbankIceland 5c6fcc8
Removal of commented out code
JamesBrooksbankIceland fe6e4e2
Update src/CommunityToolkit.Maui/Extensions/NavigatedFromEventArgsExt…
bijington 50dc299
Merge branch 'main' into add-popup-related-navigation-extension-metho…
bijington 169cfd1
Merge branch 'main' into add-popup-related-navigation-extension-metho…
TheCodeTraveler df3fc2e
Merge branch 'main' into add-popup-related-navigation-extension-metho…
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
85 changes: 85 additions & 0 deletions
85
src/CommunityToolkit.Maui.UnitTests/Extensions/NavigatedFromEventArgsExtensionsTests.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,85 @@ | ||
| using CommunityToolkit.Maui.Extensions; | ||
| using CommunityToolkit.Maui.UnitTests.Mocks; | ||
| using CommunityToolkit.Maui.UnitTests.Services; | ||
| using Xunit; | ||
|
|
||
| namespace CommunityToolkit.Maui.UnitTests.Extensions; | ||
|
|
||
| public class NavigatedFromEventArgsExtensionsTests : BaseViewTest | ||
| { | ||
| [Fact] | ||
| public async Task NavigatedFromEventArgsExtensions_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.NavigatedFromEventArgsReceived += HandleNavigatedFromEventArgsReceived; | ||
|
|
||
| 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); | ||
| var isDestinationPageACommunityToolkitPopupPage = await isDestinationPageACommunityToolkitPopupPageTCS.Task; | ||
|
|
||
| // Assert | ||
| Assert.True(isDestinationPageACommunityToolkitPopupPage); | ||
|
|
||
| void HandleNavigatedFromEventArgsReceived(object? sender, NavigatedFromEventArgs e) | ||
| { | ||
| //if (e.DestinationPage != mainPage) | ||
| //{ | ||
| isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage()); | ||
| //} | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task NavigatedFromEventArgsExtensions_IsDestinationPageACommunityToolkitPopupPage_ShouldReturnFalse() | ||
| { | ||
| // Arrange | ||
| TaskCompletionSource<bool?> isDestinationPageACommunityToolkitPopupPageTCS = new(); | ||
| var application = (MockApplication)ServiceProvider.GetRequiredService<IApplication>(); | ||
|
|
||
| var shell = (Shell)(application.Windows[0].Page ?? throw new InvalidOperationException("Unable to retrieve Shell")); | ||
| var mainPage = shell.CurrentPage; | ||
| var shellContentPage = new ShellContentPage(); | ||
| shellContentPage.NavigatedFromEventArgsReceived += HandleNavigatedFromEventArgsReceived; | ||
| var newShellContentPage = new ShellContentPage(); | ||
|
|
||
|
|
||
| // Act | ||
| await mainPage.Navigation.PushAsync(shellContentPage); | ||
| //push a new content page on top to make sure the navigation handler doesn't think we're navigating to a popup page | ||
| await mainPage.Navigation.PushAsync(newShellContentPage); | ||
| var isDestinationPageACommunityToolkitPopupPage = await isDestinationPageACommunityToolkitPopupPageTCS.Task; | ||
|
|
||
| // Assert | ||
| Assert.False(isDestinationPageACommunityToolkitPopupPage); | ||
|
|
||
| void HandleNavigatedFromEventArgsReceived(object? sender, NavigatedFromEventArgs e) | ||
| { | ||
| isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage()); | ||
| } | ||
| } | ||
|
|
||
| sealed class ShellContentPage : ContentPage | ||
| { | ||
| public event EventHandler<NavigatedFromEventArgs>? NavigatedFromEventArgsReceived; | ||
|
|
||
| protected override void OnNavigatedFrom(NavigatedFromEventArgs args) | ||
| { | ||
| base.OnNavigatedFrom(args); | ||
| NavigatedFromEventArgsReceived?.Invoke(this, args); | ||
| } | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
src/CommunityToolkit.Maui/Extensions/NavigatedFromEventArgsExtensions.shared.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,16 @@ | ||
| using CommunityToolkit.Maui.Views; | ||
|
|
||
| namespace CommunityToolkit.Maui.Extensions; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for <see cref="NavigatedToEventArgs"/>. | ||
bijington marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| public static class NavigatedFromEventArgsExtensions | ||
| { | ||
| /// <summary> | ||
| /// Determines whether the previous page was a Community Toolkit <see cref="Popup"/>. | ||
| /// </summary> | ||
| /// <param name="args">The current <see cref="NavigatedFromEventArgs"/>.</param> | ||
| /// <returns>A boolean indicating whether the previous page was a Community Toolkit <see cref="Popup"/>.</returns> | ||
| public static bool IsDestinationPageACommunityToolkitPopupPage(this NavigatedFromEventArgs args) => args.DestinationPage is PopupPage; | ||
VladislavAntonyuk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
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.