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

[net7.0] Fix count comparison when switching tabs #12886

Merged
merged 1 commit into from
Jan 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ protected virtual void OnPageSelected(int position)
// This mainly happens if all of the items that are part of this shell section
// vanish. Android calls `OnPageSelected` with position zero even though the view pager is
// empty
if (visibleItems.Count >= position)
if (position >= visibleItems.Count)
return;

var shellContent = visibleItems[position];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Android.Views;
using AndroidX.AppCompat.Widget;
using AndroidX.DrawerLayout.Widget;
using AndroidX.ViewPager2.Widget;
using Google.Android.Material.AppBar;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers.Compatibility;
Expand Down Expand Up @@ -361,5 +362,26 @@ RecyclerViewContainer GetFlyoutMenuReyclerView(ShellRenderer shellRenderer)

return flyoutContainer ?? throw new Exception("RecyclerView not found");
}

async Task TapToSelect(ContentPage page)
{
var shellContent = page.Parent as ShellContent;
var shellSection = shellContent.Parent as ShellSection;
var shellItem = shellSection.Parent as ShellItem;
var shell = shellItem.Parent as Shell;
await OnNavigatedToAsync(shell.CurrentPage);

if (shellItem != shell.CurrentItem)
throw new NotImplementedException();

if (shellSection != shell.CurrentItem.CurrentItem)
throw new NotImplementedException();

var pagerParent = (shell.CurrentPage.Handler as IPlatformViewHandler)
.PlatformView.GetParentOfType<ViewPager2>();

pagerParent.CurrentItem = shellSection.Items.IndexOf(shellContent);
await OnNavigatedToAsync(page);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -580,5 +580,55 @@ await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
Assert.Equal((rootView.SelectedItem as NavigationViewItemViewModel).Data, flyoutItems[0][1]);
});
}

async Task TapToSelect(ContentPage page)
{
var shellContent = page.Parent as ShellContent;
var shellSection = shellContent.Parent as ShellSection;
var shellItem = shellSection.Parent as ShellItem;
var shell = shellItem.Parent as Shell;

await OnNavigatedToAsync(shell.CurrentPage);

if (shellItem != shell.CurrentItem)
throw new NotImplementedException();

if (shellSection != shell.CurrentItem.CurrentItem)
throw new NotImplementedException();

var mauiNavigationView = shellItem.Handler.PlatformView as MauiNavigationView;
var navSource = mauiNavigationView.MenuItemsSource as IEnumerable;

bool found = false;
foreach (NavigationViewItemViewModel item in navSource)
{
if (item.Data == shellContent)
{
mauiNavigationView.SelectedItem = item;
found = true;
break;
}
else if (item.MenuItemsSource is IEnumerable children)
{
foreach (NavigationViewItemViewModel childContent in children)
{
if (childContent.Data == shellContent)
{
mauiNavigationView.SelectedItem = childContent;
found = true;
break;
}
}
}

if (found)
break;
}

if (!found)
throw new InvalidOperationException("Unable to locate page inside platform shell components");

await OnNavigatedToAsync(page);
}
}
}
39 changes: 39 additions & 0 deletions src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,45 @@ await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
});
}

[Fact(DisplayName = "LifeCycleEvents Fire When Navigating Top Tabs")]
public async Task LifeCycleEventsFireWhenNavigatingTopTabs()
{
SetupBuilder();

var page1 = new LifeCycleTrackingPage() { Content = new Label() { Padding = 40, Text = "Page 1", Background = SolidColorBrush.Purple } };
var page2 = new LifeCycleTrackingPage() { Content = new Label() { Padding = 40, Text = "Page 2", Background = SolidColorBrush.Green } };

var shell = await CreateShellAsync((shell) =>
{
shell.Items.Add(new Tab()
{
Items =
{
new ShellContent()
{
Title = "Tab 1",
Content = page1
},
new ShellContent()
{
Title = "Tab 2",
Content = page2
},
}
});
});

await CreateHandlerAndAddToWindow<ShellHandler>(shell, async (handler) =>
{
await OnNavigatedToAsync(page1);

Assert.Equal(0, page2.OnNavigatedToCount);
await TapToSelect(page2);
Assert.Equal(page2.Parent, shell.CurrentItem.CurrentItem.CurrentItem);
page2.AssertLifeCycleCounts();
});
}

