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

[Android] Fix crash setting SelectedTabColor on TabbedPage #12924

Merged
merged 5 commits into from
Feb 9, 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
31 changes: 20 additions & 11 deletions src/Controls/src/Core/Platform/Android/TabbedPageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,41 +512,50 @@ internal void UpdateBarBackground()

protected virtual ColorStateList GetItemTextColorStates()
{
if (_originalTabTextColors == null)
_originalTabTextColors = (IsBottomTabPlacement) ? _bottomNavigationView.ItemTextColor : _tabLayout.TabTextColors;
if (_originalTabTextColors is null)
_originalTabTextColors = IsBottomTabPlacement ? _bottomNavigationView.ItemTextColor : _tabLayout.TabTextColors;

Color barItemColor = BarItemColor;
Color barTextColor = Element.BarTextColor;
Color barSelectedItemColor = BarSelectedItemColor;

if (barItemColor == null && barTextColor == null && barSelectedItemColor == null)
if (barItemColor is null && barTextColor is null && barSelectedItemColor is null)
return _originalTabTextColors;

if (_newTabTextColors != null)
if (_newTabTextColors is not null)
return _newTabTextColors;

int checkedColor;
int defaultColor;

if (barTextColor != null)
// The new default color to use may have a color if BarItemColor is not null or the original colors for text
// are not null either. If it does not happens, this variable will be null and the ColorStateList of the
// original colors is used.
int? defaultColor = null;

if (barTextColor is not null)
{
checkedColor = barTextColor.ToPlatform().ToArgb();
defaultColor = checkedColor;
}
else
{
defaultColor = barItemColor.ToPlatform().ToArgb();
if (barItemColor is not null)
defaultColor = barItemColor.ToPlatform().ToArgb();

if (barItemColor == null && _originalTabTextColors != null)
if (barItemColor is null && _originalTabTextColors is not null)
defaultColor = _originalTabTextColors.DefaultColor;

checkedColor = defaultColor;
if (!defaultColor.HasValue)
return _originalTabTextColors;
else
checkedColor = defaultColor.Value;

if (barSelectedItemColor != null)
if (barSelectedItemColor is not null)
checkedColor = barSelectedItemColor.ToPlatform().ToArgb();
}

_newTabTextColors = GetColorStateList(defaultColor, checkedColor);
_newTabTextColors = GetColorStateList(defaultColor.Value, checkedColor);

jsuarezruiz marked this conversation as resolved.
Show resolved Hide resolved
return _newTabTextColors;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using AndroidX.ViewPager2.Widget;
using Microsoft.Maui.Controls;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Graphics;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public partial class TabbedPageTests
{
[Fact(DisplayName = "Using SelectedTab Color doesnt crash")]
public async Task SelectedTabColorNoDoesntCrash()
{
SetupBuilder();

var tabbedPage = CreateBasicTabbedPage();
tabbedPage.SelectedTabColor = Colors.Red;

await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(tabbedPage), (handler) =>
{
var platformView = tabbedPage.Handler.PlatformView as ViewPager2;
Assert.NotNull(platformView);
return Task.CompletedTask;
});
}
}
}