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

Use the MoreNavigationController DidShowViewController callback for shell sections #6786

Merged
merged 3 commits into from
Jan 31, 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,91 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

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

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 6784, "ShellItem.CurrentItem is not set when selecting shell section aggregated in more tab", PlatformAffected.iOS)]
#if UITEST
[NUnit.Framework.Category(UITestCategories.Shell)]
#endif
public class Issue6784 : TestShell
{
protected override void Init()
{
var shellItem = new ShellItem()
{
Title = "ShellItem"
};

Items.Add(shellItem);

AddBottomTab("Tab 1");
AddBottomTab("Tab 2");
AddBottomTab("Tab 3");
AddBottomTab("Tab 4").AutomationId = "Tab 4 Content";
var contentPage5 = AddBottomTab("Tab 5");
AddBottomTab("Tab 6");

shellItem.PropertyChanged += (sender, e) =>
{
if (e.PropertyName == CurrentItemProperty.PropertyName)
{
if (((ShellItem)sender).CurrentItem.AutomationId == "Tab 5")
{
contentPage5.Content = new Label()
{
Text = "Success"
};
}
}
};
}

#if UITEST && __IOS__
[Test]
public void CurrentItemIsSetWhenSelectingShellSectionAggregatedInMoreTab()
{
RunningApp.WaitForElement(x => x.Class("UITabBarButton").Marked("More"));
RunningApp.Tap(x => x.Class("UITabBarButton").Marked("More"));

RunningApp.WaitForElement(x => x.Class("UITableViewCell").Text("Tab 5"));
RunningApp.Tap(x => x.Class("UITableViewCell").Text("Tab 5"));

RunningApp.WaitForElement(x => x.Text("Success"));
}

[Test]
public void MoreControllerOpensOnFirstClick()
{
RunningApp.WaitForElement(x => x.Class("UITabBarButton").Marked("More"));
RunningApp.Tap(x => x.Class("UITabBarButton").Marked("More"));

RunningApp.WaitForElement(x => x.Class("UITableViewCell").Text("Tab 5"));
RunningApp.Tap(x => x.Class("UITableViewCell").Text("Tab 5"));

RunningApp.Tap(x => x.Class("UITabBarButton").Marked("Tab 4"));
RunningApp.WaitForElement("Tab 4 Content");

RunningApp.Tap(x => x.Class("UITabBarButton").Marked("More"));
RunningApp.WaitForElement(x => x.Class("UITableViewCell").Text("Tab 6"));
}

[Test]
public void MoreControllerDoesNotShowEditButton()
{
RunningApp.WaitForElement(x => x.Class("UITabBarButton").Marked("More"));
RunningApp.Tap(x => x.Class("UITabBarButton").Marked("More"));

RunningApp.WaitForElement(x => x.Class("UITableViewCell").Text("Tab 5"));

Assert.AreEqual(RunningApp.Query(x => x.Marked("Edit")).Count(), 0);
}
#endif
}
}
26 changes: 24 additions & 2 deletions Xamarin.Forms.Platform.iOS/Renderers/ShellItemRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using Foundation;
using ObjCRuntime;
using UIKit;

namespace Xamarin.Forms.Platform.iOS
{
public class ShellItemRenderer : UITabBarController, IShellItemRenderer, IAppearanceObserver
public class ShellItemRenderer : UITabBarController, IShellItemRenderer, IAppearanceObserver, IUINavigationControllerDelegate
{
#region IShellItemRenderer

Expand Down Expand Up @@ -68,9 +70,26 @@ public override UIViewController SelectedViewController
{
ShellItem.SetValueFromRenderer(ShellItem.CurrentItemProperty, renderer.ShellSection);
CurrentRenderer = renderer;
MoreNavigationController?.PopToRootViewController(false);
}

if (ReferenceEquals(value, MoreNavigationController))
{
MoreNavigationController.WeakDelegate = this;
}
}
}
}

[Export("navigationController:didShowViewController:animated:")]
public virtual void DidShowViewController(UINavigationController navigationController, [Transient]UIViewController viewController, bool animated)
{
var renderer = RendererForViewController(this.SelectedViewController);
if (renderer != null)
{
ShellItem.SetValueFromRenderer(ShellItem.CurrentItemProperty, renderer.ShellSection);
CurrentRenderer = renderer;
}
}

public override void ViewDidLayoutSubviews()
{
Expand Down Expand Up @@ -145,6 +164,7 @@ protected virtual void OnItemsCollectionChanged(object sender, NotifyCollectionC
if (renderer != null)
{
ViewControllers = ViewControllers.Remove(renderer.ViewController);
CustomizableViewControllers = Array.Empty<UIViewController>();
RemoveRenderer(renderer);
}
}
Expand Down Expand Up @@ -181,6 +201,7 @@ protected virtual void OnItemsCollectionChanged(object sender, NotifyCollectionC
}

ViewControllers = viewControllers;
CustomizableViewControllers = Array.Empty<UIViewController>();

if (goTo)
GoTo(ShellItem.CurrentItem);
Expand Down Expand Up @@ -246,6 +267,7 @@ void CreateTabRenderers()
viewControllers[i++] = renderer.ViewController;
}
ViewControllers = viewControllers;
CustomizableViewControllers = Array.Empty<UIViewController>();

// No sense showing a bar that has a single icon
if (ViewControllers.Length == 1)
Expand Down