Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ protected override async void OnNavigatedTo(NavigatedToEventArgs args)
}
}

protected override async void OnNavigatedFrom(NavigatedFromEventArgs args)
{
base.OnNavigatedFrom(args);
if (args.IsDestinationPageACommunityToolkitPopupPage())
{
await Toast.Make("Opening Popup").Show();
}
}

async void HandleSimplePopupButtonClicked(object sender, EventArgs e)
{
var queryAttributes = new Dictionary<string, object>
Expand Down
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);
}
}
}
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 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;
}