Skip to content

Commit

Permalink
refactored properties
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMisiukevich committed Aug 31, 2018
1 parent 36ed93f commit 9174ca6
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions ExpandableView/ExpandableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ExpandableView : StackLayout
public static readonly BindableProperty PrimaryViewProperty = BindableProperty.Create(nameof(PrimaryView), typeof(View), typeof(ExpandableView), null, propertyChanged: (bindable, oldValue, newValue) =>
{
(bindable as ExpandableView).SetPrimaryView(oldValue as View);
(bindable as ExpandableView).OnShouldHandleTapToExpandChanged();
(bindable as ExpandableView).OnTouchHandlerViewChanged();
});

public static readonly BindableProperty SecondaryViewTemplateProperty = BindableProperty.Create(nameof(SecondaryViewTemplate), typeof(DataTemplate), typeof(ExpandableView), null, propertyChanged: (bindable, oldValue, newValue) =>
Expand All @@ -25,16 +25,13 @@ public class ExpandableView : StackLayout
(bindable as ExpandableView).OnIsExpandedChanged();
});

public static readonly BindableProperty ShouldHandleTapToExpandProperty = BindableProperty.Create(nameof(ShouldHandleTapToExpand), typeof(bool), typeof(ExpandableView), true, propertyChanged: (bindable, oldValue, newValue) =>
{
(bindable as ExpandableView).OnShouldHandleTapToExpandChanged();
});

public static readonly BindableProperty TouchHandlerViewProperty = BindableProperty.Create(nameof(TouchHandlerView), typeof(View), typeof(ExpandableView), null, propertyChanged: (bindable, oldValue, newValue) =>
{
(bindable as ExpandableView).OnShouldHandleTapToExpandChanged();
(bindable as ExpandableView).OnTouchHandlerViewChanged();
});

public static readonly BindableProperty IsTouchToExpandEnabledProperty = BindableProperty.Create(nameof(IsTouchToExpandEnabled), typeof(bool), typeof(ExpandableView), true);

public static readonly BindableProperty SecondaryViewHeightRequestProperty = BindableProperty.Create(nameof(SecondaryViewHeightRequest), typeof(double), typeof(ExpandableView), 0.0);

public static readonly BindableProperty ExpandAnimationLengthProperty = BindableProperty.Create(nameof(ExpandAnimationLength), typeof(uint), typeof(ExpandableView), 250u);
Expand All @@ -54,7 +51,14 @@ public ExpandableView()
{
_defaultTapGesture = new TapGestureRecognizer
{
Command = new Command(() => IsExpanded = !IsExpanded)
Command = new Command(() =>
{
if (!IsTouchToExpandEnabled)
{
return;
}
IsExpanded = !IsExpanded;
})
};
}

Expand All @@ -76,18 +80,18 @@ public bool IsExpanded
set => SetValue(IsExpandedProperty, value);
}

public bool ShouldHandleTapToExpand
{
get => (bool)GetValue(ShouldHandleTapToExpandProperty);
set => SetValue(ShouldHandleTapToExpandProperty, value);
}

public View TouchHandlerView
{
get => GetValue(TouchHandlerViewProperty) as View;
set => SetValue(TouchHandlerViewProperty, value);
}

public bool IsTouchToExpandEnabled
{
get => (bool)GetValue(IsTouchToExpandEnabledProperty);
set => SetValue(IsTouchToExpandEnabledProperty, value);
}

public double SecondaryViewHeightRequest
{
get => (double)GetValue(SecondaryViewHeightRequestProperty);
Expand Down Expand Up @@ -186,22 +190,12 @@ private void OnIsExpandedChanged()
}
}

private void OnShouldHandleTapToExpandChanged()
private void OnTouchHandlerViewChanged()
{
if (PrimaryView == null)
{
return;
}

var viewToAttachTapGesture = TouchHandlerView ?? PrimaryView;

viewToAttachTapGesture?.GestureRecognizers.Remove(_defaultTapGesture);
var touchHandlerView = TouchHandlerView ?? PrimaryView;
touchHandlerView?.GestureRecognizers.Remove(_defaultTapGesture);
PrimaryView?.GestureRecognizers.Remove(_defaultTapGesture);

if (ShouldHandleTapToExpand)
{
viewToAttachTapGesture.GestureRecognizers.Add(_defaultTapGesture);
}
touchHandlerView?.GestureRecognizers.Add(_defaultTapGesture);
}

private void SetPrimaryView(View oldView)
Expand Down

0 comments on commit 9174ca6

Please sign in to comment.