Skip to content

Commit

Permalink
Making IsSelectable a bindable property, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sevren committed May 25, 2022
1 parent 4ae9519 commit 1d578f2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 38 deletions.
3 changes: 2 additions & 1 deletion Tabs/Tabs/BottomTabItem.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ protected override void OnPropertyChanged([CallerMemberName] string propertyName
UpdateTextVisibility();
break;

case nameof(IsSelectable):
case nameof(UnselectedLabelColor):
case nameof(UnselectedIconColor):
case nameof(SelectedTabColor):
Expand Down Expand Up @@ -123,7 +124,7 @@ private void UpdateTextVisibility()

private void UpdateColors()
{
IconText.TextColor = IsSelected ? SelectedTabColor : UnselectedLabelColor;
IconText.TextColor = IsSelectable ? IsSelected ? SelectedTabColor : UnselectedLabelColor : DisabledLabelColor;
ImageEffect.SetTintColor(Icon, IsSelected ? SelectedTabColor : UnselectedIconColor);
}
}
Expand Down
64 changes: 31 additions & 33 deletions Tabs/Tabs/TabHostView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ namespace Sharpnado.Tabs
public enum TabType
{
Fixed = 0,
Scrollable,
Scrollable
}

public enum OrientationType
{
Horizontal = 0,
Vertical,
Vertical
}

[ContentProperty("TabHostContent")]
Expand All @@ -33,7 +33,7 @@ public class TabHostView : Shadows
nameof(ItemsSource),
typeof(IEnumerable),
typeof(TabHostView),
defaultValueCreator: _ => new TabItem[0]);
defaultValueCreator: _ => Array.Empty<TabItem>());

public static readonly BindableProperty ItemTemplateProperty = BindableProperty.Create(
nameof(ItemTemplate),
Expand Down Expand Up @@ -87,7 +87,7 @@ public class TabHostView : Shadows
defaultValue: OrientationType.Horizontal,
propertyChanged: OrientationPropertyChanged);

public static readonly new BindableProperty BackgroundColorProperty = BindableProperty.Create(
public new static readonly BindableProperty BackgroundColorProperty = BindableProperty.Create(
nameof(BackgroundColor),
typeof(Color),
typeof(TabHostView),
Expand All @@ -97,7 +97,7 @@ public class TabHostView : Shadows

private readonly Grid _grid;
private readonly Frame _frame;
private List<TabItem> _selectableTabs = new List<TabItem>();
private List<TabItem> _selectableTabs = new();

private INotifyCollectionChanged _currentNotifyCollection;

Expand All @@ -121,19 +121,19 @@ public TabHostView()
ColumnSpacing = 0,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.Fill,
BackgroundColor = this.BackgroundColor,
BackgroundColor = BackgroundColor
};

_frame = new Frame
{
Padding = 0,
HasShadow = false,
IsClippedToBounds = true,
CornerRadius = this.CornerRadius,
CornerRadius = CornerRadius,
BackgroundColor = Color.Transparent,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.Fill,
BorderColor = SegmentedOutlineColor,
BorderColor = SegmentedOutlineColor
};

UpdateTabType();
Expand Down Expand Up @@ -385,7 +385,7 @@ private TabItem CreateTabItem(object item)
result = (View)ItemTemplate.CreateContent();
}

if (!(result is TabItem tabItem))
if (result is not TabItem tabItem)
{
throw new InvalidOperationException("Your ItemTemplate DataTemplate should contain a view inheriting from TabItem");
}
Expand Down Expand Up @@ -486,7 +486,7 @@ private void UpdateSelectedIndex(int selectedIndex)
selectedIndex = _selectableTabs.Count - 1;
}

for (int index = 0; index < _selectableTabs.Count; index++)
for (var index = 0; index < _selectableTabs.Count; index++)
{
_selectableTabs[index].IsSelected = selectedIndex == index;
}
Expand All @@ -497,7 +497,12 @@ private void UpdateSelectedIndex(int selectedIndex)

private void OnTabItemTapped(object tappedItem)
{
int selectedIndex = _selectableTabs.IndexOf((TabItem)tappedItem);
var selectedIndex = _selectableTabs.IndexOf((TabItem)tappedItem);

if (!_selectableTabs[selectedIndex].IsSelectable)
{
return;
}

UpdateSelectedIndex(selectedIndex);
RaiseSelectedTabIndexChanged(new SelectedPositionChangedEventArgs(selectedIndex));
Expand Down Expand Up @@ -619,7 +624,8 @@ private void AddTapCommand(TabItem tabItem)
if (Device.RuntimePlatform == Device.UWP)
{
tabItem.GestureRecognizers.Add(
new TapGestureRecognizer() { Command = TabItemTappedCommand, CommandParameter = tabItem });
new TapGestureRecognizer { Command = TabItemTappedCommand, CommandParameter = tabItem }
);
}
else
{
Expand All @@ -639,7 +645,7 @@ private void OnChildAdded(TabItem tabItem, int index)
_grid.BatchBegin();
BatchBegin();

int tabIndexInGrid = GetTabIndexInGrid(index);
var tabIndexInGrid = GetTabIndexInGrid(index);

_grid.Children.Insert(tabIndexInGrid, tabItem);
if (Orientation == OrientationType.Horizontal)
Expand Down Expand Up @@ -686,11 +692,7 @@ private void OnChildAdded(TabItem tabItem, int index)
}

RaiseTabButtons();

if (tabItem.IsSelectable)
{
AddTapCommand(tabItem);
}
AddTapCommand(tabItem);

if (TabType == TabType.Fixed)
{
Expand Down Expand Up @@ -725,7 +727,7 @@ private int GetTabIndexInGrid(int index)
}

