From 23bcc052057ac707942be3b793e041e8a2ba7ecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Thu, 26 Jan 2023 12:48:22 +0100 Subject: [PATCH 1/5] Fix crash setting SelectedTabColor on Android TabbedPage --- .../Platform/Android/TabbedPageManager.cs | 8 +++-- .../TabbedPage/TabbedPageTests.Android.cs | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 src/Controls/tests/DeviceTests/Elements/TabbedPage/TabbedPageTests.Android.cs diff --git a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs index c77758be10e8..5da69409a80d 100644 --- a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs +++ b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs @@ -513,7 +513,7 @@ internal void UpdateBarBackground() protected virtual ColorStateList GetItemTextColorStates() { if (_originalTabTextColors == null) - _originalTabTextColors = (IsBottomTabPlacement) ? _bottomNavigationView.ItemTextColor : _tabLayout.TabTextColors; + _originalTabTextColors = IsBottomTabPlacement ? _bottomNavigationView.ItemTextColor : _tabLayout.TabTextColors; Color barItemColor = BarItemColor; Color barTextColor = Element.BarTextColor; @@ -526,7 +526,7 @@ protected virtual ColorStateList GetItemTextColorStates() return _newTabTextColors; int checkedColor; - int defaultColor; + int defaultColor = 0; if (barTextColor != null) { @@ -535,7 +535,8 @@ protected virtual ColorStateList GetItemTextColorStates() } else { - defaultColor = barItemColor.ToPlatform().ToArgb(); + if (barItemColor != null) + defaultColor = barItemColor.ToPlatform().ToArgb(); if (barItemColor == null && _originalTabTextColors != null) defaultColor = _originalTabTextColors.DefaultColor; @@ -547,6 +548,7 @@ protected virtual ColorStateList GetItemTextColorStates() } _newTabTextColors = GetColorStateList(defaultColor, checkedColor); + return _newTabTextColors; } diff --git a/src/Controls/tests/DeviceTests/Elements/TabbedPage/TabbedPageTests.Android.cs b/src/Controls/tests/DeviceTests/Elements/TabbedPage/TabbedPageTests.Android.cs new file mode 100644 index 000000000000..cf08dac82294 --- /dev/null +++ b/src/Controls/tests/DeviceTests/Elements/TabbedPage/TabbedPageTests.Android.cs @@ -0,0 +1,29 @@ +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() + { + var expected = Colors.Red; + + SetupBuilder(); + var tabbedPage = CreateBasicTabbedPage(); + tabbedPage.SelectedTabColor = expected; + + await CreateHandlerAndAddToWindow(new Window(tabbedPage), (handler) => + { + var platformView = tabbedPage.Handler.PlatformView as ViewPager2; + Assert.NotNull(platformView); + return Task.CompletedTask; + }); + } + } +} \ No newline at end of file From 65ddae48aa2818b51576ad2ce7c8b62eb048c6ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Tue, 7 Feb 2023 17:30:44 +0100 Subject: [PATCH 2/5] Update src/Controls/src/Core/Platform/Android/TabbedPageManager.cs Co-authored-by: Manuel de la Pena --- src/Controls/src/Core/Platform/Android/TabbedPageManager.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs index 5da69409a80d..2e5bba04e25d 100644 --- a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs +++ b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs @@ -548,7 +548,6 @@ protected virtual ColorStateList GetItemTextColorStates() } _newTabTextColors = GetColorStateList(defaultColor, checkedColor); - return _newTabTextColors; } From 1d3c61643863678bf4891886aa1a03054ca90533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Wed, 8 Feb 2023 14:06:44 +0100 Subject: [PATCH 3/5] Refactoring code --- .../Elements/TabbedPage/TabbedPageTests.Android.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Controls/tests/DeviceTests/Elements/TabbedPage/TabbedPageTests.Android.cs b/src/Controls/tests/DeviceTests/Elements/TabbedPage/TabbedPageTests.Android.cs index cf08dac82294..fae6475a8df1 100644 --- a/src/Controls/tests/DeviceTests/Elements/TabbedPage/TabbedPageTests.Android.cs +++ b/src/Controls/tests/DeviceTests/Elements/TabbedPage/TabbedPageTests.Android.cs @@ -12,11 +12,10 @@ public partial class TabbedPageTests [Fact(DisplayName = "Using SelectedTab Color doesnt crash")] public async Task SelectedTabColorNoDoesntCrash() { - var expected = Colors.Red; - SetupBuilder(); + var tabbedPage = CreateBasicTabbedPage(); - tabbedPage.SelectedTabColor = expected; + tabbedPage.SelectedTabColor = Colors.Red; await CreateHandlerAndAddToWindow(new Window(tabbedPage), (handler) => { From 5fc782f5d219f79c314c1eb3a3d0c1a74c273368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Wed, 8 Feb 2023 14:42:45 +0100 Subject: [PATCH 4/5] Small refactoring and included comments --- .../src/Core/Platform/Android/TabbedPageManager.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs index 2e5bba04e25d..204a08d7380a 100644 --- a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs +++ b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs @@ -526,7 +526,11 @@ protected virtual ColorStateList GetItemTextColorStates() return _newTabTextColors; int checkedColor; - int defaultColor = 0; + + // 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 != null) { @@ -541,13 +545,17 @@ protected virtual ColorStateList GetItemTextColorStates() if (barItemColor == null && _originalTabTextColors != null) defaultColor = _originalTabTextColors.DefaultColor; - checkedColor = defaultColor; + if (!defaultColor.HasValue) + return _originalTabTextColors; + else + checkedColor = defaultColor.Value; if (barSelectedItemColor != null) checkedColor = barSelectedItemColor.ToPlatform().ToArgb(); } - _newTabTextColors = GetColorStateList(defaultColor, checkedColor); + _newTabTextColors = GetColorStateList(defaultColor.Value, checkedColor); + return _newTabTextColors; } From 3f968bc8f06b3090891d61e4edc62d154a9f4a80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Wed, 8 Feb 2023 17:51:33 +0100 Subject: [PATCH 5/5] Changes based on feedback --- .../src/Core/Platform/Android/TabbedPageManager.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs index 204a08d7380a..7ff8ebba06ad 100644 --- a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs +++ b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs @@ -512,17 +512,17 @@ internal void UpdateBarBackground() protected virtual ColorStateList GetItemTextColorStates() { - if (_originalTabTextColors == null) + 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; @@ -532,17 +532,17 @@ protected virtual ColorStateList GetItemTextColorStates() // original colors is used. int? defaultColor = null; - if (barTextColor != null) + if (barTextColor is not null) { checkedColor = barTextColor.ToPlatform().ToArgb(); defaultColor = checkedColor; } else { - if (barItemColor != null) + 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; if (!defaultColor.HasValue) @@ -550,7 +550,7 @@ protected virtual ColorStateList GetItemTextColorStates() else checkedColor = defaultColor.Value; - if (barSelectedItemColor != null) + if (barSelectedItemColor is not null) checkedColor = barSelectedItemColor.ToPlatform().ToArgb(); }