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 93bdbea commit 9289188
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 43 deletions.
75 changes: 40 additions & 35 deletions PanCardView/CardsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,10 @@ protected virtual void OnSizeChanged()
ForceLayout();
}

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

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

var oldView = CurrentView;
var newView = PrepareView(SelectedIndex, AnimationDirection.Current, Enumerable.Empty<View>());
CurrentView = newView;
SetupBackViews(OldIndex);
ResetActiveInactiveBackViews(realDirection);
SwapViews(realDirection);

BackViewProcessor.HandleInitView(Enumerable.Repeat(CurrentView, 1), this, realDirection);

SetupLayout(CurrentView);

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

var animationId = Guid.NewGuid();
StartAutoNavigation(oldView, animationId, animationDirection);

StartAutoNavigation(views, animationId, animationDirection);
var autoNavigationTask = Task.WhenAll(
BackViewProcessor.HandleAutoNavigate(Enumerable.Repeat(oldView, 1), this, realDirection, CurrentInactiveBackViews),
BackViewProcessor.HandleAutoNavigate(CurrentBackViews, this, realDirection, CurrentInactiveBackViews),
FrontViewProcessor.HandleAutoNavigate(Enumerable.Repeat(CurrentView, 1), this, realDirection, Enumerable.Empty<View>()));

if (ItemTemplate != null)
{
SetupBackViews();
}

await autoNavigationTask;
EndAutoNavigation(oldView, animationId, animationDirection);

EndAutoNavigation(views, animationId, animationDirection);

return true;
}

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

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

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

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


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

PrevViews = GetViews(AnimationDirection.Prev, BackViewProcessor, indeces);
Expand Down Expand Up @@ -773,9 +772,9 @@ private void SetPanGesture(bool _isForceRemove = false)
}
}

private void StartAutoNavigation(View oldView, Guid animationId, AnimationDirection animationDirection)
private void StartAutoNavigation(IEnumerable<View> views, Guid animationId, AnimationDirection animationDirection)
{
if (oldView != null)
if (views != null)
{
_interactions.Add(animationId, InteractionType.Auto, InteractionState.Removing);
IsAutoInteractionRunning = true;
Expand All @@ -785,24 +784,30 @@ private void StartAutoNavigation(View oldView, Guid animationId, AnimationDirect
}
lock (_viewsInUseLocker)
{
_viewsInUse.Add(oldView);
foreach(var view in views)
{
_viewsInUse.Add(view);
}
}
FireItemDisappearing(InteractionType.Auto, animationDirection != AnimationDirection.Prev, OldIndex);
}
}

private void EndAutoNavigation(View oldView, Guid animationId, AnimationDirection animationDirection)
private void EndAutoNavigation(IEnumerable<View> views, Guid animationId, AnimationDirection animationDirection)
{
_inCoursePanDelay = 0;
if (oldView != null)
if (views != null)
{
lock (_viewsInUseLocker)
{
_viewsInUse.Remove(oldView);
CleanView(oldView);
foreach (var view in views)
{
_viewsInUse.Remove(view);
}
}
}
IsAutoInteractionRunning = false;

var isProcessingNow = !_interactions.CheckLastId(animationId);
RemoveRedundantChildren(isProcessingNow);
FireItemAppearing(InteractionType.Auto, animationDirection != AnimationDirection.Prev, SelectedIndex);
Expand Down Expand Up @@ -964,9 +969,6 @@ private async void OnTouchEnded()
);
}

var oldView = CurrentBackViews.FirstOrDefault();
var newView = CurrentView;

FireUserInteracted(UserInteractionStatus.Ending, diff, oldIndex);
if (isNextSelected.HasValue)
{
Expand Down Expand Up @@ -1127,6 +1129,9 @@ private void SwapViews(bool isNext)
NextViews = CurrentBackViews;
}

private void SwapViews(AnimationDirection animationDirection)
=> SwapViews(animationDirection == AnimationDirection.Next);

