Skip to content

Commit

Permalink
https://github.com/AndreiMisiukevich/CardView/issues/59
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMisiukevich committed Jul 18, 2018
1 parent c575631 commit 545eb78
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 9 deletions.
24 changes: 15 additions & 9 deletions PanCardView/CardsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public class CardsView : AbsoluteLayout
private readonly Dictionary<Guid, IEnumerable<View>> _viewsGestureCounter = new Dictionary<Guid, IEnumerable<View>>();
private readonly List<TimeDiffItem> _timeDiffItems = new List<TimeDiffItem>();
private readonly ViewsInUseSet _viewsInUse = new ViewsInUseSet();
private readonly InteractionQueue _interactions = new InteractionQueue();

private IEnumerable<View> _prevViews = Enumerable.Empty<View>();
private IEnumerable<View> _nextViews = Enumerable.Empty<View>();
Expand All @@ -152,7 +153,6 @@ public class CardsView : AbsoluteLayout
private bool _hasRenderer;
private bool? _isPortraitOrientation;
private bool? _shouldScrollParent;
private Guid _gestureId;
private DateTime _lastPanTime;
private CancellationTokenSource _slideshowTokenSource;

Expand Down Expand Up @@ -670,7 +670,7 @@ private void StartAutoNavigation(View view, Guid animationId, AnimationDirection
{
if (view != null)
{
_gestureId = animationId;
_interactions.Add(animationId, InteractionType.Animation);
IsAutoNavigating = true;
if (IsPanInCourse)
{
Expand All @@ -696,9 +696,10 @@ private void EndAutoNavigation(View view, Guid animationId, AnimationDirection a
}
}
IsAutoNavigating = false;
var isProcessingNow = _gestureId != animationId;
var isProcessingNow = !_interactions.CheckLastId(animationId);
RemoveRedundantChildren(isProcessingNow);
FireAutoNavigationEnded(animationDirection != AnimationDirection.Prev);
_interactions.Remove(animationId);
}

private AnimationDirection GetAutoNavigateAnimationDirection()
Expand Down Expand Up @@ -754,7 +755,9 @@ private void OnTouchStarted()
_inCoursePanDelay = int.MaxValue;
}

var gestureId = _gestureId = Guid.NewGuid();
var gestureId = Guid.NewGuid();
_interactions.Add(gestureId, InteractionType.Gesture);

FirePanStarted();
IsPanRunning = true;
_isPanEndRequested = false;
Expand Down Expand Up @@ -796,7 +799,7 @@ private async void OnTouchEnded(bool? isSwiped)
}

_lastPanTime = DateTime.Now;
var gestureId = _gestureId;
var gestureId = _interactions.GetFirstId(InteractionType.Gesture);

_isPanEndRequested = true;
var absDiff = Abs(CurrentDiff);
Expand Down Expand Up @@ -858,8 +861,9 @@ await Task.WhenAll(

FirePanEnded(isNextSelected, index, diff);

RemoveRangeViewsInUse(gestureId);
var isProcessingNow = gestureId != _gestureId;
var isProcessingNow = !_interactions.CheckLastId(gestureId);
RemoveRangeViewsInUse(gestureId, isProcessingNow);

if (!isProcessingNow)
{
IsPanRunning = false;
Expand All @@ -877,6 +881,8 @@ await Task.WhenAll(
RemoveRedundantChildren(isProcessingNow);

_inCoursePanDelay = 0;

_interactions.Remove(gestureId);
}

private bool? CheckSwipe()
Expand Down Expand Up @@ -1320,14 +1326,14 @@ private void AddRangeViewsInUse(Guid gestureId)
}
}

private void RemoveRangeViewsInUse(Guid gestureId)
private void RemoveRangeViewsInUse(Guid gestureId, bool isProcessingNow)
{
lock (_viewsInUseLocker)
{
foreach (var view in _viewsGestureCounter[gestureId])
{
_viewsInUse.Remove(view);
if (_gestureId != gestureId && !_viewsInUse.Contains(view))
if (isProcessingNow && !_viewsInUse.Contains(view))
{
CleanView(view);
}
Expand Down
14 changes: 14 additions & 0 deletions PanCardView/Enums/InteractionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

using Xamarin.Forms;

namespace PanCardView.Enums
{
[Flags]
public enum InteractionType
{
Gesture = -1,
Animation = 1
}
}

3 changes: 3 additions & 0 deletions PanCardView/PanCardView.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
<Compile Include="SceneView.cs" />
<Compile Include="Processors\BaseSceneBackProcessor.cs" />
<Compile Include="Processors\BaseSceneFrontProcessor.cs" />
<Compile Include="Utility\InteractionQueue.cs" />
<Compile Include="Enums\InteractionType.cs" />
<Compile Include="Utility\InteractionItem.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Processors\" />
Expand Down
10 changes: 10 additions & 0 deletions PanCardView/Utility/InteractionItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using PanCardView.Enums;
namespace PanCardView.Utility
{
public struct InteractionItem
{
public Guid Id { get; set; }
public InteractionType Type { get; set; }
}
}
51 changes: 51 additions & 0 deletions PanCardView/Utility/InteractionQueue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using PanCardView.Enums;
using System.Collections.Generic;
using System.Linq;

namespace PanCardView.Utility
{
public sealed class InteractionQueue
{
private readonly List<InteractionItem> _queue = new List<InteractionItem>();
private readonly object _queueLocker = new object();

public Guid GetFirstId(InteractionType type = InteractionType.Gesture | InteractionType.Animation)
{
lock(_queueLocker)
{
return _queue.FirstOrDefault(i => type.HasFlag(i.Type)).Id;
}
}

public void Add(Guid id, InteractionType type)
{
lock (_queueLocker)
{
_queue.Add(new InteractionItem
{
Id = id,
Type = type
});
}
}

public void Remove(Guid id)
{
lock (_queueLocker)
{
var item = _queue.FirstOrDefault(i => i.Id == id);
_queue.Remove(item);
}
}

public bool CheckLastId(Guid id, InteractionType type = InteractionType.Gesture | InteractionType.Animation)
{
lock(_queueLocker)
{
return _queue.LastOrDefault(i => type.HasFlag(i.Type)).Id == id;
}
}
}
}

0 comments on commit 545eb78

Please sign in to comment.