protected Task<Shell> CreateShellAsync(Action<Shell> action) =>
InvokeOnMainThreadAsync(() =>
{
Expand Down
44 changes: 42 additions & 2 deletions src/Controls/tests/DeviceTests/Elements/Shell/ShellTests.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Foundation;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Handlers.Compatibility;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Controls.Platform.Compatibility;
using Microsoft.Maui.Controls.PlatformConfiguration;
using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;
using Microsoft.Maui.Platform;
using UIKit;
using Xunit;

namespace Microsoft.Maui.DeviceTests
Expand All @@ -32,7 +35,7 @@ public async Task SwipingAwayModalPropagatesToShell()
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
{
var modalPage = new ContentPage();
modalPage.On<iOS>().SetModalPresentationStyle(UIModalPresentationStyle.FormSheet);
modalPage.On<iOS>().SetModalPresentationStyle(Controls.PlatformConfiguration.iOSSpecific.UIModalPresentationStyle.FormSheet);
var platformWindow = MauiContext.GetPlatformWindow().RootViewController;

await shell.Navigation.PushModalAsync(modalPage);
Expand Down Expand Up @@ -73,7 +76,7 @@ public async Task SwipingAwayModalRemovesEntireNavigationPage()
await CreateHandlerAndAddToWindow<ShellRenderer>(shell, async (handler) =>
{
var modalPage = new Controls.NavigationPage(new ContentPage());
modalPage.On<iOS>().SetModalPresentationStyle(UIModalPresentationStyle.FormSheet);
modalPage.On<iOS>().SetModalPresentationStyle(Controls.PlatformConfiguration.iOSSpecific.UIModalPresentationStyle.FormSheet);
var platformWindow = MauiContext.GetPlatformWindow().RootViewController;

await shell.Navigation.PushModalAsync(modalPage);
Expand Down Expand Up @@ -197,6 +200,43 @@ void ShellNavigating(object sender, ShellNavigatingEventArgs e)
});
}

async Task TapToSelect(ContentPage page)
{
var shellContent = page.Parent as ShellContent;
var shellSection = shellContent.Parent as ShellSection;
var shellItem = shellSection.Parent as ShellItem;
var shell = shellItem.Parent as Shell;
await OnNavigatedToAsync(shell.CurrentPage);

if (shellItem != shell.CurrentItem)
throw new NotImplementedException();

if (shellSection != shell.CurrentItem.CurrentItem)
throw new NotImplementedException();

var pagerParent = (shell.CurrentPage.Handler as IPlatformViewHandler)
.PlatformView.FindParent(x => x.NextResponder is UITabBarController);

var tabController = pagerParent.NextResponder as ShellItemRenderer;

var section = tabController.SelectedViewController as ShellSectionRenderer;

var rootCV = section.ViewControllers[0] as
ShellSectionRootRenderer;

var rootHeader = rootCV.ChildViewControllers
.OfType<ShellSectionRootHeader>()
.First();

var newIndex = shellSection.Items.IndexOf(shellContent);

await Task.Delay(100);

rootHeader.ItemSelected(rootHeader.CollectionView, NSIndexPath.FromItemSection((int)newIndex, 0));

await OnNavigatedToAsync(page);
}

class ModalShellPage : ContentPage
{
public ModalShellPage()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public class LifeCycleTrackingPage : ContentPage
{
public NavigatedFromEventArgs NavigatedFromArgs { get; private set; }
public NavigatingFromEventArgs NavigatingFromArgs { get; private set; }
public NavigatedToEventArgs NavigatedToArgs { get; private set; }
public int AppearingCount { get; private set; }
public int DisappearingCount { get; private set; }
public int OnNavigatedToCount { get; private set; }
public int OnNavigatingFromCount { get; private set; }
public int OnNavigatedFromCount { get; private set; }

public void ClearNavigationArgs()
{
NavigatedFromArgs = null;
NavigatingFromArgs = null;
NavigatedToArgs = null;
}

protected override void OnAppearing()
{
base.OnAppearing();
AppearingCount++;
}

protected override void OnDisappearing()
{
base.OnDisappearing();
DisappearingCount++;
}

protected override void OnNavigatedFrom(NavigatedFromEventArgs args)
{
base.OnNavigatedFrom(args);
NavigatedFromArgs = args;
OnNavigatedFromCount++;
}

protected override void OnNavigatingFrom(NavigatingFromEventArgs args)
{
base.OnNavigatingFrom(args);
NavigatingFromArgs = args;
OnNavigatingFromCount++;
}

protected override void OnNavigatedTo(NavigatedToEventArgs args)
{
base.OnNavigatedTo(args);
NavigatedToArgs = args;
OnNavigatedToCount++;
}

public void AssertLifeCycleCounts()
{
if (!this.HasAppeared)
{
Assert.Equal(AppearingCount, DisappearingCount);
Assert.Equal(DisappearingCount, OnNavigatedToCount);
Assert.Equal(OnNavigatingFromCount, OnNavigatedToCount);
}
else
{
Assert.Equal(AppearingCount, OnNavigatedToCount);
Assert.Equal(DisappearingCount, AppearingCount - 1);
Assert.Equal(OnNavigatingFromCount, AppearingCount - 1);
}

Assert.Equal(DisappearingCount, OnNavigatingFromCount);
}
}
}