private IEnumerable<View> GetViews(AnimationDirection animationDirection, ICardProcessor processor, params int[] indeces)
{
var views = new View[indeces.Length];
Expand Down
5 changes: 1 addition & 4 deletions PanCardView/CoverFlowView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public double PositionShiftValue

protected override int DefaultMaxChildrenCount => 17;

protected override int DefaultDesiredMaxChildrenCount => 12;

protected override Task<bool> TryAutoNavigate()
=> Task.FromResult(false);
protected override int DefaultDesiredMaxChildrenCount => 12;
}
}
11 changes: 10 additions & 1 deletion PanCardView/Processors/BaseCardBackViewProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,26 @@ public virtual void HandleInitView(IEnumerable<View> views, CardsView cardsView,
var view = views.FirstOrDefault();
if (view != null)
{
view.BatchBegin();
view.TranslationX = 0;
view.Rotation = 0;
view.TranslationY = 0;
view.Opacity = 1;
view.IsVisible = false;
view.Scale = InitialScale;
view.BatchCommit();
}
}

public virtual void HandleCleanView(IEnumerable<View> views, CardsView cardsView)
=> HandleInitView(views, cardsView, AnimationDirection.Null);
{
var view = views.FirstOrDefault();
if (view != null)
{
view.IsVisible = false;
view.TranslationX = cardsView.Width;
}
}

public virtual void HandlePanChanged(IEnumerable<View> views, CardsView cardsView, double xPos, AnimationDirection animationDirection, IEnumerable<View> inactiveViews)
{
Expand Down
11 changes: 10 additions & 1 deletion PanCardView/Processors/BaseCarouselBackViewProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@ public virtual void HandleInitView(IEnumerable<View> views, CardsView cardsView,
var view = views.FirstOrDefault();
if(view != null)
{
view.BatchBegin();
view.TranslationX = Sign((int)animationDirection) * cardsView.Width;
view.IsVisible = false;
view.BatchCommit();
}
}

public virtual void HandleCleanView(IEnumerable<View> views, CardsView cardsView)
=> HandleInitView(views, cardsView, AnimationDirection.Null);
{
var view = views.FirstOrDefault();
if (view != null)
{
view.IsVisible = false;
view.TranslationX = cardsView.Width;
}
}

public virtual void HandlePanChanged(IEnumerable<View> views, CardsView cardsView, double xPos, AnimationDirection animationDirection, IEnumerable<View> inactiveViews)
{
Expand Down
6 changes: 6 additions & 0 deletions PanCardView/Processors/BaseCoverFlowBackViewProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public virtual void HandleInitView(IEnumerable<View> views, CardsView cardsView,
{
continue;
}
view.BatchBegin();
view.TranslationX = Sign((int)animationDirection) * GetStep(cardsView) * index;
view.IsVisible = true;
view.BatchCommit();
}
}

Expand All @@ -39,7 +42,10 @@ public virtual void HandleCleanView(IEnumerable<View> views, CardsView cardsView
{
continue;
}
view.BatchBegin();
view.TranslationX = cardsView.Width;
view.IsVisible = false;
view.BatchCommit();
}
}

Expand Down
1 change: 1 addition & 0 deletions PanCardView/Processors/BaseCoverFlowFrontViewProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public virtual void HandleInitView(IEnumerable<View> views, CardsView cardsView,
if (view != null)
{
view.TranslationX = 0;
view.IsVisible = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,26 @@ public partial class CoverFlowSampleXamlView : ContentPage
{
public CoverFlowSampleXamlView()
{
InitializeComponent();
InitializeComponent();

var prevItem = new ToolbarItem
{
Text = "**Prev**",
Icon = "prev",
CommandParameter = false
};
prevItem.SetBinding(MenuItem.CommandProperty, nameof(CardsSampleViewModel.PanPositionChangedCommand));

var nextItem = new ToolbarItem
{
Text = "**Next**",
Icon = "next",
CommandParameter = true
};
nextItem.SetBinding(MenuItem.CommandProperty, nameof(CardsSampleViewModel.PanPositionChangedCommand));

ToolbarItems.Add(prevItem);
ToolbarItems.Add(nextItem);
}
}
}
2 changes: 1 addition & 1 deletion PanCardViewSample/iOS/PanCardViewSample.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<MtouchNoSymbolStrip>true</MtouchNoSymbolStrip>
<MtouchFastDev>true</MtouchFastDev>
<IOSDebuggerPort>11763</IOSDebuggerPort>
<MtouchLink>SdkOnly</MtouchLink>
<MtouchLink>None</MtouchLink>
<MtouchArch>x86_64</MtouchArch>
<MtouchHttpClientHandler>HttpClientHandler</MtouchHttpClientHandler>
<PlatformTarget>x86</PlatformTarget>
Expand Down

0 comments on commit 9289188

Please sign in to comment.