Skip to content

Commit

Permalink
Add animations to CardsLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavAntonyuk committed Mar 2, 2024
1 parent b8ad6dd commit 774e117
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 54 deletions.
64 changes: 54 additions & 10 deletions CardLayout/CardsLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,48 @@ private void ItemsChanged()

foreach (var item in Items)
{
Children.Add(ViewFor(item));
var swipeView = new SwipeView()
{
Threshold = 150,
Content = ViewFor(item)
};
switch (Direction)
{
case CardLayoutDirection.RightToLeft :
case CardLayoutDirection.LeftToRight:
swipeView.RightItems = new SwipeItems([new SwipeItemView()])
{
Mode = SwipeMode.Execute
};
swipeView.LeftItems = new SwipeItems([new SwipeItemView()])
{
Mode = SwipeMode.Execute
};
break;
case CardLayoutDirection.UpToDown:
case CardLayoutDirection.DownToUp:
swipeView.TopItems = new SwipeItems([new SwipeItemView()])
{
Mode = SwipeMode.Execute
};
swipeView.BottomItems = new SwipeItems([new SwipeItemView()])
{
Mode = SwipeMode.Execute
};
break;
default:
throw new ArgumentOutOfRangeException();
}
swipeView.SwipeEnded += SwipeView_SwipeEnded;
Children.Add(swipeView);
}
}

private void SwipeView_SwipeEnded(object? sender, SwipeEndedEventArgs e)
{
HandleTouchEnd(e.SwipeDirection);
}

protected override void OnChildAdded(Element child)
{
if (Children.Count > 1 && EmptyView is not null && Children[0] == EmptyView)
Expand All @@ -55,6 +93,13 @@ protected override void OnChildAdded(Element child)
else
{
base.OnChildAdded(child);
for (var index = 0; index < Children.Count - 1; index++)
{
var childView = (View)Children[index];
childView.IsEnabled = false;
}

((View)child).IsEnabled = true;
}
}

Expand All @@ -66,6 +111,13 @@ protected override void OnChildRemoved(Element child, int oldLogicalIndex)
}

base.OnChildRemoved(child, oldLogicalIndex);
for (var index = 0; index < Children.Count - 1; index++)
{
var childView = (View)Children[index];
childView.IsEnabled = false;
}

((View)Children[^1]).IsEnabled = true;
}

protected virtual View? ViewFor(object item)
Expand All @@ -86,14 +138,6 @@ public CardsLayout()
var panGesture = new PanGestureRecognizer();
panGesture.PanUpdated += PanGesture_PanUpdated;
GestureRecognizers.Add(panGesture);
//var swipeGesture = new SwipeGestureRecognizer();
//swipeGesture.Swiped += SwipeGesture_Swiped;
//GestureRecognizers.Add(swipeGesture);
}

private void SwipeGesture_Swiped(object? sender, SwipedEventArgs e)
{
HandleTouchEnd(e.Direction);
}

private SwipeDirection? swipedDirection;
Expand Down Expand Up @@ -126,7 +170,7 @@ private void HandleTouch(double eTotalX, double eTotalY)
}
};
}

private void HandleTouchEnd(SwipeDirection? swiped)
{
if (swiped == null)
Expand Down
74 changes: 30 additions & 44 deletions CardLayout/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,36 @@

public partial class MainPageViewModel : ObservableObject
{

[ObservableProperty]
private ObservableCollection<Pizza> pizzas;

public MainPageViewModel()
{
pizzas = new()
private ObservableCollection<Pizza> pizzas =
[
new Pizza
{
Name = "Margherita",
Description = "Margherita pizza is a classic Italian pizza topped with fresh mozzarella cheese, tomatoes, and basil leaves.",
Price = 5.99m,
Image = "https://romans-bnry.s3.eu-west-2.amazonaws.com/images/root/v2/pizza/grumbling/pizza-hotone-pan.png"
},
new Pizza
{
Name = "Pepperoni",
Description = "Pepperoni pizza is an American pizza variety1. It is typically topped with pepperoni, a type of cured sausage made with a mixture of beef, pork, and spices.",
Price = 6.99m,
Image = "https://romans-bnry.s3.eu-west-2.amazonaws.com/images/root/v2/pizza/pizza-halaal-hawaiian-thin.png"
},
new Pizza
{
Name = "Hawaiian",
Description = "Hawaiian pizza is a type of pizza that typically features tomato sauce, cheese, ham or Canadian bacon, and pineapple as the main toppings.",
Price = 7.99m,
Image = "https://romans-bnry.s3.eu-west-2.amazonaws.com/images/root/v2/pizza/pizza-hawaiian-pan.png"
},
new Pizza
{
new Pizza
{
Name = "Margherita",
Price = 5.99m,
Image = "https://romans-bnry.s3.eu-west-2.amazonaws.com/images/root/v2/pizza/grumbling/pizza-hotone-pan.png"
},
new Pizza
{
Name = "Pepperoni",
Price = 6.99m,
Image = "https://romans-bnry.s3.eu-west-2.amazonaws.com/images/root/v2/pizza/pizza-halaal-hawaiian-thin.png"
},
new Pizza
{
Name = "Hawaiian",
Price = 7.99m,
Image = "https://romans-bnry.s3.eu-west-2.amazonaws.com/images/root/v2/pizza/pizza-hawaiian-pan.png"
},
new Pizza
{
Name = "Vegetarian",
Price = 8.99m,
Image = "https://romans-bnry.s3.eu-west-2.amazonaws.com/images/root/v2/pizza/pizza-vegetarian-pan.png"
},
new Pizza
{
Name = "Meat Lovers",
Price = 9.99m,
Image = "https://romans-bnry.s3.eu-west-2.amazonaws.com/images/root/v2/pizza/grumbling/pizza-bolognaise-pan.png"
},
new Pizza
{
Name = "Supreme",
Price = 10.99m,
Image = "https://romans-bnry.s3.eu-west-2.amazonaws.com/images/root/v2/pizza/pizza-regina-pan.png"
}
};
}
Name = "Vegetarian",
Description = "Vegetarian pizza is a delightful fusion of dough, sauce, and toppings that are free from meat, embracing a bounty of vegetables, cheeses, and herbs. It’s a canvas of culinary creativity, catering to those seeking a meatless option without sacrificing flavor.",
Price = 8.99m,
Image = "https://romans-bnry.s3.eu-west-2.amazonaws.com/images/root/v2/pizza/pizza-vegetarian-pan.png"
}
];
}

0 comments on commit 774e117

Please sign in to comment.