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

Commit

Permalink
[GH-6184] - Make able to disable tabs inside the MoreViewController […
Browse files Browse the repository at this point in the history
…iOS] (#9781)

* Fixed the mess-up

* Fix merge conflict and UITest

* Update Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6184.xaml.cs

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>

* Added information on how to run the test

* Improviments in the code and prevent crash on iOS14 or newer

* make sure that Forms is into the commit

* code style fix

* - fix for iOS14

* - change pages

* - fix delegate

* - fix cast exception

* - retain default text color

* - add page numbers

* - fix

* - reduce number of more

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
  • Loading branch information
pictos and PureWeen committed Nov 16, 2020
1 parent 357801d commit 9090bcc
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8" ?>
<controls:TestShell
x:Class="Xamarin.Forms.Controls.Issues.Issue6184"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:Xamarin.Forms.Controls"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:local="clr-namespace:Xamarin.Forms.Controls.Issues"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<ShellItem>
<ShellSection Title="Issue 1" IsEnabled="False">
<ShellContent>
<local:PageInstruction PageNumber="1" />
</ShellContent>
</ShellSection>
<ShellSection Title="Issue 2" IsEnabled="False">
<ShellContent>
<local:PageInstruction PageNumber="2" />
</ShellContent>
</ShellSection>
<ShellSection Title="Issue 3" IsEnabled="False">
<ShellContent>
<local:PageInstruction PageNumber="3" />
</ShellContent>
</ShellSection>
<ShellSection Title="Issue 4" IsEnabled="False">
<ShellContent>
<local:PageInstruction PageNumber="4" />
</ShellContent>
</ShellSection>
<ShellSection Title="Issue 5" IsEnabled="False">
<ShellContent>
<local:PageInstruction PageNumber="4" />
</ShellContent>
</ShellSection>
<ShellSection Title="Issue 6">
<ShellContent>
<local:PageInstruction PageNumber="6" />
</ShellContent>
</ShellSection>
<ShellSection Title="Issue 7">
<ShellContent>
<local:PageInstruction PageNumber="7" />
</ShellContent>
</ShellSection>
<ShellSection Title="Issue 8">
<ShellContent>
<local:PageInstruction PageNumber="8" />
</ShellContent>
</ShellSection>
<ShellSection Title="Issue 9" IsEnabled="False">
<ShellContent>
<local:PageInstruction PageNumber="9" />
</ShellContent>
</ShellSection>
<ShellSection Title="Issue 18" IsEnabled="False">
<ShellContent>
<local:PageInstruction PageNumber="18" />
</ShellContent>
</ShellSection>
</ShellItem>
</controls:TestShell>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

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

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.Shell)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 6184, "Throws exception when set isEnabled to false in ShellItem index > 5", PlatformAffected.iOS)]
public partial class Issue6184 : TestShell
{
public Issue6184()
{
#if APP

InitializeComponent();
#endif
}

protected override void Init()
{
}

#if UITEST && __IOS__
[Test]
public void GitHubIssue6184()
{
RunningApp.WaitForElement(q => q.Marked("More"));
RunningApp.Tap(q => q.Marked("More"));
RunningApp.Tap(q => q.Marked("Issue 5"));
RunningApp.WaitForElement(q => q.Marked("Issue 5"));
}
#endif
}

[Preserve(AllMembers = true)]
public class PageInstruction : ContentPage
{

Label pageNumber;
public int PageNumber
{
set
{
pageNumber.Text = $"Page Number: {value}";
}
}

public PageInstruction()
{
pageNumber = new Label();
var stack = new StackLayout();
var label = new Label
{
Text = "Press the more page, and see if the Cells with Title \"Issue 5\", \"Issue 9\", \"Issue 18\" are Disabled. If don't the test fails",
FontSize = 20
};
stack.Children.Add(label);
stack.Children.Add(pageNumber);
Content = stack;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue3262.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8308.xaml.cs">
<DependentUpon>Issue8308.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue6184.xaml.cs">
<DependentUpon>Issue6184.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue8715.xaml.cs">
Expand Down Expand Up @@ -2402,6 +2405,12 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue6184.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue8715.xaml">
<SubType>Designer</SubType>
Expand Down
9 changes: 5 additions & 4 deletions Xamarin.Forms.Platform.iOS/Forms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ internal static bool IsiOS14OrNewer
}
}


