From beba29e7985c46a7d51269e94d02fe20cbeac249 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 a6f31151e6d2..faca16bd303b 100644 --- a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs +++ b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs @@ -512,7 +512,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; @@ -525,7 +525,7 @@ protected virtual ColorStateList GetItemTextColorStates() return _newTabTextColors; int checkedColor; - int defaultColor; + int defaultColor = 0; if (barTextColor != null) { @@ -534,7 +534,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; @@ -546,6 +547,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 7048132e2def199e9181d6e6b6e66e20d6aa89f7 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 faca16bd303b..def0460d6f97 100644 --- a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs +++ b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs @@ -547,7 +547,6 @@ protected virtual ColorStateList GetItemTextColorStates() } _newTabTextColors = GetColorStateList(defaultColor, checkedColor); - return _newTabTextColors; } From 32096284658e90832b5b99b4e91aa38fe0083210 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 d8b1492c90c8c7c89c89f8a2afa02b9106458d23 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 def0460d6f97..5a5dce75d00c 100644 --- a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs +++ b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs @@ -525,7 +525,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) { @@ -540,13 +544,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 bde912397d2ea56720737f7daf2a0130a10e9ec0 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 5a5dce75d00c..91f4a9e20b5b 100644 --- a/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs +++ b/src/Controls/src/Core/Platform/Android/TabbedPageManager.cs @@ -511,17 +511,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; @@ -531,17 +531,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) @@ -549,7 +549,7 @@ protected virtual ColorStateList GetItemTextColorStates() else checkedColor = defaultColor.Value; - if (barSelectedItemColor != null) + if (barSelectedItemColor is not null) checkedColor = barSelectedItemColor.ToPlatform().ToArgb(); }