Skip to content

Commit

Permalink
Harmonize repetitive code (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasgalliker committed Jul 24, 2024
1 parent 01946c8 commit 7d16c83
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Android.Widget;
using RadioButton = Android.Widget.RadioButton;

namespace Plugin.SegmentedControl.Maui.Platforms.Extensions
{
internal static class RadioGroupExtensions
{
internal static RadioButton GetRadioButtonAt(this RadioGroup radioGroup, int index)
{
var radioButton = radioGroup.GetChildAt(index) as RadioButton;
return radioButton;
}

internal static IEnumerable<RadioButton> GetRadioButtons(this RadioGroup radioGroup)
{
var childCount = radioGroup.ChildCount;
if (childCount > 0)
{
for (var i = 0; i < childCount; i++)
{
if (radioGroup.GetChildAt(i) is RadioButton radioButton)
{
yield return radioButton;
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using Plugin.SegmentedControl.Maui.Extensions;
using Plugin.SegmentedControl.Maui.Platforms.Extensions;
using Plugin.SegmentedControl.Maui.Utils;
using static Android.Views.ViewGroup;
using RadioButton = Android.Widget.RadioButton;
Expand Down Expand Up @@ -216,7 +217,7 @@ private static Android.Graphics.Color GetTextColor(SegmentedControl segmentedCon
? segmentedControl.TextColor.ToPlatform()
: segmentedControl.DisabledTextColor.ToPlatform();
}

private static Android.Graphics.Color GetSelectedTextColor(SegmentedControl segmentedControl, bool enabled)
{
return enabled
Expand All @@ -235,9 +236,6 @@ private void ConfigureRadioButton(int selectedSegment, bool isEnabled, RadioButt
}

var isSelected = selectedSegment == segmentedControl.SelectedSegment;

var tintColor = this.GetTintColor(isSelected, isButtonEnabled);

if (isSelected)
{
var textColor = GetSelectedTextColor(segmentedControl, isButtonEnabled);
Expand All @@ -264,6 +262,7 @@ private void ConfigureRadioButton(int selectedSegment, bool isEnabled, RadioButt

radioButton.SetTypeface(typeface, TypefaceStyle.Normal);

var tintColor = this.GetTintColor(isSelected, isButtonEnabled);
SetTintColor(radioButton, tintColor);
}

Expand Down Expand Up @@ -323,7 +322,7 @@ private static void MapSelectedSegment(SegmentedControlHandler handler, Segmente
var radioGroup = handler.PlatformView;

var selectedSegment = segmentedControl.SelectedSegment;
var radioButton = (RadioButton)radioGroup.GetChildAt(selectedSegment);
var radioButton = radioGroup.GetRadioButtonAt(selectedSegment);
if (radioButton == null)
{
return;
Expand Down Expand Up @@ -358,7 +357,8 @@ private static void UpdateSelectedTextColor(SegmentedControlHandler handler, Seg
return;
}

var radioButton = (RadioButton)handler.PlatformView.GetChildAt(selectedSegment);
var radioGroup = handler.PlatformView;
var radioButton = radioGroup.GetRadioButtonAt(selectedSegment);
if (radioButton != null)
{
var enabled = segmentedControl.IsEnabled && radioButton.Enabled;
Expand All @@ -374,7 +374,8 @@ private static void MapDisabledTintColor(SegmentedControlHandler handler, Segmen
return;
}

var radioButton = (RadioButton)handler.PlatformView.GetChildAt(selectedSegment);
var radioGroup = handler.PlatformView;
var radioButton = radioGroup.GetRadioButtonAt(selectedSegment);
if (radioButton != null)
{
var isButtonEnabled = segmentedControl.IsEnabled && radioButton.Enabled;
Expand All @@ -387,37 +388,37 @@ private static void MapDisabledTextColor(SegmentedControlHandler handler, Segmen
{
// Go through tab items and update disabled segments
var radioGroup = handler.PlatformView;
var childCount = radioGroup.ChildCount;
if (childCount > 0)
var disabledRadioButtons = radioGroup.GetRadioButtons()
.Where(b => b.Enabled == false)
.ToArray();

if (disabledRadioButtons.Length > 0)
{
var disabledTextColor = segmentedControl.DisabledTextColor.ToPlatform();

for (var i = 0; i < childCount; i++)
foreach (var radioButton in disabledRadioButtons)
{
var radioButton = (RadioButton)radioGroup.GetChildAt(i);
if (radioButton == null || radioButton.Enabled)
{
continue;
}

radioButton.SetTextColor(disabledTextColor);
}
}
}

private static void MapDisabledBackgroundColor(SegmentedControlHandler handler, SegmentedControl segmentedControl)
{
for (var i = 0; i < handler.PlatformView.ChildCount; i++)
var radioGroup = handler.PlatformView;
var disabledRadioButtons = radioGroup.GetRadioButtons()
.Where(b => b.Enabled == false)
.ToArray();

if (disabledRadioButtons.Length > 0)
{
var radioButton = (RadioButton)handler.PlatformView.GetChildAt(i);
if (radioButton == null)
var disabledBackgroundColor = segmentedControl.DisabledBackgroundColor.ToPlatform();

foreach (var radioButton in disabledRadioButtons)
{
continue;
var tintColor = handler.GetTintColor(radioButton.Checked, enabled: false);
SetTintColor(radioButton, tintColor);
}

var enabled = !(radioButton.Enabled && segmentedControl.IsEnabled);
var tintColor = handler.GetTintColor(radioButton.Checked, enabled);
SetTintColor(radioButton, tintColor);
}
}

Expand Down Expand Up @@ -480,27 +481,22 @@ private static void MapChildren(SegmentedControlHandler handler, SegmentedContro
private static void MapTextColor(SegmentedControlHandler handler, SegmentedControl segmentedControl)
{
var radioGroup = handler.PlatformView;
var childCount = radioGroup.ChildCount;
if (childCount > 0)
{
var textColor = segmentedControl.TextColor.ToPlatform();
var selectedTextColor = segmentedControl.SelectedTextColor.ToPlatform();
var radioButtons = radioGroup.GetRadioButtons().ToArray();

for (var i = 0; i < childCount; i++)
if (radioButtons.Length > 0)
{
for (var i = 0; i < radioButtons.Length; i++)
{
var radioButton = (RadioButton)radioGroup.GetChildAt(i);
if (radioButton == null)
var radioButton = radioButtons[i];
if (i == segmentedControl.SelectedSegment)
{
continue;
}

if (i != segmentedControl.SelectedSegment)
{
radioButton.SetTextColor(textColor);
var selectedTextColor = GetSelectedTextColor(segmentedControl, radioButton.Enabled);
radioButton.SetTextColor(selectedTextColor);
}
else
{
radioButton.SetTextColor(selectedTextColor);
var textColor = GetTextColor(segmentedControl, radioButton.Enabled);
radioButton.SetTextColor(textColor);
}
}
}
Expand Down

0 comments on commit 7d16c83

Please sign in to comment.