Skip to content

Commit 1502a39

Browse files
Provide an extension method to detect if popup was previous page (CommunityToolkit#2767)
* Provide an extension method to detect if popup was previous page * Add Unit Tests * Add Sample * Remove Unused Code * `dotnet format` * Increase `MauiPackageVersion1 --------- Co-authored-by: Shaun Lawrence <17139988+bijington@users.noreply.github.com> Co-authored-by: Brandon Minnick <13558917+TheCodeTraveler@users.noreply.github.com>
1 parent 91b5cf0 commit 1502a39

File tree

5 files changed

+112
-4
lines changed

5 files changed

+112
-4
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<NuGetAuditMode>all</NuGetAuditMode>
1717

1818
<!-- MAUI Specific -->
19-
<MauiPackageVersion>9.0.111</MauiPackageVersion>
19+
<MauiPackageVersion>9.0.120</MauiPackageVersion>
2020
<NextMauiPackageVersion>10.0.0</NextMauiPackageVersion>
2121
<MauiStrictXamlCompilation>true</MauiStrictXamlCompilation>
2222
<SkipValidateMauiImplicitPackageReferences>true</SkipValidateMauiImplicitPackageReferences>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ public PopupsPage(PopupsViewModel multiplePopupViewModel, IPopupService popupSer
2020
InitializeComponent();
2121
}
2222

23+
protected override async void OnNavigatedTo(NavigatedToEventArgs args)
24+
{
25+
base.OnNavigatedTo(args);
26+
if (args.WasPreviousPageACommunityToolkitPopupPage())
27+
{
28+
await Toast.Make("Popup Closed").Show();
29+
}
30+
}
31+
2332
async void HandleSimplePopupButtonClicked(object sender, EventArgs e)
2433
{
2534
var queryAttributes = new Dictionary<string, object>

src/CommunityToolkit.Maui.UnitTests/BaseViewTest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,17 @@ static void InitializeServicesAndSetMockApplication(out IServiceProvider service
8484
var mauiApp = appBuilder.Build();
8585
serviceProvider = mauiApp.Services;
8686

87-
var page = new ContentPage();
87+
var shell = new Shell();
88+
shell.Items.Add(new ContentPage());
89+
8890
var application = (MockApplication)mauiApp.Services.GetRequiredService<IApplication>();
89-
application.AddWindow(new Window { Page = page });
91+
application.AddWindow(new Window { Page = shell });
9092

9193
IPlatformApplication.Current = application;
9294

9395
application.Handler = new ApplicationHandlerStub();
9496
application.Handler.SetMauiContext(new HandlersContextStub(serviceProvider));
9597

96-
CreateViewHandler<MockPageHandler>(page);
98+
CreateViewHandler<MockPageHandler>(shell);
9799
}
98100
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 NavigatedToEventArgsExtensionsTests : BaseViewTest
9+
{
10+
[Fact]
11+
public async Task NavigatedToEventArgsExtensions_WasPreviousPageACommunityToolkitPopupPage_ShouldReturnTrue()
12+
{
13+
// Arrange
14+
TaskCompletionSource<bool?> wasPreviousPageACommunityToolkitPopupPageTCS = 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.NavigatedToEventArgsReceived += HandleNavigatedToEventArgsReceived;
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 wasPreviousPageACommunityToolkitPopupPage = await wasPreviousPageACommunityToolkitPopupPageTCS.Task;
33+
34+
// Assert
35+
Assert.True(wasPreviousPageACommunityToolkitPopupPage);
36+
37+
void HandleNavigatedToEventArgsReceived(object? sender, NavigatedToEventArgs e)
38+
{
39+
if (e.PreviousPage != mainPage)
40+
{
41+
wasPreviousPageACommunityToolkitPopupPageTCS.SetResult(e.WasPreviousPageACommunityToolkitPopupPage());
42+
}
43+
}
44+
}
45+
46+
[Fact]
47+
public async Task NavigatedToEventArgsExtensions_WasPreviousPageACommunityToolkitPopupPage_ShouldReturnFalse()
48+
{
49+
// Arrange
50+
TaskCompletionSource<bool?> wasPreviousPageACommunityToolkitPopupPageTCS = new();
51+
var application = (MockApplication)ServiceProvider.GetRequiredService<IApplication>();
52+
53+
var shell = (Shell)(application.Windows[0].Page ?? throw new InvalidOperationException("Unable to retrieve Shell"));
54+
var mainPage = shell.CurrentPage;
55+
var shellContentPage = new ShellContentPage();
56+
shellContentPage.NavigatedToEventArgsReceived += HandleNavigatedToEventArgsReceived;
57+
58+
// Act
59+
await mainPage.Navigation.PushAsync(shellContentPage);
60+
var wasPreviousPageACommunityToolkitPopupPage = await wasPreviousPageACommunityToolkitPopupPageTCS.Task;
61+
62+
// Assert
63+
Assert.False(wasPreviousPageACommunityToolkitPopupPage);
64+
65+
void HandleNavigatedToEventArgsReceived(object? sender, NavigatedToEventArgs e)
66+
{
67+
wasPreviousPageACommunityToolkitPopupPageTCS.SetResult(e.WasPreviousPageACommunityToolkitPopupPage());
68+
}
69+
}
70+
71+
sealed class ShellContentPage : ContentPage
72+
{
73+
public event EventHandler<NavigatedToEventArgs>? NavigatedToEventArgsReceived;
74+
75+
protected override void OnNavigatedTo(NavigatedToEventArgs args)
76+
{
77+
base.OnNavigatedTo(args);
78+
NavigatedToEventArgsReceived?.Invoke(this, args);
79+
}
80+
}
81+
}
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="NavigatedToEventArgs"/>.
7+
/// </summary>
8+
public static class NavigatedToEventArgsExtensions
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="NavigatedToEventArgs"/>.</param>
14+
/// <returns>A boolean indicating whether the previous page was a Community Toolkit <see cref="Popup"/>.</returns>
15+
public static bool WasPreviousPageACommunityToolkitPopupPage(this NavigatedToEventArgs args) => args.PreviousPage is PopupPage;
16+
}

0 commit comments

Comments
 (0)