Skip to content

Commit

Permalink
Update Demo and fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmanuelthiel committed Dec 26, 2017
1 parent ef56865 commit 31ba731
Show file tree
Hide file tree
Showing 15 changed files with 7,701 additions and 7,152 deletions.
14 changes: 14 additions & 0 deletions SwipeCards.Controls/Arguments/DraggingEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace SwipeCards.Controls.Arguments
{
public class DraggingEventArgs : EventArgs
{
public readonly object Item;

public DraggingEventArgs(object item)
{
this.Item = item;
}
}
}
21 changes: 21 additions & 0 deletions SwipeCards.Controls/Arguments/SwipedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace SwipeCards.Controls.Arguments
{
public class SwipedEventArgs : EventArgs
{
public readonly object Item;
public readonly SwipeDirection Direction;

public SwipedEventArgs(object item, SwipeDirection direction)
{
this.Item = item;
this.Direction = direction;
}
}

public enum SwipeDirection
{
Left, Right
}
}
3 changes: 2 additions & 1 deletion SwipeCards.Controls/SwipeCards.Controls.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="2.5.0.91635" />
<PackageReference Include="Xamarin.Forms" Version="2.5.0.121934" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Views\CardStackView.xaml">
Expand All @@ -35,5 +35,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Views\" />
<Folder Include="Arguments\" />
</ItemGroup>
</Project>
35 changes: 18 additions & 17 deletions SwipeCards.Controls/Views/CardStackView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Xamarin.Forms.Internals;
using SwipeCards.Controls.Arguments;

namespace SwipeCards.Controls
{
Expand Down Expand Up @@ -117,11 +118,10 @@ public ICommand SwipedLeftCommand
//}

#endregion

public Action<object> SwipedRight = null;
public Action<object> SwipedLeft = null;
public Action<object> StartedDragging = null;
public Action<object> FinishedDragging = null;

public event EventHandler<SwipedEventArgs> Swiped;
public event EventHandler<DraggingEventArgs> StartedDragging;
public event EventHandler<DraggingEventArgs> FinishedDragging;

private const int numberOfCards = 2;
private const int animationLength = 250;
Expand Down Expand Up @@ -152,12 +152,14 @@ public void Setup()
for (var i = numberOfCards - 1; i >= 0; i--)
{
// Create CardView
var cardView = new CardView(ItemTemplate);
cardView.IsVisible = false;
cardView.Scale = (i == 0) ? 1.0f : defaultSubcardScale;
cardView.IsEnabled = false;

// Add CardView to UI
var cardView = new CardView(ItemTemplate)
{
IsVisible = false,
Scale = (i == 0) ? 1.0f : defaultSubcardScale,
IsEnabled = false
};

// Add CardView to UI
CardStack.Children.Add(
cardView,
Constraint.Constant(0), // X
Expand All @@ -172,13 +174,12 @@ public void Setup()
ShowNextCard();
}


protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);

// Recalculate move distance
if (CardMoveDistance == -1)
if (CardMoveDistance == -1 && !width.Equals(-1))
CardMoveDistance = (int)(width / 3);
}

Expand All @@ -204,7 +205,7 @@ async void OnPanUpdated(object sender, PanUpdatedEventArgs e)
private void HandleTouchStart()
{
if (itemIndex < ItemsSource.Count)
StartedDragging?.Invoke(ItemsSource[itemIndex]);
StartedDragging?.Invoke(this, new DraggingEventArgs(ItemsSource[itemIndex]));
}

private void HandleTouchRunning(float xDiff)
Expand Down Expand Up @@ -254,13 +255,13 @@ private async Task HandleTouchCompleted()
// Fire events
if (cardDistance > 0)
{
SwipedRight?.Invoke(ItemsSource[itemIndex]);
Swiped?.Invoke(this, new SwipedEventArgs(ItemsSource[itemIndex], SwipeDirection.Right));
if (SwipedRightCommand != null && SwipedRightCommand.CanExecute(ItemsSource[itemIndex]))
SwipedRightCommand.Execute(ItemsSource[itemIndex]);
}
else
{
SwipedLeft?.Invoke(ItemsSource[itemIndex]);
Swiped?.Invoke(this, new SwipedEventArgs(ItemsSource[itemIndex], SwipeDirection.Left));
if (SwipedLeftCommand != null && SwipedLeftCommand.CanExecute(ItemsSource[itemIndex]))
SwipedLeftCommand.Execute(ItemsSource[itemIndex]);
}
Expand All @@ -283,7 +284,7 @@ private async Task HandleTouchCompleted()
}

if (itemIndex < ItemsSource.Count)
FinishedDragging?.Invoke(ItemsSource[itemIndex]);
FinishedDragging?.Invoke(this, new DraggingEventArgs(ItemsSource[itemIndex]));
}

private void ShowNextCard()
Expand Down
2 changes: 1 addition & 1 deletion SwipeCards.Demo.Forms.Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace SwipeCards.Demo.Forms.Droid
{
[Activity(Label = "SwipeCards.Demo.Forms", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
[Activity(Label = "SwipeCards Demo", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
Expand Down
Loading

0 comments on commit 31ba731

Please sign in to comment.