Skip to content

Commit

Permalink
[Android] #fixes 2279 - watch for changes to page title/icon and prop…
Browse files Browse the repository at this point in the history
…agate those to tabs (xamarin#3146)
  • Loading branch information
PureWeen authored and rmarinho committed Jun 27, 2018
1 parent dfa2606 commit e5d8d61
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using AToolbarPlacement = Xamarin.Forms.PlatformConfiguration.AndroidSpecific.ToolbarPlacement;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;

#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, 1898, "TabbedPage Page not watching icon changes", PlatformAffected.Android, issueTestNumber: 1)]
public class Issue1898 : TestTabbedPage
{
protected override void Init() =>
Issue1898Setup.SetupTabbedPage(this, AToolbarPlacement.Top);


#if UITEST
[Test]
public void TabIconsAndTitlesChange() =>
Issue1898Setup.RunUITests(RunningApp);
#endif
}

[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 1898, "TabbedPage Page not watching icon changes", issueTestNumber: 2)]
public partial class Issue1898_2 : TestTabbedPage
{
protected override void Init() =>
Issue1898Setup.SetupTabbedPage(this, AToolbarPlacement.Bottom);

#if UITEST && __ANDROID__
[Test]
public void TabBottomIconsAndTitlesChange() =>
Issue1898Setup.RunUITests(RunningApp);
#endif
}

[Preserve(AllMembers = true)]
internal class Issue1898Setup
{
#if UITEST
internal static void RunUITests(IApp RunningApp)
{
RunningApp.WaitForElement(Title1);
RunningApp.Tap(ChangeTitle1);
RunningApp.Tap(ChangeIcon1);
RunningApp.Tap(ChangeIconPage2);
RunningApp.Tap(ChangeIcon1Null);
RunningApp.WaitForElement(ChangeTitle1);


RunningApp.Tap(Title2);
RunningApp.Tap(ChangeTitle2);
RunningApp.Tap(ChangeIcon2);
RunningApp.Tap(ChangeIconPage1);
RunningApp.Tap(ChangeIcon2Null);
RunningApp.WaitForElement(ChangeTitle2);
}
#endif

const string Title1 = "Title 1";
const string ChangeTitle1 = "Change Title 1";
const string ChangeIcon1 = "Change Icon 1";
const string ChangeIconPage2 = "Change Icon on Page 2";
const string ChangeIcon1Null = "Null Icon 1";

const string Title2 = "Title 2";
const string ChangeTitle2 = "Change Title 2";
const string ChangeIcon2 = "Change Icon 2";
const string ChangeIconPage1 = "Change Icon on Page 1";
const string ChangeIcon2Null = "Null Icon 2";

internal static void SetupTabbedPage(TabbedPage tabbedPage, AToolbarPlacement placement)
{
ContentPage Issue1898PageOne = new ContentPage() { Title = Title1, Icon = "bank.png" };
ContentPage Issue1898PageTwo = new ContentPage() { Title = Title2, Icon = "bank.png" };

Issue1898PageOne.Content =
new StackLayout
{
Margin = 20,
Children =
{
new Label(){ Text = "Click through each button on each tab to make sure they do what they say they do" },
new Button(){ Text = ChangeTitle1, Command = new Command(() => Issue1898PageOne.Title = ChangeTitle1) },
new Button(){ Text = ChangeIcon1, Command = new Command(() => Issue1898PageOne.Icon = "coffee.png")},
new Button(){ Text = ChangeIconPage2, Command = new Command(() => Issue1898PageTwo.Icon = "coffee.png")},
new Button(){ Text = ChangeIcon1Null, Command = new Command(() => Issue1898PageOne.Icon = null)},
}
};

Issue1898PageTwo.Content =
new StackLayout
{
Margin = 20,
Children =
{
new Button(){ Text = ChangeTitle2, Command = new Command(() => Issue1898PageTwo.Title = ChangeTitle2) },
new Button(){ Text = ChangeIcon2, Command = new Command(() => Issue1898PageTwo.Icon = "bank.png")},
new Button(){ Text = ChangeIconPage1, Command = new Command(() => Issue1898PageOne.Icon = "calculator.png")},
new Button(){ Text = ChangeIcon2Null, Command = new Command(() => Issue1898PageTwo.Icon = null)},
}
};

tabbedPage.Children.Add(Issue1898PageOne);
tabbedPage.Children.Add(Issue1898PageTwo);

tabbedPage.On<Android>().SetBarItemColor(Color.Blue);
tabbedPage.On<Android>().SetBarSelectedItemColor(Color.Green);
tabbedPage.On<Android>().SetToolbarPlacement(placement);
tabbedPage.On<Windows>().SetHeaderIconsEnabled(true);
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue1888.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1891.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1895.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1898.xaml.cs">
<DependentUpon>Issue1898.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue1905.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1914.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue194.cs" />
Expand Down Expand Up @@ -745,6 +742,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla40092.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1426.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1733.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1898.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1583_1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1323.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2399.cs" />
Expand Down Expand Up @@ -786,9 +784,6 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue1766.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue1898.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue2282.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
Expand Down
24 changes: 22 additions & 2 deletions Xamarin.Forms.Platform.Android/AppCompat/TabbedPageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,23 @@ void OnPagePropertyChanged(object sender, PropertyChangedEventArgs e)
tab.SetText(page.Title);
}
}
else if (e.PropertyName == Page.IconProperty.PropertyName)
{
var page = (Page)sender;
var index = Element.Children.IndexOf(page);
FileImageSource icon = page.Icon;

if (IsBottomTabPlacement)
{
var menuItem = _bottomNavigationView.Menu.GetItem(index);
menuItem.SetIcon(GetIconDrawable(icon));
}
else
{
TabLayout.Tab tab = _tabLayout.GetTabAt(index);
SetTabIcon(tab, icon);
}
}
}

void ScrollToCurrentPage()
Expand Down Expand Up @@ -601,7 +618,7 @@ void UpdateBottomNavigationViewIcons()
continue;

var menuItem = bottomNavigationView.Menu.GetItem(i);
menuItem.SetIcon(ResourceManager.IdFromTitle(icon, ResourceManager.DrawableClass));
menuItem.SetIcon(GetIconDrawable(icon));
}
}

Expand All @@ -627,9 +644,12 @@ void UpdateTabIcons()
}
}

Drawable GetIconDrawable(FileImageSource icon) =>
Context.GetDrawable(icon);

protected virtual void SetTabIcon(TabLayout.Tab tab, FileImageSource icon)
{
tab.SetIcon(ResourceManager.IdFromTitle(icon, ResourceManager.DrawableClass));
tab.SetIcon(GetIconDrawable(icon));
this.SetIconColorFilter(tab);
}

Expand Down

0 comments on commit e5d8d61

Please sign in to comment.