Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ protected override async void OnNavigatedFrom(NavigatedFromEventArgs args)
{
base.OnNavigatedFrom(args);
if (args.IsDestinationPageACommunityToolkitPopupPage())
{
await Toast.Make("Popup Opened").Show();
}
}

protected override async void OnNavigatingFrom(NavigatingFromEventArgs args)
{
base.OnNavigatingFrom(args);
if (args.IsDestinationPageACommunityToolkitPopupPage())
{
await Toast.Make("Opening Popup").Show();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public async Task NavigatedFromEventArgsExtensions_IsDestinationPageACommunityTo

void HandleNavigatedFromEventArgsReceived(object? sender, NavigatedFromEventArgs e)
{
shellContentPage.NavigatedFromEventArgsReceived -= HandleNavigatedFromEventArgsReceived;
isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void HandleNavigatedToEventArgsReceived(object? sender, NavigatedToEventArgs e)
{
if (e.PreviousPage != mainPage)
{
shellContentPage.NavigatedToEventArgsReceived -= HandleNavigatedToEventArgsReceived;
wasPreviousPageACommunityToolkitPopupPageTCS.SetResult(e.WasPreviousPageACommunityToolkitPopupPage());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using CommunityToolkit.Maui.Extensions;
using CommunityToolkit.Maui.UnitTests.Mocks;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Extensions;

public class NavigatingFromEventArgsExtensionsTests : BaseViewTest
{
[Fact]
public async Task NavigatingFromEventArgsExtensions_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.NavigatingFromEventArgsReceived += HandleNavigatingFromEventArgsReceived;

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);
bool? isDestinationPageACommunityToolkitPopupPage = await isDestinationPageACommunityToolkitPopupPageTCS.Task;

// Assert
Assert.True(isDestinationPageACommunityToolkitPopupPage);

void HandleNavigatingFromEventArgsReceived(object? sender, NavigatingFromEventArgs e)
{
ArgumentNullException.ThrowIfNull(sender);

if (sender is not ShellContentPage)
{
shellContentPage.NavigatingFromEventArgsReceived -= HandleNavigatingFromEventArgsReceived;
isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage());
}
}
}

[Fact]
public async Task NavigatingFromEventArgsExtensions_IsDestinationPageACommunityToolkitPopupPage_ShouldReturnFalse()
{
// Arrange
MockApplication application = (MockApplication)ServiceProvider.GetRequiredService<IApplication>();
IPopupService popupService = ServiceProvider.GetRequiredService<IPopupService>();

Shell shell = (Shell)(application.Windows[0].Page ?? throw new InvalidOperationException("Unable to retrieve Shell"));
Page mainPage = shell.CurrentPage;

ShellContentPage shellContentPage = new();
ShellContentPage anotherShellContentPage = new();

TaskCompletionSource<bool?> isDestinationPageACommunityToolkitPopupPageTCS = new();
shellContentPage.NavigatingFromEventArgsReceived += HandleNavigatingFromEventArgsReceived;

// Act
await mainPage.Navigation.PushAsync(shellContentPage);
await mainPage.Navigation.PushAsync(anotherShellContentPage);
bool? isDestinationPageACommunityToolkitPopupPage = await isDestinationPageACommunityToolkitPopupPageTCS.Task;

// Assert
Assert.False(isDestinationPageACommunityToolkitPopupPage);

void HandleNavigatingFromEventArgsReceived(object? sender, NavigatingFromEventArgs e)
{
ArgumentNullException.ThrowIfNull(sender);

shellContentPage.NavigatingFromEventArgsReceived -= HandleNavigatingFromEventArgsReceived;
isDestinationPageACommunityToolkitPopupPageTCS.SetResult(e.IsDestinationPageACommunityToolkitPopupPage());
}
}

sealed class ShellContentPage : ContentPage
{
public event EventHandler<NavigatingFromEventArgs>? NavigatingFromEventArgsReceived;

protected override void OnNavigatingFrom(NavigatingFromEventArgs args)
{
base.OnNavigatingFrom(args);
NavigatingFromEventArgsReceived?.Invoke(this, args);
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using CommunityToolkit.Maui.Views;

namespace CommunityToolkit.Maui.Extensions;

/// <summary>
/// Extension methods for <see cref="NavigatedFromEventArgs"/>, <see cref="NavigatedToEventArgs"/>, <see cref="NavigatingFromEventArgs"/> and <see cref="NavigatedFromEventArgs"/>.
/// </summary>
public static class NavigationEventArgsExtensions
{
/// <summary>
/// Determines if the destination page is a Community Toolkit <see cref="Popup"/>.
/// </summary>
/// <param name="args">The current <see cref="NavigatedFromEventArgs"/>.</param>
/// <returns>A boolean indicating if the destination page is a Community Toolkit <see cref="Popup"/>.</returns>
public static bool IsDestinationPageACommunityToolkitPopupPage(this NavigatedFromEventArgs args) => args.DestinationPage is PopupPage;

/// <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;

/// <summary>
/// Determines if the destination page will be a Community Toolkit <see cref="Popup"/>.
/// </summary>
/// <param name="args">The current <see cref="NavigatingFromEventArgs"/>.</param>
/// <returns>A boolean indicating if the destination page will be a Community Toolkit <see cref="Popup"/>.</returns>
public static bool IsDestinationPageACommunityToolkitPopupPage(this NavigatingFromEventArgs args) => args.DestinationPage is PopupPage;
}
Loading