Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a couple life cycle scenarios on shell modals #13177

Merged
merged 2 commits into from
Feb 8, 2023
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 src/Controls/src/Core/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public void SendAppearing()
// Visual Hierarchy
// The window/application will take care of re-firing this appearing
// if it needs to
var window = this.FindParentOfType<Window>();
var window = this.FindParentOfType<IWindow>();
if (window?.Parent == null)
{
return;
Expand Down
7 changes: 7 additions & 0 deletions src/Controls/src/Core/Shell/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,14 @@ protected override async Task OnPushModal(Page modal, bool animated)
modal.Parent = (Element)_shell.FindParentOfType<IWindow>();

if (!_shell.CurrentItem.CurrentItem.IsPushingModalStack)
{
if (ModalStack.Count > 0)
{
ModalStack[ModalStack.Count - 1].SendDisappearing();
}

modal.SendAppearing();
}

await base.OnPushModal(modal, animated);

Expand Down
5 changes: 5 additions & 0 deletions src/Controls/src/Core/Shell/ShellSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ async Task PrepareCurrentStackForBeingReplaced(ShellNavigationRequest request, S
continue;
}

// This is the page that we will eventually get to once we've finished
// modifying the existing navigation stack
// So we want to fire appearing on it
navPage.SendAppearing();

IsPoppingModalStack = true;

while (navStack.Count > popCount && Navigation.ModalStack.Count > 0)
Expand Down
2 changes: 0 additions & 2 deletions src/Controls/tests/Core.UnitTests/ShellLifeCycleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ public void AppearingOnShellItemChanged()
Assert.False(state.ContentAppearing);
}


[Fact]
public async Task ShellPartWithModalPush()
{
Expand All @@ -336,7 +335,6 @@ public async Task ShellPartWithModalPush()
lifeCycleState.AllTrue();
}


[Fact]
public async Task ShellPartWithPagePush()
{
Expand Down
44 changes: 44 additions & 0 deletions src/Controls/tests/Core.UnitTests/ShellModalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,50 @@ namespace Microsoft.Maui.Controls.Core.UnitTests

public class ShellModalTests : ShellTestBase
{
[Fact]
public async Task AppearingAndDisappearingFireOnMultipleModals()
{
var windowPage = new ContentPage();
var modalPage1 = new ContentPage();
var modalPage2 = new ContentPage();

int modal1Appearing = 0;
int modal1Disappearing = 0;
int modal2Appearing = 0;
int modal2Disappearing = 0;
int windowAppearing = 0;
int windowDisappearing = 0;

modalPage1.Appearing += (_, _) => modal1Appearing++;
modalPage1.Disappearing += (_, _) => modal1Disappearing++;

modalPage2.Appearing += (_, _) => modal2Appearing++;
modalPage2.Disappearing += (_, _) => modal2Disappearing++;

windowPage.Appearing += (_, _) => windowAppearing++;
windowPage.Disappearing += (_, _) => windowDisappearing++;

var window = new TestWindow(new TestShell() { CurrentItem = windowPage });
await windowPage.Navigation.PushModalAsync(modalPage1);
Assert.Equal(1, modal1Appearing);

await windowPage.Navigation.PushModalAsync(modalPage2);
Assert.Equal(1, modal2Appearing);
Assert.Equal(1, modal1Disappearing);

await windowPage.Navigation.PopModalAsync();
await windowPage.Navigation.PopModalAsync();

Assert.Equal(2, modal1Appearing);
Assert.Equal(2, modal1Disappearing);

Assert.Equal(1, modal2Appearing);
Assert.Equal(1, modal2Disappearing);

Assert.Equal(2, windowAppearing);
Assert.Equal(1, windowDisappearing);
}

[Fact]
public async Task BasicModalBehaviorTest()
{
Expand Down