diff --git a/Directory.Build.props b/Directory.Build.props index 2da03d98a9..b40f60d699 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -16,7 +16,7 @@ all - 9.0.111 + 9.0.120 10.0.0 true true diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/PopupsPage.xaml.cs b/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/PopupsPage.xaml.cs index 2bc5852b52..79c4a2fb70 100644 --- a/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/PopupsPage.xaml.cs +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/PopupsPage.xaml.cs @@ -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 diff --git a/src/CommunityToolkit.Maui.UnitTests/BaseViewTest.cs b/src/CommunityToolkit.Maui.UnitTests/BaseViewTest.cs index 341eac27a1..523802e4a5 100644 --- a/src/CommunityToolkit.Maui.UnitTests/BaseViewTest.cs +++ b/src/CommunityToolkit.Maui.UnitTests/BaseViewTest.cs @@ -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(); - 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(page); + CreateViewHandler(shell); } } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.UnitTests/Extensions/NavigatedToEventArgsExtensionsTests.cs b/src/CommunityToolkit.Maui.UnitTests/Extensions/NavigatedToEventArgsExtensionsTests.cs new file mode 100644 index 0000000000..dcdc34a086 --- /dev/null +++ b/src/CommunityToolkit.Maui.UnitTests/Extensions/NavigatedToEventArgsExtensionsTests.cs @@ -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 wasPreviousPageACommunityToolkitPopupPageTCS = new(); + var application = (MockApplication)ServiceProvider.GetRequiredService(); + var popupService = ServiceProvider.GetRequiredService(); + + 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 + { + { nameof(ContentPage.BackgroundColor), Colors.Orange } + }; + + + // Act + await mainPage.Navigation.PushAsync(shellContentPage); + await popupService.ShowPopupAsync(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 wasPreviousPageACommunityToolkitPopupPageTCS = new(); + var application = (MockApplication)ServiceProvider.GetRequiredService(); + + 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? NavigatedToEventArgsReceived; + + protected override void OnNavigatedTo(NavigatedToEventArgs args) + { + base.OnNavigatedTo(args); + NavigatedToEventArgsReceived?.Invoke(this, args); + } + } +} \ No newline at end of file diff --git a/src/CommunityToolkit.Maui/Extensions/NavigatedToEventArgsExtensions.shared.cs b/src/CommunityToolkit.Maui/Extensions/NavigatedToEventArgsExtensions.shared.cs new file mode 100644 index 0000000000..961bed55a2 --- /dev/null +++ b/src/CommunityToolkit.Maui/Extensions/NavigatedToEventArgsExtensions.shared.cs @@ -0,0 +1,16 @@ +using CommunityToolkit.Maui.Views; + +namespace CommunityToolkit.Maui.Extensions; + +/// +/// Extension methods for . +/// +public static class NavigatedToEventArgsExtensions +{ + /// + /// Determines whether the previous page was a Community Toolkit . + /// + /// The current . + /// A boolean indicating whether the previous page was a Community Toolkit . + public static bool WasPreviousPageACommunityToolkitPopupPage(this NavigatedToEventArgs args) => args.PreviousPage is PopupPage; +} \ No newline at end of file