Skip to content

Commit 7802139

Browse files
Add popup related navigation extension methods (#2955)
New navigationfrom method added
1 parent 4f03f94 commit 7802139

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/PopupsPage.xaml.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ protected override async void OnNavigatedTo(NavigatedToEventArgs args)
2929
}
3030
}
3131

32+
protected override async void OnNavigatedFrom(NavigatedFromEventArgs args)
33+
{
34+
base.OnNavigatedFrom(args);
35+
if (args.IsDestinationPageACommunityToolkitPopupPage())
36+
{
37+
await Toast.Make("Opening Popup").Show();
38+
}
39+
}
40+
3241
async void HandleSimplePopupButtonClicked(object? sender, EventArgs e)
3342
{
3443
var queryAttributes = new Dictionary<string, object>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using CommunityToolkit.Maui.Extensions;
2+
using CommunityToolkit.Maui.UnitTests.Mocks;
3+
using CommunityToolkit.Maui.UnitTests.Services;
4+
using Xunit;
5+
6+
namespace CommunityToolkit.Maui.UnitTests.Extensions;
7+
8+
public class NavigatedFromEventArgsExtensionsTests : BaseViewTest
9+
{
10+
[Fact]
11+
public async Task NavigatedFromEventArgsExtensions_IsDestinationPageACommunityToolkitPopupPage_ShouldReturnTrue()
12+
{
13+
// Arrange
14+
TaskCompletionSource<bool?> isDestinationPageACommunityToolkitPopupPageTCS = new();
15+
var application = (MockApplication)ServiceProvider.GetRequiredService<IApplication>();
16+
var popupService = ServiceProvider.GetRequiredService<IPopupService>();
17+
18+
var shell = (Shell)(application.Windows[0].Page ?? throw new InvalidOperationException("Unable to retrieve Shell"));
19+
var mainPage = shell.CurrentPage;
20+
var shellContentPage = new ShellContentPage();
21+
shellContentPage.NavigatedFromEventArgsReceived += HandleNavigatedFromEventArgsReceived;
22+
23+
var shellParameters = new Dictionary<string, object>
24+
{
25+
{ nameof(ContentPage.BackgroundColor), Colors.Orange }
26+
};
27+
28+
29+
// Act
30+
await mainPage.Navigation.PushAsync(shellContentPage);
31+
await popupService.ShowPopupAsync<ShortLivedMockPageViewModel>(shell, null, shellParameters, TestContext.Current.CancellationToken);
32+
var isDestinationPageACommunityToolkitPopupPage = await isDestinationPageACommunityToolkitPopupPageTCS.Task;
33+
34+
// Assert
35+
Assert.True(isDestinationPageACommunityToolkitPopupPage);
36+
37+
void HandleNavigatedFromEventArgsReceived(object? sender, NavigatedFromEventArgs e)
38+
{
39+
isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage());
40+
}
41+
}
42+
43+
[Fact]
44+
public async Task NavigatedFromEventArgsExtensions_IsDestinationPageACommunityToolkitPopupPage_ShouldReturnFalse()
45+
{
46+
// Arrange
47+
TaskCompletionSource<bool?> isDestinationPageACommunityToolkitPopupPageTCS = new();
48+
var application = (MockApplication)ServiceProvider.GetRequiredService<IApplication>();
49+
50+
var shell = (Shell)(application.Windows[0].Page ?? throw new InvalidOperationException("Unable to retrieve Shell"));
51+
var mainPage = shell.CurrentPage;
52+
var shellContentPage = new ShellContentPage();
53+
shellContentPage.NavigatedFromEventArgsReceived += HandleNavigatedFromEventArgsReceived;
54+
var newShellContentPage = new ShellContentPage();
55+
56+
57+
// Act
58+
await mainPage.Navigation.PushAsync(shellContentPage);
59+
//push a new content page on top to make sure the navigation handler doesn't think we're navigating to a popup page
60+
await mainPage.Navigation.PushAsync(newShellContentPage);
61+
var isDestinationPageACommunityToolkitPopupPage = await isDestinationPageACommunityToolkitPopupPageTCS.Task;
62+
63+
// Assert
64+
Assert.False(isDestinationPageACommunityToolkitPopupPage);
65+
66+
void HandleNavigatedFromEventArgsReceived(object? sender, NavigatedFromEventArgs e)
67+
{
68+
isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage());
69+
}
70+
}
71+
72+
sealed class ShellContentPage : ContentPage
73+
{
74+
public event EventHandler<NavigatedFromEventArgs>? NavigatedFromEventArgsReceived;
75+
76+
protected override void OnNavigatedFrom(NavigatedFromEventArgs args)
77+
{
78+
base.OnNavigatedFrom(args);
79+
NavigatedFromEventArgsReceived?.Invoke(this, args);
80+
}
81+
}
82+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using CommunityToolkit.Maui.Views;
2+
3+
namespace CommunityToolkit.Maui.Extensions;
4+
5+
/// <summary>
6+
/// Extension methods for <see cref="NavigatedFromEventArgs"/>.
7+
/// </summary>
8+
public static class NavigatedFromEventArgsExtensions
9+
{
10+
/// <summary>
11+
/// Determines whether the previous page was a Community Toolkit <see cref="Popup"/>.
12+
/// </summary>
13+
/// <param name="args">The current <see cref="NavigatedFromEventArgs"/>.</param>
14+
/// <returns>A boolean indicating whether the previous page was a Community Toolkit <see cref="Popup"/>.</returns>
15+
public static bool IsDestinationPageACommunityToolkitPopupPage(this NavigatedFromEventArgs args) => args.DestinationPage is PopupPage;
16+
}

0 commit comments

Comments
 (0)