Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<NuGetAuditMode>all</NuGetAuditMode>

<!-- MAUI Specific -->
<MauiPackageVersion>9.0.111</MauiPackageVersion>
<MauiPackageVersion>9.0.120</MauiPackageVersion>
<NextMauiPackageVersion>10.0.0</NextMauiPackageVersion>
<MauiStrictXamlCompilation>true</MauiStrictXamlCompilation>
<SkipValidateMauiImplicitPackageReferences>true</SkipValidateMauiImplicitPackageReferences>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public PopupsPage(PopupsViewModel multiplePopupViewModel, IPopupService popupSer
InitializeComponent();
}

protected override async void OnNavigatedTo(NavigatedToEventArgs args)
{
base.OnNavigatedTo(args);
if (args.WasPreviousPageACommunityToolkitPopupPage())
{
await Toast.Make("Popup Closed").Show();
}
}

async void HandleSimplePopupButtonClicked(object sender, EventArgs e)
{
var queryAttributes = new Dictionary<string, object>
Expand Down
8 changes: 5 additions & 3 deletions src/CommunityToolkit.Maui.UnitTests/BaseViewTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,17 @@ static void InitializeServicesAndSetMockApplication(out IServiceProvider service
var mauiApp = appBuilder.Build();
serviceProvider = mauiApp.Services;

var page = new ContentPage();
var shell = new Shell();
shell.Items.Add(new ContentPage());

var application = (MockApplication)mauiApp.Services.GetRequiredService<IApplication>();
application.AddWindow(new Window { Page = page });
application.AddWindow(new Window { Page = shell });

IPlatformApplication.Current = application;

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

CreateViewHandler<MockPageHandler>(page);
CreateViewHandler<MockPageHandler>(shell);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using CommunityToolkit.Maui.Extensions;
using CommunityToolkit.Maui.UnitTests.Mocks;
using CommunityToolkit.Maui.UnitTests.Services;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Extensions;

public class NavigatedToEventArgsExtensionsTests : BaseViewTest
{
[Fact]
public async Task NavigatedToEventArgsExtensions_WasPreviousPageACommunityToolkitPopupPage_ShouldReturnTrue()
{
// Arrange
TaskCompletionSource<bool?> wasPreviousPageACommunityToolkitPopupPageTCS = 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.NavigatedToEventArgsReceived += HandleNavigatedToEventArgsReceived;

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 wasPreviousPageACommunityToolkitPopupPage = await wasPreviousPageACommunityToolkitPopupPageTCS.Task;

// Assert
Assert.True(wasPreviousPageACommunityToolkitPopupPage);

void HandleNavigatedToEventArgsReceived(object? sender, NavigatedToEventArgs e)
{
if (e.PreviousPage != mainPage)
{
wasPreviousPageACommunityToolkitPopupPageTCS.SetResult(e.WasPreviousPageACommunityToolkitPopupPage());
}
}
}

[Fact]
public async Task NavigatedToEventArgsExtensions_WasPreviousPageACommunityToolkitPopupPage_ShouldReturnFalse()
{
// Arrange
TaskCompletionSource<bool?> wasPreviousPageACommunityToolkitPopupPageTCS = 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.NavigatedToEventArgsReceived += HandleNavigatedToEventArgsReceived;

// Act
await mainPage.Navigation.PushAsync(shellContentPage);
var wasPreviousPageACommunityToolkitPopupPage = await wasPreviousPageACommunityToolkitPopupPageTCS.Task;

// Assert
Assert.False(wasPreviousPageACommunityToolkitPopupPage);

void HandleNavigatedToEventArgsReceived(object? sender, NavigatedToEventArgs e)
{
wasPreviousPageACommunityToolkitPopupPageTCS.SetResult(e.WasPreviousPageACommunityToolkitPopupPage());
}
}

sealed class ShellContentPage : ContentPage
{
public event EventHandler<NavigatedToEventArgs>? NavigatedToEventArgsReceived;

protected override void OnNavigatedTo(NavigatedToEventArgs args)
{
base.OnNavigatedTo(args);
NavigatedToEventArgsReceived?.Invoke(this, args);
}
}
}
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"/>.
/// </summary>
public static class NavigatedToEventArgsExtensions
{
/// <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;
}
Loading