Skip to content

Commit

Permalink
https://github.com/AndreiMisiukevich/CardView/issues/158
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMisiukevich committed Feb 3, 2019
1 parent 9289188 commit dc74808
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
38 changes: 24 additions & 14 deletions PanCardView/CardsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,10 @@ protected virtual void OnSizeChanged()

protected virtual void SetupBackViews(int? index = null)
{
SetupNextView(index);
SetupPrevView(index);
var realIndex = index ?? SelectedIndex;

SetupNextView(realIndex);
SetupPrevView(realIndex);

if (IsRightToLeftFlowDirectionEnabled)
{
Expand Down Expand Up @@ -634,18 +636,20 @@ protected virtual async Task<bool> TryAutoNavigate()
realDirection = (AnimationDirection)Sign(-(int)realDirection);
}

var oldView = CurrentView;
SetupBackViews(OldIndex);
ResetActiveInactiveBackViews(realDirection);
SwapViews(realDirection);
AddChild(oldView, CurrentView);

var views = CurrentBackViews
.Union(CurrentInactiveBackViews)
.Union(Enumerable.Repeat(CurrentView, 1))
.Where(x => x != null);

var animationId = Guid.NewGuid();

StartAutoNavigation(views, animationId, animationDirection);
await Task.Delay(5);
var autoNavigationTask = Task.WhenAll(
BackViewProcessor.HandleAutoNavigate(CurrentBackViews, this, realDirection, CurrentInactiveBackViews),
FrontViewProcessor.HandleAutoNavigate(Enumerable.Repeat(CurrentView, 1), this, realDirection, Enumerable.Empty<View>()));
Expand All @@ -657,25 +661,22 @@ protected virtual async Task<bool> TryAutoNavigate()
return true;
}

protected virtual void SetupNextView(int? index = null)
protected virtual void SetupNextView(int index)
{
var realIndex = index ?? SelectedIndex;
var indeces = new int[BackViewsDepth];
for (int i = 0; i < indeces.Length; ++i)
{
indeces[i] = realIndex + 1 + i;
indeces[i] = index + 1 + i;
}

NextViews = GetViews(AnimationDirection.Next, BackViewProcessor, indeces);
}

protected virtual void SetupPrevView(int? index = null)
protected virtual void SetupPrevView(int index)
{
var realIndex = index ?? SelectedIndex;

var prevIndex = IsOnlyForwardDirection
? realIndex + 1
: realIndex - 1;
? index + 1
: index - 1;


var indeces = new int[BackViewsDepth];
Expand All @@ -686,7 +687,7 @@ protected virtual void SetupPrevView(int? index = null)
{
incValue = -incValue;
}
indeces[i] = realIndex + incValue;
indeces[i] = index + incValue;
}

PrevViews = GetViews(AnimationDirection.Prev, BackViewProcessor, indeces);
Expand Down Expand Up @@ -795,20 +796,29 @@ private void StartAutoNavigation(IEnumerable<View> views, Guid animationId, Anim

private void EndAutoNavigation(IEnumerable<View> views, Guid animationId, AnimationDirection animationDirection)
{
var isProcessingNow = !_interactions.CheckLastId(animationId);

_inCoursePanDelay = 0;
if (views != null)
{
lock (_viewsInUseLocker)
{
var depth = BackViewsDepth;
foreach (var view in views)
{
_viewsInUse.Remove(view);
if (isProcessingNow &&
!_viewsInUse.Contains(view) &&
depth <= 1 &&
view != CurrentView)
{
CleanView(view);
}
}
}
}
IsAutoInteractionRunning = false;

var isProcessingNow = !_interactions.CheckLastId(animationId);
IsAutoInteractionRunning = false;
RemoveRedundantChildren(isProcessingNow);
FireItemAppearing(InteractionType.Auto, animationDirection != AnimationDirection.Prev, SelectedIndex);
_interactions.Remove(animationId);
Expand Down
2 changes: 2 additions & 0 deletions PanCardView/Processors/BaseCardFrontViewProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,14 @@ protected virtual void ResetInitialState(View view, bool isVisible = true)
{
if (view != null)
{
view.BatchBegin();
view.Scale = 1;
view.Opacity = 1;
view.TranslationX = 0;
view.Rotation = 0;
view.TranslationY = 0;
view.IsVisible = isVisible;
view.BatchCommit();
}
}

Expand Down
2 changes: 2 additions & 0 deletions PanCardView/Processors/BaseCarouselFrontViewProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public virtual void HandleInitView(IEnumerable<View> views, CardsView cardsView,
var view = views.FirstOrDefault();
if (view != null)
{
view.BatchBegin();
view.TranslationX = 0;
view.IsVisible = true;
view.BatchCommit();
}
}

Expand Down
2 changes: 2 additions & 0 deletions PanCardView/Processors/BaseCoverFlowFrontViewProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ public virtual void HandleInitView(IEnumerable<View> views, CardsView cardsView,
var view = views.FirstOrDefault();
if (view != null)
{
view.BatchBegin();
view.TranslationX = 0;
view.IsVisible = true;
view.BatchCommit();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public CardsSampleViewModel()

PanPositionChangedCommand = new Command(v =>
{
if(IsAutoAnimationRunning || IsUserInteractionRunning)
{
return;
}

var index = CurrentIndex + ((bool)v ? 1 : -1);
if (index < 0 || index >= Items.Count)
{
Expand Down Expand Up @@ -60,7 +65,11 @@ public int CurrentIndex
}
}

public ObservableCollection<object> Items { get; }
public bool IsAutoAnimationRunning { get; set; }

public bool IsUserInteractionRunning { get; set; }

public ObservableCollection<object> Items { get; }

private string CreateSource()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<cards:CoverFlowView
PositionShiftValue="60"
IsCyclical="false"
IsAutoInteractionRunning="{Binding IsAutoAnimationRunning}"
IsUserInteractionRunning="{Binding IsUserInteractionRunning}"
SelectedIndex="{Binding CurrentIndex}"
ItemsSource="{Binding Items}">
<cards:CoverFlowView.ItemTemplate>
Expand Down

0 comments on commit dc74808

Please sign in to comment.