Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Trigger pop logic when you replace back button with icon #12320

Merged
merged 6 commits into from
Oct 20, 2020
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
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;


#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 12126, "[iOS] TabBarIsVisible = True/False breaking for multiple nested pages",
PlatformAffected.iOS)]
#if UITEST
[NUnit.Framework.Category(Core.UITests.UITestCategories.Github10000)]
[NUnit.Framework.Category(UITestCategories.Shell)]
#endif
public class Issue12126 : TestShell
{
bool firstNavigated = true;
protected override void Init()
{
var page1 = AddFlyoutItem("Tab 1");
AddBottomTab("Tab 2");
Shell.SetTabBarIsVisible(page1, true);
}

protected override async void OnNavigated(ShellNavigatedEventArgs args)
{
base.OnNavigated(args);

if(firstNavigated)
{
firstNavigated = false;
ContentPage contentPage = new ContentPage();
contentPage.Content = new Label()
{
Text = "If you don't see any bottom tabs the test has failed"
};
Shell.SetTabBarIsVisible(contentPage, true);

ContentPage contentPage2 = new ContentPage();
contentPage2.Content =
new StackLayout()
{
Children =
{
new Label()
{
Text = "Click The Back Arrow",
AutomationId = "TestReady"
}
}
};

Shell.SetTabBarIsVisible(contentPage2, false);
await Navigation.PushAsync(contentPage);
await Navigation.PushAsync(contentPage2);
}
}


#if UITEST && __SHELL__
[Test]
public void NavigatingBackFromMultiplePushPagesChangesTabVisibilityCorrectly()
{
RunningApp.WaitForElement("TestReady");
TapBackArrow();
RunningApp.WaitForElement("Tab 1");
}
#endif
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;


#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 12320, "[iOS] TabBarIsVisible = True/False doesn't work on Back Navigation When using BackButtonBehavior",
PlatformAffected.iOS)]
#if UITEST
[NUnit.Framework.Category(Core.UITests.UITestCategories.Github10000)]
[NUnit.Framework.Category(UITestCategories.Shell)]
#endif
public class Issue12320 : TestShell
{
bool firstNavigated = true;
protected override void Init()
{
var page1 = new ContentPage();
page1.Content = new Label()
{
Text = "If you don't see any bottom tabs the test has failed"
};

AddFlyoutItem(page1, "Tab 1");
AddBottomTab("Tab 2");
Shell.SetTabBarIsVisible(page1, true);
}

protected override async void OnNavigated(ShellNavigatedEventArgs args)
{
base.OnNavigated(args);

if(firstNavigated)
{
firstNavigated = false;
ContentPage contentPage = new ContentPage();
contentPage.Content = new Label()
{
Text = "Click the Coffee Cup in the Nav Bar",
AutomationId = "TestReady"
};

Shell.SetTabBarIsVisible(contentPage, false);
Shell.SetBackButtonBehavior(contentPage, new BackButtonBehavior() { IconOverride = "coffee.png" });
await Navigation.PushAsync(contentPage);
}
}


#if UITEST && __SHELL__
[Test]
public void PopLogicExecutesWhenUsingBackButtonBehavior()
{
RunningApp.WaitForElement("TestReady");
base.TapBackArrow();
RunningApp.WaitForElement("Tab 1");
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)CollectionViewGroupTypeIssue.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12320.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12153.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue10324.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Github9536.xaml.cs">
Expand Down Expand Up @@ -1593,6 +1594,7 @@
<Compile Include="$(MSBuildThisFileDirectory)HeaderFooterShellFlyout.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12134.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11963.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue12126.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11831.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue11496.xaml.cs">
<DependentUpon>Issue11496.xaml</DependentUpon>
Expand Down
9 changes: 1 addition & 8 deletions Xamarin.Forms.Platform.iOS/Renderers/ShellItemRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ void IAppearanceObserver.OnAppearanceChanged(ShellAppearance appearance)
Page _displayedPage;
bool _disposed;
ShellItem _shellItem;
bool _switched = true;

IShellSectionRenderer CurrentRenderer { get; set; }

Expand Down Expand Up @@ -330,7 +329,6 @@ void GoTo(ShellSection shellSection)
if (_currentSection != null)
{
((IShellSectionController)_currentSection).AddDisplayedPageObserver(this, OnDisplayedPageChanged);
_switched = true;
}
}

Expand All @@ -347,12 +345,7 @@ void OnDisplayedPageChanged(Page page)
if (_displayedPage != null)
{
_displayedPage.PropertyChanged += OnDisplayedPagePropertyChanged;

if (!_currentSection.Stack.Contains(_displayedPage) || _switched)
{
_switched = false;
UpdateTabBarHidden();
}
UpdateTabBarHidden();
}
}

Expand Down
24 changes: 12 additions & 12 deletions Xamarin.Forms.Platform.iOS/Renderers/ShellPageRendererTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,7 @@ async Task UpdateLeftToolbarItems()

if (String.IsNullOrWhiteSpace(text) && image == null)
{
Element item = Page;
while (!Application.IsApplicationOrNull(item))
{
if (item is IShellController shell)
{
image = shell.FlyoutIcon;
item = null;
}
item = item?.Parent;
}
image = _context.Shell.FlyoutIcon;
}

if (image != null)
Expand Down Expand Up @@ -284,9 +275,16 @@ async Task UpdateLeftToolbarItems()
if (NavigationItem.LeftBarButtonItem != null)
{
if (String.IsNullOrWhiteSpace(image?.AutomationId))
NavigationItem.LeftBarButtonItem.AccessibilityIdentifier = "OK";
{
if (IsRootPage)
NavigationItem.LeftBarButtonItem.AccessibilityIdentifier = "OK";
PureWeen marked this conversation as resolved.
Show resolved Hide resolved
else
NavigationItem.LeftBarButtonItem.AccessibilityIdentifier = "Back";
}
else
{
NavigationItem.LeftBarButtonItem.AccessibilityIdentifier = image.AutomationId;
}

if (image != null)
{
Expand All @@ -309,7 +307,9 @@ void LeftBarButtonItemHandler(UIViewController controller, bool isRootPage)
}
else if (!isRootPage)
{
if (controller?.ParentViewController is UINavigationController navigationController)
if (controller?.ParentViewController is ShellSectionRenderer ssr)
ssr.SendPop();
else if (controller?.ParentViewController is UINavigationController navigationController)
navigationController.PopViewController(true);
}
else if(_flyoutBehavior == FlyoutBehavior.Flyout)
Expand Down
7 changes: 5 additions & 2 deletions Xamarin.Forms.Platform.iOS/Renderers/ShellSectionRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ public ShellSectionRenderer(IShellContext context)

[Export("navigationBar:shouldPopItem:")]
[Internals.Preserve(Conditional = true)]
public bool ShouldPopItem(UINavigationBar navigationBar, UINavigationItem item)
{
public bool ShouldPopItem(UINavigationBar navigationBar, UINavigationItem item) =>
SendPop();

internal bool SendPop()
{
// this means the pop is already done, nothing we can do
if (ViewControllers.Length < NavigationBar.Items.Length)
return true;
Expand Down