internal static bool RespondsToSetNeedsUpdateOfHomeIndicatorAutoHidden
{
get
Expand Down Expand Up @@ -162,7 +163,7 @@ public static void SetFlags(params string[] flags)
}

s_flags = (string[])flags.Clone();
if (s_flags.Contains ("Profile"))
if (s_flags.Contains("Profile"))
Profile.Enable();
}

Expand Down Expand Up @@ -736,9 +737,9 @@ public SizeRequest GetNativeSize(VisualElement view, double widthConstraint, dou
}

public OSAppTheme RequestedTheme
{
get
{
{
get
{
#if __IOS__ || __TVOS__
if (!IsiOS13OrNewer)
return OSAppTheme.Unspecified;
Expand Down
46 changes: 41 additions & 5 deletions Xamarin.Forms.Platform.iOS/Renderers/ShellItemRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void IAppearanceObserver.OnAppearanceChanged(ShellAppearance appearance)
Page _displayedPage;
bool _disposed;
ShellItem _shellItem;
static UIColor _defaultMoreTextLabelTextColor;

IShellSectionRenderer CurrentRenderer { get; set; }

Expand Down Expand Up @@ -76,18 +77,22 @@ public override UIViewController SelectedViewController
{
MoreNavigationController.WeakDelegate = this;
}

UpdateMoreCellsEnabled();
}
}

[Export("navigationController:didShowViewController:animated:")]
public virtual void DidShowViewController(UINavigationController navigationController, [Transient]UIViewController viewController, bool animated)
[Preserve(AllMembers = true)]
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;
}
UpdateMoreCellsEnabled();
}

public override void ViewDidLayoutSubviews()
Expand Down Expand Up @@ -271,7 +276,7 @@ void AddRenderer(IShellSectionRenderer renderer)

void CreateTabRenderers()
{
if(ShellItem.CurrentItem == null)
if (ShellItem.CurrentItem == null)
throw new InvalidOperationException($"Content not found for active {ShellItem}. Title: {ShellItem.Title}. Route: {ShellItem.Route}.");

var items = ShellItemController.GetItems();
Expand All @@ -298,18 +303,49 @@ void CreateTabRenderers()

// Make sure we are at the right item
GoTo(ShellItem.CurrentItem);
UpdateMoreCellsEnabled();
}

void UpdateMoreCellsEnabled()
{
var moreNavigationCells = GetMoreNavigationCells();
var viewControllersLength = ViewControllers.Length;
// now that they are applied we can set the enabled state of the TabBar items
for (i = 0; i < ViewControllers.Length; i++)
for (int i = 4; i < viewControllersLength; i++)
{
if((i - 4) >= (moreNavigationCells.Length))
{
break;
}

var renderer = RendererForViewController(ViewControllers[i]);
var cell = moreNavigationCells[i - 4];

if (!renderer.ShellSection.IsEnabled)
{
TabBar.Items[i].Enabled = false;
cell.UserInteractionEnabled = false;

if (_defaultMoreTextLabelTextColor == null)
_defaultMoreTextLabelTextColor = cell.TextLabel.TextColor;

cell.TextLabel.TextColor = Color.FromRgb(213, 213, 213).ToUIColor();
}
else if(!cell.UserInteractionEnabled)
{
cell.UserInteractionEnabled = true;
cell.TextLabel.TextColor = _defaultMoreTextLabelTextColor;
}
}

UITableViewCell[] GetMoreNavigationCells()
{
if(MoreNavigationController.TopViewController.View is UITableView uITableView)
return uITableView.VisibleCells;

return new UITableViewCell[0];
}
}

void GoTo(ShellSection shellSection)
{
if (shellSection == null || _currentSection == shellSection)
Expand Down

0 comments on commit 9090bcc

Please sign in to comment.