Skip to content

Commit

Permalink
[net7.0] [Android] Fix crash setting SelectedTabColor on TabbedPage (#…
Browse files Browse the repository at this point in the history
…13396)

* Fix crash setting SelectedTabColor on Android TabbedPage

* Update src/Controls/src/Core/Platform/Android/TabbedPageManager.cs

Co-authored-by: Manuel de la Pena <mandel@microsoft.com>

* Refactoring code

* Small refactoring and included comments

* Changes based on feedback

---------

Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>
Co-authored-by: Manuel de la Pena <mandel@microsoft.com>
  • Loading branch information
3 people authored Feb 16, 2023
1 parent 54c209e commit b7f7c0b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
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 @@ -511,41 +511,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);

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;
});
}
}
}

0 comments on commit b7f7c0b

Please sign in to comment.