var previousElementAt = _grid.Children.Where(v => v is TabItem).ElementAtOrDefault(index - 1);
int indexInGrid = _grid.Children.IndexOf(previousElementAt) + 1;
var indexInGrid = _grid.Children.IndexOf(previousElementAt) + 1;

InternalLogger.Debug(Tag, () => $"GetTabIndexInGrid() => indexInGrid: {indexInGrid}");
return indexInGrid;
Expand All @@ -752,7 +754,7 @@ private void OnChildRemoved(TabItem tabItem)
}
}

int tabItemIndex = _grid.Children.IndexOf(tabItem);
var tabItemIndex = _grid.Children.IndexOf(tabItem);

InternalLogger.Debug(Tag, () => $"OnChildRemoved( tabItem: {tabItem.GetType().Name}, index: {tabItemIndex} )");

Expand Down Expand Up @@ -791,7 +793,7 @@ private void UpdateSelectableTabs()

private void ConsolidateColumnIndexes()
{
int index = 0;
var index = 0;
foreach (var tabItem in Tabs)
{
if (Orientation == OrientationType.Horizontal)
Expand Down Expand Up @@ -833,13 +835,13 @@ private void ConsolidateSeparatedColumnIndexes()
}
}

int index = 0;
var index = 0;
while (index < _grid.Children.Count)
{
var currentItem = _grid.Children[index];

bool previousItemIsTab = index > 0 && _grid.Children[index - 1] is TabItem;
bool currentItemIsTab = currentItem is TabItem;
var previousItemIsTab = index > 0 && _grid.Children[index - 1] is TabItem;
var currentItemIsTab = currentItem is TabItem;

if (previousItemIsTab && currentItemIsTab)
{
Expand Down Expand Up @@ -871,8 +873,8 @@ private void ConsolidateSeparatedColumnIndexes()
continue;
}

bool previousItemIsSeparator = index > 0 && _grid.Children[index - 1] is BoxView;
bool currentItemIsSeparator = currentItem is BoxView;
var previousItemIsSeparator = index > 0 && _grid.Children[index - 1] is BoxView;
var currentItemIsSeparator = currentItem is BoxView;

if (previousItemIsSeparator && currentItemIsSeparator)
{
Expand Down Expand Up @@ -987,12 +989,12 @@ private void UpdateTabOrientation()
_grid.BatchBegin();
BatchBegin();

if (_grid.RowDefinitions.Count() != 0)
if (_grid.RowDefinitions.Count != 0)
{
_grid.RowDefinitions.Clear();
}

if (_grid.ColumnDefinitions.Count() != 0)
if (_grid.ColumnDefinitions.Count != 0)
{
_grid.ColumnDefinitions.Clear();
}
Expand Down Expand Up @@ -1046,11 +1048,7 @@ private void UpdateTabOrientation()
}

RaiseTabButtons();

if (tabItem.IsSelectable)
{
AddTapCommand(tabItem);
}
AddTapCommand(tabItem);

if (TabType == TabType.Fixed)
{
Expand Down
28 changes: 26 additions & 2 deletions Tabs/Tabs/TabItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;

using Xamarin.Forms;

namespace Sharpnado.Tabs
Expand All @@ -23,7 +23,26 @@ public abstract class TabItem : ContentView
typeof(TabItem),
default(BadgeView),
propertyChanged: OnBadgeChanged);

public static readonly BindableProperty IsSelectableProperty = BindableProperty.Create(
nameof(IsSelectable),
typeof(bool),
typeof(TabItem),
true);

public static readonly BindableProperty DisabledLabelColorProperty = BindableProperty.Create(
nameof(DisabledLabelColor),
typeof(Color),
typeof(TabTextItem),
Color.Default);


public Color DisabledLabelColor
{
get => (Color)GetValue(DisabledLabelColorProperty);
set => SetValue(DisabledLabelColorProperty, value);
}

public bool IsSelected
{
get => (bool)GetValue(IsSelectedProperty);
Expand All @@ -42,7 +61,12 @@ public BadgeView Badge
set => SetValue(BadgeProperty, value);
}

public bool IsSelectable { get; set; } = true;
public bool IsSelectable
{
get => (bool)GetValue(IsSelectableProperty);
set => SetValue(IsSelectableProperty, value);
}


protected abstract void OnBadgeChanged(BadgeView oldBadge);

Expand Down
4 changes: 3 additions & 1 deletion Tabs/Tabs/TabTextItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;

using Xamarin.Forms;

namespace Sharpnado.Tabs
Expand Down Expand Up @@ -29,6 +29,8 @@ public abstract class TabTextItem : TabItem
typeof(Color),
typeof(TabTextItem),
Color.Default);



public string Label
{
Expand Down
3 changes: 2 additions & 1 deletion Tabs/Tabs/UnderlinedTabItemBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ protected override void OnPropertyChanged([CallerMemberName] string propertyName
UpdateUnderlineAllTab();
break;

case nameof(IsSelectable):
case nameof(UnselectedLabelColor):
case nameof(SelectedTabColor):
case nameof(IsSelected):
Expand Down Expand Up @@ -112,7 +113,7 @@ private void UpdateMargin()

private void UpdateColors()
{
InnerLabelImpl.TextColor = IsSelected ? SelectedTabColor : UnselectedLabelColor;
InnerLabelImpl.TextColor = IsSelectable ? IsSelected ? SelectedTabColor : UnselectedLabelColor : DisabledLabelColor;
UnderlineImpl.Color = SelectedTabColor;
}
}
Expand Down

0 comments on commit 1d578f2

Please sign in to comment.